blob: fd614a4ac1730aa79cd8e24f058135f7517b4732 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070039import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Tony Wickhama3c74d12015-10-23 11:43:47 -070055import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070069import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070088import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070097import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080098import com.android.launcher3.allapps.DefaultAppSearchController;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Tony Wickhame0c33232016-02-08 11:37:04 -0800104import com.android.launcher3.config.FeatureFlags;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Sunny Goyal26119432016-02-18 22:09:23 +0000109import com.android.launcher3.folder.Folder;
110import com.android.launcher3.folder.FolderIcon;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800111import com.android.launcher3.logging.UserEventLogger;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700112import com.android.launcher3.model.WidgetsModel;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800113import com.android.launcher3.logging.LoggerUtils;
114import com.android.launcher3.userevent.nano.LauncherLogProto;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700115import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700116import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700117import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700118import com.android.launcher3.util.Thunk;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700119import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700120import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700121import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700123
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700124import java.io.FileDescriptor;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700125import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Sunny Goyal29538332016-02-18 09:10:19 -0800129import java.util.Collections;
Adam Cohen4caf2982013-08-20 18:54:31 -0700130import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700132import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700133import java.util.List;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800134import java.util.Locale;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136/**
137 * Default launcher application.
138 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100139public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700140 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700141 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
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;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
Dan Sandlerd5024042014-01-09 15:01:33 -0500150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Sunny Goyal28c6b962015-10-12 11:42:05 -0700160 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700166 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
167
Mathew Inwood876a8462013-06-14 14:12:41 +0100168 /**
169 * IntentStarter uses request codes starting with this. This must be greater than all activity
170 * request codes used internally.
171 */
172 protected static final int REQUEST_LAST = 100;
173
Michael Jurka0a457bf2012-11-19 14:05:05 -0800174 // To turn on these properties, type
175 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700186 // Type: Content Values / parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohen432609a2014-03-13 17:03:22 -0700193 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Adam Cohen3ed316a2014-07-23 18:21:20 -0700196 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
197 static final String ACTION_FIRST_LOAD_COMPLETE =
198 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
199
Sunny Goyal594d76d2014-11-06 10:12:54 -0800200 private static final String QSB_WIDGET_ID = "qsb_widget_id";
201 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700206 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
207 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700208
Adam Cohen091440a2015-03-18 14:16:05 -0700209 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700210 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700212 private boolean mIsSafeModeEnabled;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohen4f8071b2016-02-13 16:06:05 -0800215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 500;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800229 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700232
233 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalffe83f12014-08-14 17:39:34 -0700235 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700238 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800239 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800240 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700241
Michael Jurka0280c3b2010-09-17 15:00:07 -0700242 private int[] mTmpAddItemCellCoordinates = new int[2];
243
Sunny Goyal316490e2015-06-02 09:38:28 -0700244 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800245 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700248 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249
Winson Chungc51db6a2011-10-05 11:44:49 -0700250 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700251 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
253 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700254 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700256 // Main container view and the model for the widget tray screen.
257 @Thunk WidgetsContainerView mWidgetsView;
258 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700259
Sunny Goyal594d76d2014-11-06 10:12:54 -0800260 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700279 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700283 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700285 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Sunny Goyal639e9062015-08-19 19:17:06 -0700288 private LauncherClings mClings;
289
Sunny Goyale2df0622015-04-24 11:27:00 -0700290 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Adam Cohen61f560d2013-09-30 15:58:20 -0700292 private View.OnTouchListener mHapticFeedbackTouchListener;
293
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 // Related to the auto-advancing of widgets
295 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 private static final int ADVANCE_INTERVAL = 20000;
297 private static final int ADVANCE_STAGGER = 250;
298
299 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 private long mAutoAdvanceSentTime;
301 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700303
Winson Chung400438b2011-01-16 17:53:48 -0800304 // Determines how long to wait after a rotation before restoring the screen orientation to
305 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700312
Sunny Goyala1365452015-10-01 15:46:24 -0700313 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
314 private static final Date sDateStamp = new Date();
315 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700317
Winson Chung46353de2012-02-16 14:05:10 -0800318 // We only want to get the SharedPreferences once since it does an FS stat each time we get
319 // it from the context.
320 private SharedPreferences mSharedPrefs;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700329 private DeviceProfile mDeviceProfile;
330
Adam Cohen4b66bf32015-09-18 12:15:19 -0700331 private boolean mMoveToDefaultScreenFromNewIntent;
332
Winson Chung13eb5272015-05-11 16:30:13 -0700333 // This is set to the view that launched the activity that navigated the user away from
334 // launcher. Since there is no callback for when the activity has finished launching, enable
335 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 private BubbleTextView mWaitingForResume;
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
339 new HashMap<String, CustomAppWidget>();
340
Adam Cohen59400422014-03-05 18:07:04 -0800341 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700342 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
343 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800344 }
345 }
346
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700349 if (mWorkspace != null) {
350 mWorkspace.buildPageHardwareLayers();
351 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 }
353 };
354
Adam Cohendb364c32014-05-20 14:23:40 -0700355 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356
Adam Cohen091440a2015-03-18 14:16:05 -0700357 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 int requestCode;
359 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700361 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 int cellX;
363 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700364 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 }
366
Daniel Sandlerff02d492013-08-05 02:12:05 -0400367 private Stats mStats;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800368 private UserEventLogger mUserEventLogger;
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800369
Adam Cohenf9c184a2016-01-15 16:47:43 -0800370 public FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700371 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400372
373 @Thunk void setOrientation() {
374 if (mRotationEnabled) {
375 unlockScreenOrientation(true);
376 } else {
377 setRequestedOrientation(
378 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700379 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400380 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700381
Sunny Goyal7779d622015-06-11 16:18:39 -0700382 private Runnable mUpdateOrientationRunnable = new Runnable() {
383 public void run() {
384 setOrientation();
385 }
386 };
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 mDeviceProfile = getResources().getConfiguration().orientation
415 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700416 app.getInvariantDeviceProfile().landscapeProfile
417 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700418
Sunny Goyalf7258242015-10-19 16:59:07 -0700419 mSharedPrefs = Utilities.getPrefs(this);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700420 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800421 mModel = app.setLauncher(this);
422 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700423
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700425 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Daniel Sandlerff02d492013-08-05 02:12:05 -0400427 mStats = new Stats(this);
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800428 initLogger();
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429
Sunny Goyalffe83f12014-08-14 17:39:34 -0700430 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
433 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700435 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
436 // this also ensures that any synchronous binding below doesn't re-trigger another
437 // LauncherModel load.
438 mPaused = false;
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200441 android.os.Debug.startMethodTracing(
442 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700446
Tony Wickhameef44322015-10-20 13:24:36 -0700447 app.getInvariantDeviceProfile().landscapeProfile.setSearchBarHeight(getSearchBarHeight());
448 app.getInvariantDeviceProfile().portraitProfile.setSearchBarHeight(getSearchBarHeight());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700450 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Sunny Goyal8f3819b2016-02-23 14:49:22 -0800480 mRotationEnabled = getResources().getBoolean(R.bool.allow_rotation);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700485 }
486
487 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
488 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400489 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
493 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700494
495 if (shouldShowIntroScreen()) {
496 showIntroScreen();
497 } else {
498 showFirstRunActivity();
499 showFirstRunClings();
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
502
Sunny Goyal7779d622015-06-11 16:18:39 -0700503 @Override
504 public void onSettingsChanged(String settings, boolean value) {
505 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
506 mRotationEnabled = value;
507 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
508 mUpdateOrientationRunnable.run();
509 }
510 }
511 }
512
Adam Cohen9211d422014-10-07 18:14:53 -0700513 private LauncherCallbacks mLauncherCallbacks;
514
515 public void onPostCreate(Bundle savedInstanceState) {
516 super.onPostCreate(savedInstanceState);
517 if (mLauncherCallbacks != null) {
518 mLauncherCallbacks.onPostCreate(savedInstanceState);
519 }
520 }
521
Sunny Goyal32554d12015-12-03 15:31:25 -0800522 /**
523 * Call this after onCreate to set or clear overlay.
524 */
525 public void setLauncherOverlay(LauncherOverlay overlay) {
526 if (overlay != null) {
527 overlay.setOverlayCallbacks(new LauncherOverlayCallbacksImpl());
528 }
529 mWorkspace.setLauncherOverlay(overlay);
530 }
531
Adam Cohen9211d422014-10-07 18:14:53 -0700532 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
533 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700535 private boolean mWorkspaceImportanceStored = false;
536 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 private int mWorkspaceImportanceForAccessibility =
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800538 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
540
541 @Override
542 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700544 return;
545 }
546 // The underlying workspace and hotseat are temporarily suppressed by the search
Sunny Goyal08442b82015-10-21 17:15:08 -0700547 // overlay. So they shouldn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700548 if (mWorkspace != null) {
549 mWorkspaceImportanceForAccessibility =
550 mWorkspace.getImportantForAccessibility();
551 mWorkspace.setImportantForAccessibility(
552 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
553 mWorkspaceImportanceStored = true;
554 }
555 if (mHotseat != null) {
556 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
557 mHotseat.setImportantForAccessibility(
558 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
559 mHotseatImportanceStored = true;
560 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700561 }
562
563 @Override
564 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700565 if (mWorkspaceImportanceStored && mWorkspace != null) {
566 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
567 }
568 if (mHotseatImportanceStored && mHotseat != null) {
569 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
570 }
571 mWorkspaceImportanceStored = false;
572 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700573 }
574 });
Adam Cohen9211d422014-10-07 18:14:53 -0700575 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700576 }
577
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700578 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700579 public void onLauncherProviderChange() {
580 if (mLauncherCallbacks != null) {
581 mLauncherCallbacks.onLauncherProviderChange();
582 }
583 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700584
Adam Cohen9211d422014-10-07 18:14:53 -0700585 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700586 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700587 if (mLauncherCallbacks != null) {
588 return mLauncherCallbacks.hasCustomContentToLeft();
589 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700590 return false;
591 }
592
Dave Hawkeya8881582013-09-17 15:55:33 -0600593 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500594 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 * {@link #addToCustomContentPage}. This will only be invoked if
596 * {@link #hasCustomContentToLeft()} is {@code true}.
597 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500598 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700599 if (mLauncherCallbacks != null) {
600 mLauncherCallbacks.populateCustomContentContainer();
601 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600602 }
603
604 /**
605 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
606 * ensure the custom content page is added or removed if necessary.
607 */
608 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600609 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 // Not bound yet, wait for bindScreens to be called.
611 return;
612 }
613
614 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
615 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500616 mWorkspace.createCustomContentContainer();
617 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
619 mWorkspace.removeCustomContentPage();
620 }
621 }
622
Anton Hanssona2f665f2013-09-26 12:18:32 +0100623 public Stats getStats() {
624 return mStats;
625 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800626
627 /**
628 * Logger object is a singleton and does not have to be coupled with the foreground activity.
629 * Since most user event logging is done on the UI, the object is retrieved from the
630 * callback for convenience.
631 */
632 private void initLogger() {
633 if (mLauncherCallbacks != null) {
634 mUserEventLogger = mLauncherCallbacks.getLogger();
635 }
636 if (mUserEventLogger == null) {
637 mUserEventLogger = new UserEventLogger() {
638 @Override
639 public void processEvent(LauncherLogProto.LauncherEvent ev) {
640 if (ev.action.touch == LauncherLogProto.Action.TAP && ev.srcTarget.itemType == LauncherLogProto.APP_ICON) {
641 Log.d(TAG, String.format(Locale.US, "action:%s target:%s\n\telapsed container %d ms session %d ms",
642 LoggerUtils.getActionStr(ev.action),
643 LoggerUtils.getTargetStr(ev.srcTarget),
644 ev.elapsedContainerMillis,
645 ev.elapsedSessionMillis));
646 }
647 }
648 };
649 }
650 }
651
652 public UserEventLogger getLogger() {
653 return mUserEventLogger;
654 }
Anton Hanssona2f665f2013-09-26 12:18:32 +0100655
Hyunyoung Song3f471442015-04-08 19:01:34 -0700656 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700657 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
658 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700659 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700660 }
661
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700663 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
664 // This cast is safe as long as the id < 0x00FFFFFF
665 // Since we jail all the dynamically generated views, there should be no clashes
666 // with any other views.
667 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000668 }
669
670 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
672 * a configuration step, this allows the proper animations to run after other transitions.
673 */
Adam Cohendb364c32014-05-20 14:23:40 -0700674 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700675 long screenId = args.screenId;
676 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
677 // When the screen id represents an actual screen (as opposed to a rank) we make sure
678 // that the drop page actually exists.
679 screenId = ensurePendingDropLayoutExists(args.screenId);
680 }
Adam Cohendb364c32014-05-20 14:23:40 -0700681
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700684 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700686 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700688 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700689 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700690 case REQUEST_RECONFIGURE_APPWIDGET:
691 completeRestoreAppWidget(args.appWidgetId);
692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 }
Michael Jurka27614d22012-04-02 06:40:22 -0700694 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
695 // if you turned the screen off and then back while in All Apps, Launcher would not
696 // return to the workspace. Clearing mAddInfo.container here fixes this issue
697 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700698 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
700
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800701 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700703 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700704 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700705 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800706 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700707
Adam Cohenad4e15c2013-10-17 16:21:35 -0700708 Runnable exitSpringLoaded = new Runnable() {
709 @Override
710 public void run() {
711 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
712 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
713 }
714 };
715
Michael Jurka8b805b12012-04-18 14:23:14 -0700716 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700717 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700718 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700719 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
720 if (resultCode == RESULT_CANCELED) {
721 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700722 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700724 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800725 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700726 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700727
728 // When the user has granted permission to bind widgets, we should check to see if
729 // we can inflate the default search bar widget.
730 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 }
732 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200733 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
734 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700735 // User could have free-scrolled between pages before picking a wallpaper; make sure
736 // we move to the closest one now.
737 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700738 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200739 }
740 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200742
Adam Cohened66b2b2012-01-23 17:28:51 -0800743 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700744 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700745
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800747 // We have special handling for widgets
748 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 final int appWidgetId;
750 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
751 : -1;
752 if (widgetId < 0) {
753 appWidgetId = pendingAddWidgetId;
754 } else {
755 appWidgetId = widgetId;
756 }
757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800759 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700760 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
761 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 result = RESULT_CANCELED;
763 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed(false, 0, null);
768 }
769 };
Adam Cohendb364c32014-05-20 14:23:40 -0700770 if (workspaceLocked) {
771 // No need to remove the empty screen if we're mid-binding, as the
772 // the bind will not add the empty screen.
773 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
774 } else {
775 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
776 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
777 }
Winson Chung5aaab772012-04-27 15:24:02 -0700778 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700779 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700780 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
781 // When the screen id represents an actual screen (as opposed to a rank)
782 // we make sure that the drop page actually exists.
783 mPendingAddInfo.screenId =
784 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
785 }
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
787
788 dropLayout.setDropPending(true);
789 final Runnable onComplete = new Runnable() {
790 @Override
791 public void run() {
792 completeTwoStageWidgetDrop(resultCode, appWidgetId);
793 dropLayout.setDropPending(false);
794 }
795 };
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 } else {
799 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
800 mPendingAddInfo);
801 sPendingAddItem = args;
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 return;
805 }
806
Sunny Goyalff572272014-07-23 13:58:07 -0700807 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
808 if (resultCode == RESULT_OK) {
809 // Update the widget view.
810 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
811 pendingAddWidgetId, mPendingAddInfo);
812 if (workspaceLocked) {
813 sPendingAddItem = args;
814 } else {
815 completeAdd(args);
816 }
817 }
818 // Leave the widget in the pending state if the user canceled the configure.
819 return;
820 }
821
Sunny Goyalaad90582015-07-09 14:22:50 -0700822 if (requestCode == REQUEST_CREATE_SHORTCUT) {
823 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
824 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
825 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
826 mPendingAddInfo);
827 if (isWorkspaceLocked()) {
828 sPendingAddItem = args;
829 } else {
830 completeAdd(args);
831 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 }
834 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700835 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800840
841 }
842
843 @Override
844 protected void onActivityResult(
845 final int requestCode, final int resultCode, final Intent data) {
846 handleActivityResult(requestCode, resultCode, data);
847 if (mLauncherCallbacks != null) {
848 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 }
851
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600852 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700853 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600854 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700855 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
856 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
857 View v = null;
858 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
859 if (layout != null) {
860 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
861 }
862 Intent intent = sPendingAddItem.intent;
863 sPendingAddItem = null;
864 if (grantResults.length > 0
865 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
866 startActivity(v, intent, null);
867 } else {
868 // TODO: Show a snack bar with link to settings
869 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
870 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
871 }
872 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
875 grantResults);
876 }
877 }
878
Adam Cohendb364c32014-05-20 14:23:40 -0700879 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
880 appWidgetId, ItemInfo info) {
881 PendingAddArguments args = new PendingAddArguments();
882 args.requestCode = requestCode;
883 args.intent = data;
884 args.container = info.container;
885 args.screenId = info.screenId;
886 args.cellX = info.cellX;
887 args.cellY = info.cellY;
888 args.appWidgetId = appWidgetId;
889 return args;
890 }
891
892 /**
893 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
894 *
895 * @param screenId the screen id to check
896 * @return the new screen, or screenId if it exists
897 */
898 private long ensurePendingDropLayoutExists(long screenId) {
Sunny Goyal32554d12015-12-03 15:31:25 -0800899 CellLayout dropLayout = mWorkspace.getScreenWithId(screenId);
Adam Cohendb364c32014-05-20 14:23:40 -0700900 if (dropLayout == null) {
901 // it's possible that the add screen was removed because it was
902 // empty and a re-bind occurred
903 mWorkspace.addExtraEmptyScreen();
904 return mWorkspace.commitExtraEmptyScreen();
905 } else {
906 return screenId;
907 }
908 }
909
Adam Cohen091440a2015-03-18 14:16:05 -0700910 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Tony Wickhama501d492015-11-03 18:05:01 -0800911 CellLayout cellLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 Runnable onCompleteRunnable = null;
913 int animationType = 0;
914
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700915 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 if (resultCode == RESULT_OK) {
917 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
918 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700919 mPendingAddWidgetInfo);
920 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 onCompleteRunnable = new Runnable() {
922 @Override
923 public void run() {
924 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700926 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
927 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
929 };
930 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800931 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 if (mDragLayer.getAnimatedView() != null) {
935 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
936 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
937 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700938 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 // The animated view may be null in the case of a rotation during widget configuration
940 onCompleteRunnable.run();
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700945 protected void onStop() {
946 super.onStop();
947 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.onStop();
951 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700952 }
953
954 @Override
955 protected void onStart() {
956 super.onStart();
957 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStart();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200966 long startTime = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400969 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
Adam Cohen9211d422014-10-07 18:14:53 -0700971
972 if (mLauncherCallbacks != null) {
973 mLauncherCallbacks.preOnResume();
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 super.onResume();
Hyunyoung Songfbf19cc2016-02-19 12:14:47 -0800977 mUserEventLogger.resetElapsedSessionMillis();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700980 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800982 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700983 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700984 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700985 // view after launching an app, as they may be depending on the UI to be static to
986 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700987 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700988 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.WIDGETS) {
990 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700994 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700995 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
996 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700997
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800998 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001000 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001001 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001003 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1007 // execute them here
1008 long startTimeCallbacks = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTimeCallbacks = System.currentTimeMillis();
1011 }
1012
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1014 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001016 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 if (DEBUG_RESUME_TIME) {
1018 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1019 (System.currentTimeMillis() - startTimeCallbacks));
1020 }
Michael Jurka447bf842013-05-15 14:52:15 +02001021 }
Michael Jurka54554252013-08-01 12:52:23 +02001022 if (mOnResumeCallbacks.size() > 0) {
1023 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1024 mOnResumeCallbacks.get(i).run();
1025 }
1026 mOnResumeCallbacks.clear();
1027 }
Winson Chunge4e50662012-01-23 14:45:13 -08001028
1029 // Reset the pressed state of icons that were locked in the press state while activities
1030 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001032 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 mWaitingForResume.setStayPressed(false);
1034 }
Winson Chung82963d52013-10-09 11:20:57 -07001035
Adam Cohen06dff352012-06-01 17:17:08 -07001036 // It is possible that widgets can receive updates while launcher is not in the foreground.
1037 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1038 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1039 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001040 if (!isWorkspaceLoading()) {
1041 getWorkspace().reinflateWidgetsIfNecessary();
1042 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001043 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001044
Michael Jurka447bf842013-05-15 14:52:15 +02001045 if (DEBUG_RESUME_TIME) {
1046 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1047 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001048
Adam Cohen4b66bf32015-09-18 12:15:19 -07001049 // We want to suppress callbacks about CustomContent being shown if we have just received
1050 // onNewIntent while the user was present within launcher. In that case, we post a call
1051 // to move the user to the main screen (which will occur after onResume). We don't want to
1052 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1053 // suppress here.
1054 if (mWorkspace.getCustomContentCallbacks() != null
1055 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001057 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1059 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001060 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001061 }
1062 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001063 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001064 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001065 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001066
Sunny Goyal756adbc2015-04-16 15:20:51 -07001067 if (!isWorkspaceLoading()) {
1068 // Process any items that were added while Launcher was away.
1069 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1070 }
Adam Cohen9211d422014-10-07 18:14:53 -07001071
1072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
1081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001083 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001084 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001085 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001086
1087 // We call onHide() aggressively. The custom content callbacks should be able to
1088 // debounce excess onHide calls.
1089 if (mWorkspace.getCustomContentCallbacks() != null) {
1090 mWorkspace.getCustomContentCallbacks().onHide();
1091 }
Romain Guycbb89e42009-06-08 15:52:54 -07001092
Adam Cohen9211d422014-10-07 18:14:53 -07001093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 public interface LauncherOverlay {
1114
1115 /**
1116 * Touch interaction leading to overscroll has begun
1117 */
1118 public void onScrollInteractionBegin();
1119
1120 /**
1121 * Touch interaction related to overscroll has ended
1122 */
1123 public void onScrollInteractionEnd();
1124
1125 /**
1126 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1127 * screen (or in the case of RTL, the rightmost screen).
1128 */
Sunny Goyalc86df472016-02-25 09:19:38 -08001129 public void onScrollChange(float progress, boolean rtl);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130
1131 /**
Sunny Goyal32554d12015-12-03 15:31:25 -08001132 * Called when the launcher is ready to use the overlay
1133 * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
Adam Cohenc2d6e892014-10-16 09:49:24 -07001134 */
Sunny Goyal32554d12015-12-03 15:31:25 -08001135 public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001136 }
1137
Jun Mukai8af0cd82015-05-12 12:32:12 -07001138 public interface LauncherSearchCallbacks {
1139 /**
1140 * Called when the search overlay is shown.
1141 */
1142 public void onSearchOverlayOpened();
1143
1144 /**
1145 * Called when the search overlay is dismissed.
1146 */
1147 public void onSearchOverlayClosed();
1148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlayCallbacks {
Sunny Goyalc86df472016-02-25 09:19:38 -08001151 public void onScrollChanged(float progress);
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
Sunny Goyalc86df472016-02-25 09:19:38 -08001156 public void onScrollChanged(float progress) {
1157 if (mWorkspace != null) {
1158 mWorkspace.onOverlayScrollChanged(progress);
1159 }
1160 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 }
1162
Jorim Jaggid017f882014-01-14 17:08:48 -08001163 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001164 if (mLauncherCallbacks != null) {
1165 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001166 } else {
1167 // On devices with a locked orientation, we will at least have the allow rotation
1168 // setting.
Sunny Goyal8f3819b2016-02-23 14:49:22 -08001169 return !getResources().getBoolean(R.bool.allow_rotation);
Adam Cohen9211d422014-10-07 18:14:53 -07001170 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001171 }
1172
Adam Cohen9211d422014-10-07 18:14:53 -07001173 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001174 CustomContentCallbacks callbacks, String description) {
1175 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001176 }
1177
Adam Cohenedb40762013-07-18 16:45:45 -07001178 // The custom content needs to offset its content to account for the QSB
1179 public int getTopOffsetForCustomContent() {
1180 return mWorkspace.getPaddingTop();
1181 }
1182
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 @Override
1184 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001186 if (mModel.isCurrentCallbacks(this)) {
1187 mModel.stopLoader();
1188 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001189 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1190
Romain Guy13c2e7b2010-03-10 19:45:00 -08001191 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001192 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195 @Override
1196 public void onWindowFocusChanged(boolean hasFocus) {
1197 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001198 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001199
1200 if (mLauncherCallbacks != null) {
1201 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1202 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001203 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 private boolean acceptFilter() {
1206 final InputMethodManager inputManager = (InputMethodManager)
1207 getSystemService(Context.INPUT_METHOD_SERVICE);
1208 return !inputManager.isFullscreenMode();
1209 }
1210
1211 @Override
1212 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001213 final int uniChar = event.getUnicodeChar();
1214 final boolean handled = super.onKeyDown(keyCode, event);
1215 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1216 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1218 keyCode, event);
1219 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001221 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 // showSearchDialog()
1223 // If there are multiple keystrokes before the search dialog takes focus,
1224 // onSearchRequested() will be called for every keystroke,
1225 // but it is idempotent, so it's fine.
1226 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
1229
Joe Onorato8a9625e2010-01-28 15:55:35 -08001230 // Eat the long press event so the keyboard doesn't come up.
1231 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1232 return true;
1233 }
1234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 return handled;
1236 }
1237
Winson46163472015-09-22 17:31:56 -07001238 @Override
1239 public boolean onKeyUp(int keyCode, KeyEvent event) {
1240 if (keyCode == KeyEvent.KEYCODE_MENU) {
1241 // Ignore the menu key if we are currently dragging or are on the custom content screen
1242 if (!isOnCustomContent() && !mDragController.isDragging()) {
1243 // Close any open folders
1244 closeFolder();
1245
1246 // Stop resizing any widgets
1247 mWorkspace.exitWidgetResizeMode();
1248
1249 // Show the overview mode if we are on the workspace
1250 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1251 !mWorkspace.isSwitchingState()) {
1252 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001253 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001254 }
1255 }
1256 return true;
1257 }
1258 return super.onKeyUp(keyCode, event);
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
1265 private void clearTypedText() {
1266 mDefaultKeySsb.clear();
1267 mDefaultKeySsb.clearSpans();
1268 Selection.setSelection(mDefaultKeySsb, 0);
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1273 * State
1274 */
1275 private static State intToState(int stateOrdinal) {
1276 State state = State.WORKSPACE;
1277 final State[] stateValues = State.values();
1278 for (int i = 0; i < stateValues.length; i++) {
1279 if (stateValues[i].ordinal() == stateOrdinal) {
1280 state = stateValues[i];
1281 break;
1282 }
1283 }
1284 return state;
1285 }
1286
1287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 * Restores the previous state, if it exists.
1289 *
1290 * @param savedState The previous state.
1291 */
1292 private void restoreState(Bundle savedState) {
1293 if (savedState == null) {
1294 return;
1295 }
1296
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001298 if (state == State.APPS || state == State.WIDGETS) {
1299 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001300 }
1301
Adam Cohen21cd0022013-10-09 18:57:02 -07001302 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1303 PagedView.INVALID_RESTORE_PAGE);
1304 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001305 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
Sunny Goyal756cd262015-08-20 12:33:21 -07001308 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1309 if (itemValues != null) {
1310 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001311 AppWidgetProviderInfo info = savedState.getParcelable(
1312 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001313 mPendingAddWidgetInfo = info == null ?
1314 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1315
Adam Cohen4637b5a2013-11-04 18:21:24 -08001316 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001317 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 mRestoring = true;
1319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321
1322 /**
1323 * Finds all the views we need and configure them properly.
1324 */
1325 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001326 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001327
Craig Mautner360310b2012-10-26 15:13:08 -07001328 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001329 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001330 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1331 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001332 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001333 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001334
John Spurlock77e1f472013-09-11 10:09:51 -04001335 mLauncherView.setSystemUiVisibility(
1336 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
Winson Chung4c98d922011-05-31 16:50:48 -07001339 // Setup the drag layer
1340 mDragLayer.setup(this, dragController);
1341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 // Setup the hotseat
1343 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1344 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001345 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 }
1347
Winsone9f27272015-10-13 10:47:51 -07001348 // Setup the overview panel
1349 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350
Winson Chung4c98d922011-05-31 16:50:48 -07001351 // Setup the workspace
1352 mWorkspace.setHapticFeedbackEnabled(false);
1353 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001354 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001355 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001356
Tony Wickham34d2c912015-09-11 09:27:58 -07001357 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001358 mSearchDropTargetBar = (SearchDropTargetBar)
1359 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001360 // Get the app info bar
1361 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1362 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001363
Winson Chungef7f8742015-06-04 17:18:17 -07001364 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001365 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001366 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Sunny Goyalf51084c2016-02-18 00:40:49 +00001367 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1368 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1369 } else {
1370 mAppsView.setSearchBarController(new DefaultAppSearchController());
1371 }
Craig Mautner360310b2012-10-26 15:13:08 -07001372
Winson Chung3d503fb2011-07-13 17:25:49 -07001373 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001374 dragController.setDragScoller(mWorkspace);
Tony Wickham0f97b782015-12-02 17:55:07 -08001375 dragController.setScrollView(mDragLayer);
Winson Chung4c98d922011-05-31 16:50:48 -07001376 dragController.setMoveTarget(mWorkspace);
1377 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001378 if (mSearchDropTargetBar != null) {
1379 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001380 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001381 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001382 if (mAppInfoDropTargetBar != null) {
1383 mAppInfoDropTargetBar.setup(this, dragController);
1384 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001385
Sunny Goyal322d5562015-06-25 19:35:49 -07001386 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1387 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
Winsone9f27272015-10-13 10:47:51 -07001391 private void setupOverviewPanel() {
1392 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1393
1394 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1395 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1396 @Override
1397 public boolean onLongClick(View v) {
1398 return v.performClick();
1399 }
1400 };
1401
1402 // Bind wallpaper button actions
1403 View wallpaperButton = findViewById(R.id.wallpaper_button);
1404 wallpaperButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View view) {
1407 if (!mWorkspace.isSwitchingState()) {
1408 onClickWallpaperPicker(view);
1409 }
1410 }
1411 });
1412 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1413 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1414
1415 // Bind widget button actions
1416 mWidgetsButton = findViewById(R.id.widget_button);
1417 mWidgetsButton.setOnClickListener(new OnClickListener() {
1418 @Override
1419 public void onClick(View view) {
1420 if (!mWorkspace.isSwitchingState()) {
1421 onClickAddWidgetButton(view);
1422 }
1423 }
1424 });
1425 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1426 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 // Bind settings actions
1429 View settingsButton = findViewById(R.id.settings_button);
1430 boolean hasSettings = hasSettings();
1431 if (hasSettings) {
1432 settingsButton.setOnClickListener(new OnClickListener() {
1433 @Override
1434 public void onClick(View view) {
1435 if (!mWorkspace.isSwitchingState()) {
1436 onClickSettingsButton(view);
1437 }
1438 }
1439 });
1440 settingsButton.setOnLongClickListener(performClickOnLongClick);
1441 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1442 } else {
1443 settingsButton.setVisibility(View.GONE);
1444 }
1445
1446 mOverviewPanel.setAlpha(0f);
1447 }
1448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 * Sets the all apps button. This method is called from {@link Hotseat}.
1451 */
1452 public void setAllAppsButton(View allAppsButton) {
1453 mAllAppsButton = allAppsButton;
1454 }
1455
1456 public View getAllAppsButton() {
1457 return mAllAppsButton;
1458 }
1459
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001460 public View getWidgetsButton() {
1461 return mWidgetsButton;
1462 }
1463
Anjali Koppal5ad44842014-03-10 20:34:39 -07001464 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 * Creates a view representing a shortcut.
1466 *
1467 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001469 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001470 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 /**
1474 * Creates a view representing a shortcut inflated from the specified resource.
1475 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * @param parent The group the shortcut belongs to.
1477 * @param info The data structure describing the shortcut.
1478 *
1479 * @return A View inflated from layoutResId.
1480 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001481 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001482 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001483 parent, false);
1484 favorite.applyFromShortcutInfo(info, mIconCache);
1485 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001487 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 return favorite;
1489 }
1490
1491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * Add a shortcut to the workspace.
1493 *
1494 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001496 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001497 int cellY) {
1498 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001499 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Sunny Goyal5c97f512015-05-19 16:03:28 -07001501 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001502 if (info == null) {
1503 return;
1504 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001505 final View view = createShortcut(info);
1506
Sunny Goyal5c97f512015-05-19 16:03:28 -07001507 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 // First we check if we already know the exact location where we want to add this item.
1509 if (cellX >= 0 && cellY >= 0) {
1510 cellXY[0] = cellX;
1511 cellXY[1] = cellY;
1512 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001513
1514 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001515 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 true, null,null)) {
1517 return;
1518 }
1519 DragObject dragObject = new DragObject();
1520 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001521 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1522 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001523 return;
1524 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001525 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001527 }
1528
1529 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001530 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 return;
1532 }
1533
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001534 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001545 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 */
Adam Cohen091440a2015-03-18 14:16:05 -07001547 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001548 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1549
1550 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (appWidgetInfo == null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08001552 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 }
Romain Guycbb89e42009-06-08 15:52:54 -07001554
Adam Cohen59400422014-03-05 18:07:04 -08001555 if (appWidgetInfo.isCustomWidget) {
1556 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001557 }
1558
Adam Cohen59400422014-03-05 18:07:04 -08001559 LauncherAppWidgetInfo launcherInfo;
1560 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1561 launcherInfo.spanX = info.spanX;
1562 launcherInfo.spanY = info.spanY;
1563 launcherInfo.minSpanX = info.minSpanX;
1564 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001565 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001568 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 if (hostView == null) {
1572 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001573 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1574 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 } else {
1576 // The AppWidgetHostView has already been inflated and instantiated
1577 launcherInfo.hostView = hostView;
1578 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001579 launcherInfo.hostView.setVisibility(View.VISIBLE);
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001580 addAppWidgetToWorkspace(launcherInfo, appWidgetInfo, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001582 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Sunny Goyal25c2e3e2015-10-28 23:28:21 -07001585 private void addAppWidgetToWorkspace(LauncherAppWidgetInfo item,
1586 LauncherAppWidgetProviderInfo appWidgetInfo, boolean insert) {
1587 item.hostView.setTag(item);
1588 item.onBindAppWidget(this);
1589
1590 item.hostView.setFocusable(true);
1591 item.hostView.setOnFocusChangeListener(mFocusHandler);
1592
1593 mWorkspace.addInScreen(item.hostView, item.container, item.screenId,
1594 item.cellX, item.cellY, item.spanX, item.spanY, insert);
1595
1596 if (!item.isCustomWidget()) {
1597 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
1598 }
1599 }
1600
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1602 @Override
1603 public void onReceive(Context context, Intent intent) {
1604 final String action = intent.getAction();
1605 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1606 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001607 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001608 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001609
Winson Chungc100e8e2011-08-09 16:02:43 -07001610 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001611 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001612 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001613 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001614 if (!showWorkspace(false)) {
1615 // If we are already on the workspace, then manually reset all apps
1616 mAppsView.reset();
1617 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001618 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1620 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001621 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001622 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1623 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001624 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001625 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1626 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1627 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001628 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001629 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1630 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 }
1632 }
1633 };
1634
1635 @Override
1636 public void onAttachedToWindow() {
1637 super.onAttachedToWindow();
1638
1639 // Listen for broadcasts related to user-presence
1640 final IntentFilter filter = new IntentFilter();
1641 filter.addAction(Intent.ACTION_SCREEN_OFF);
1642 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001643 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001644 if (ENABLE_DEBUG_INTENTS) {
1645 filter.addAction(DebugIntents.DELETE_DATABASE);
1646 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001649 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001650 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 mVisible = true;
Sunny Goyalc86df472016-02-25 09:19:38 -08001652
1653 if (mLauncherCallbacks != null) {
1654 mLauncherCallbacks.onAttachedToWindow();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 @Override
1659 public void onDetachedFromWindow() {
1660 super.onDetachedFromWindow();
1661 mVisible = false;
1662
Adam Cohend113e0c2010-11-11 10:48:05 -08001663 if (mAttached) {
1664 unregisterReceiver(mReceiver);
1665 mAttached = false;
1666 }
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Sunny Goyalc86df472016-02-25 09:19:38 -08001668
1669 if (mLauncherCallbacks != null) {
1670 mLauncherCallbacks.onDetachedFromWindow();
1671 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 }
1673
1674 public void onWindowVisibilityChanged(int visibility) {
1675 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 // The following code used to be in onResume, but it turns out onResume is called when
1678 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1679 // is a more appropriate event to handle
1680 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 if (!mWorkspaceLoading) {
1682 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001683 // We want to let Launcher draw itself at least once before we force it to build
1684 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001685 // apps is nice and speedy.
1686 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001687 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001689 if (mStarted) return;
1690 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001691 // We delay the layer building a bit in order to give
1692 // other message processing a time to run. In particular
1693 // this avoids a delay in hiding the IME if it was
1694 // currently shown, because doing that may involve
1695 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001696 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001697 final ViewTreeObserver.OnDrawListener listener = this;
1698 mWorkspace.post(new Runnable() {
Tony Wickhama0628cc2015-10-14 15:23:04 -07001699 public void run() {
1700 if (mWorkspace != null &&
1701 mWorkspace.getViewTreeObserver() != null) {
1702 mWorkspace.getViewTreeObserver().
1703 removeOnDrawListener(listener);
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 }
Tony Wickhama0628cc2015-10-14 15:23:04 -07001705 }
1706 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001707 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001708 }
1709 });
1710 }
1711 clearTypedText();
1712 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 }
1714
Adam Cohen091440a2015-03-18 14:16:05 -07001715 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 mHandler.removeMessages(ADVANCE_MSG);
1717 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1718 mHandler.sendMessageDelayed(msg, delay);
1719 mAutoAdvanceSentTime = System.currentTimeMillis();
1720 }
1721
Adam Cohen091440a2015-03-18 14:16:05 -07001722 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1724 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1725 mAutoAdvanceRunning = autoAdvanceRunning;
1726 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 sendAdvanceMessage(delay);
1729 } else {
1730 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1733 }
1734 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001735 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737 }
1738 }
1739
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001740 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1741
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (msg.what == ADVANCE_MSG) {
1745 int i = 0;
1746 for (View key: mWidgetsToAdvance.keySet()) {
1747 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001748 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001750 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 public void run() {
1752 ((Advanceable) v).advance();
1753 }
1754 }, delay);
1755 }
1756 i++;
1757 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001758 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001760 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001762 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001763
Winsonc0b52fe2015-09-09 16:38:15 -07001764 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001765 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1767 if (v instanceof Advanceable) {
1768 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001769 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001770 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772 }
1773
Winsonc0b52fe2015-09-09 16:38:15 -07001774 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 if (mWidgetsToAdvance.containsKey(hostView)) {
1776 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001777 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 }
1780
Hyunyoung Song3f471442015-04-08 19:01:34 -07001781 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001782 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1783 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 }
1785
Winson Chunga6945242014-01-08 14:04:34 -08001786 public DragLayer getDragLayer() {
1787 return mDragLayer;
1788 }
1789
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001790 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001791 return mAppsView;
1792 }
1793
Hyunyoung Song3f471442015-04-08 19:01:34 -07001794 public WidgetsContainerView getWidgetsView() {
1795 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001796 }
1797
Winson Chunga6945242014-01-08 14:04:34 -08001798 public Workspace getWorkspace() {
1799 return mWorkspace;
1800 }
1801
1802 public Hotseat getHotseat() {
1803 return mHotseat;
1804 }
1805
Jorim Jaggid017f882014-01-14 17:08:48 -08001806 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001807 return mOverviewPanel;
1808 }
1809
Winson Chung006ee262015-08-03 14:40:11 -07001810 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001811 return mSearchDropTargetBar;
1812 }
1813
Tony Wickham34d2c912015-09-11 09:27:58 -07001814 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1815 return mAppInfoDropTargetBar;
1816 }
1817
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001818 public LauncherAppWidgetHost getAppWidgetHost() {
1819 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 }
Romain Guycbb89e42009-06-08 15:52:54 -07001821
Winson Chunga9abd0e2010-10-27 17:18:37 -07001822 public LauncherModel getModel() {
1823 return mModel;
1824 }
1825
Winson Chunga6945242014-01-08 14:04:34 -08001826 protected SharedPreferences getSharedPrefs() {
1827 return mSharedPrefs;
1828 }
1829
Adam Cohen2e6da152015-05-06 11:42:25 -07001830 public DeviceProfile getDeviceProfile() {
1831 return mDeviceProfile;
1832 }
1833
Bjorn Bringertc459e522013-06-07 19:36:01 +01001834 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001835 getWindow().closeAllPanels();
1836
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001837 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001838 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 }
1840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 @Override
1842 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001843 long startTime = 0;
1844 if (DEBUG_RESUME_TIME) {
1845 startTime = System.currentTimeMillis();
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 super.onNewIntent(intent);
1848
1849 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001850 Folder openFolder = mWorkspace.getOpenFolder();
1851 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1852 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1853 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1854 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1855 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001856 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001857 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001858
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 if (mWorkspace == null) {
1860 // Can be cases where mWorkspace is null, this prevents a NPE
1861 return;
1862 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001863 // In all these cases, only animate if we're already on home
1864 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001865
Sunny Goyal935fca12015-10-13 10:19:01 -07001866 closeFolder(alreadyOnHome);
Adam Cohen6fecd412013-10-02 17:41:50 -07001867 exitSpringLoadedDragMode();
1868
1869 // If we are already on home, then just animate back to the workspace,
1870 // otherwise, just wait until onResume to set the state back to Workspace
1871 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001872 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001873 } else {
1874 mOnResumeState = State.WORKSPACE;
1875 }
1876
1877 final View v = getWindow().peekDecorView();
1878 if (v != null && v.getWindowToken() != null) {
Tonyc17390962015-10-25 17:39:37 -07001879 InputMethodManager imm = (InputMethodManager) getSystemService(
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 INPUT_METHOD_SERVICE);
1881 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1882 }
1883
Winson Chungb745afb2015-03-02 11:51:23 -08001884 // Reset the apps view
1885 if (!alreadyOnHome && mAppsView != null) {
1886 mAppsView.scrollToTop();
1887 }
1888
Hyunyoung Song3f471442015-04-08 19:01:34 -07001889 // Reset the widgets view
1890 if (!alreadyOnHome && mWidgetsView != null) {
1891 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001892 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001893
Adam Cohen9211d422014-10-07 18:14:53 -07001894 if (mLauncherCallbacks != null) {
1895 mLauncherCallbacks.onHomeIntent();
1896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
Adam Cohened307df2013-10-02 09:37:31 -07001898
Adam Cohen9211d422014-10-07 18:14:53 -07001899 if (mLauncherCallbacks != null) {
1900 mLauncherCallbacks.onNewIntent(intent);
1901 }
Winson15f8b172015-08-19 11:02:39 -07001902
1903 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1904 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1905 // animation.
1906 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001907 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1908 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001909 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1910 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001911
1912 // We use this flag to suppress noisy callbacks above custom content state
1913 // from onResume.
1914 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001915 mWorkspace.post(new Runnable() {
1916 @Override
1917 public void run() {
Tonyc17390962015-10-25 17:39:37 -07001918 if (mWorkspace != null) {
1919 mWorkspace.moveToDefaultScreen(true);
1920 }
Winson15f8b172015-08-19 11:02:39 -07001921 }
1922 });
1923 }
1924 }
1925
1926 if (DEBUG_RESUME_TIME) {
1927 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1928 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001932 public void onRestoreInstanceState(Bundle state) {
1933 super.onRestoreInstanceState(state);
1934 for (int page: mSynchronouslyBoundPages) {
1935 mWorkspace.restoreInstanceStateForChild(page);
1936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
1938
1939 @Override
1940 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001941 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001942 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1943 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001944 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001945 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946
Michael Jurka883f55b2010-10-21 15:47:14 -07001947 outState.putInt(RUNTIME_STATE, mState.ordinal());
Sunny Goyal44d0b532016-03-14 14:08:12 -07001948 // We close any open folder since it will not be re-opened, and we need to make sure
1949 // this state is reflected.
1950 // TODO: Move folderInfo.isOpened out of the model and make it a UI state.
1951 closeFolder(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001954 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001955 ContentValues itemValues = new ContentValues();
1956 mPendingAddInfo.writeToValues(itemValues);
1957 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001958 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001959 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Hyunyoung Song3f471442015-04-08 19:01:34 -07001962 // Save the current widgets tray?
1963 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001964
1965 if (mLauncherCallbacks != null) {
1966 mLauncherCallbacks.onSaveInstanceState(outState);
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
1970 @Override
1971 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001973
Winson Chunge7a03942011-08-05 15:05:12 -07001974 // Remove all pending runnables
1975 mHandler.removeMessages(ADVANCE_MSG);
1976 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001977 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001978
Winson Chungcd2b0142011-06-08 16:02:26 -07001979 // Stop callbacks from LauncherModel
Adam Cohen1a85c582014-09-30 09:48:49 -07001980 // It's possible to receive onDestroy after a new Launcher activity has
1981 // been created. In this case, don't interfere with the new Launcher.
1982 if (mModel.isCurrentCallbacks(this)) {
1983 mModel.stopLoader();
Sunny Goyald365ed62016-02-12 09:44:03 -08001984 LauncherAppState.getInstance().setLauncher(null);
Adam Cohen1a85c582014-09-30 09:48:49 -07001985 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001988 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001990 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001992 mAppWidgetHost = null;
1993
1994 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995
1996 TextKeyListener.getInstance().release();
1997
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001998 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001999
Michael Jurka2ecf9952012-06-18 12:52:28 -07002000 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002001
2002 if (mLauncherCallbacks != null) {
2003 mLauncherCallbacks.onDestroy();
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
Adam Cohena9cf38f2011-05-02 15:36:58 -07002007 public DragController getDragController() {
2008 return mDragController;
2009 }
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 @Override
2012 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002013 onStartForResult(requestCode);
2014 super.startActivityForResult(intent, requestCode);
2015 }
2016
2017 @Override
2018 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2019 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2020 onStartForResult(requestCode);
2021 try {
2022 super.startIntentSenderForResult(intent, requestCode,
2023 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2024 } catch (IntentSender.SendIntentException e) {
2025 throw new ActivityNotFoundException();
2026 }
2027 }
2028
2029 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002030 if (requestCode >= 0) {
2031 setWaitingForResult(true);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
Winson Chung70d72102011-08-12 11:24:35 -07002035 /**
2036 * Indicates that we want global search for this activity by setting the globalSearch
2037 * argument for {@link #startSearch} to true.
2038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002040 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002042
Karl Rosaen138a0412009-04-23 19:00:21 -07002043 if (initialQuery == null) {
2044 // Use any text typed in the launcher as the initial query
2045 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 if (appSearchData == null) {
2048 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002049 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
Winson Chungadf0c182012-08-23 14:59:07 -07002051 Rect sourceBounds = new Rect();
2052 if (mSearchDropTargetBar != null) {
2053 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2054 }
Romain Guycbb89e42009-06-08 15:52:54 -07002055
Ian Parkinson047036e2014-09-01 15:40:53 +01002056 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002057 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 if (clearTextImmediately) {
2059 clearTypedText();
2060 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002061
2062 // We need to show the workspace after starting the search
2063 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 }
2065
Ian Parkinson047036e2014-09-01 15:40:53 +01002066 /**
2067 * Start a text search.
2068 *
2069 * @return {@code true} if the search will start immediately, so any further keypresses
2070 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2071 * to buffer keypresses.
2072 */
2073 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002075 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2076 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2077 sourceBounds);
2078 }
2079
Michael Jurkaa33411c2012-06-14 16:18:21 -07002080 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 }
2084
2085 /**
2086 * Starts the global search activity. This code is a copied from SearchManager
2087 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002090 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2092 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2093 if (globalSearchActivity == null) {
2094 Log.w(TAG, "No global search activity found.");
2095 return;
2096 }
2097 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2098 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2099 intent.setComponent(globalSearchActivity);
2100 // Make sure that we have a Bundle to put source in
2101 if (appSearchData == null) {
2102 appSearchData = new Bundle();
2103 } else {
2104 appSearchData = new Bundle(appSearchData);
2105 }
Adam Cohen9211d422014-10-07 18:14:53 -07002106 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 if (!appSearchData.containsKey("source")) {
2108 appSearchData.putString("source", getPackageName());
2109 }
2110 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2111 if (!TextUtils.isEmpty(initialQuery)) {
2112 intent.putExtra(SearchManager.QUERY, initialQuery);
2113 }
2114 if (selectInitialQuery) {
2115 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2116 }
2117 intent.setSourceBounds(sourceBounds);
2118 try {
2119 startActivity(intent);
2120 } catch (ActivityNotFoundException ex) {
2121 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124
Yura4f93ec62014-02-04 14:15:21 +00002125 public boolean isOnCustomContent() {
2126 return mWorkspace.isOnOrMovingToCustomContent();
2127 }
2128
Winson Chung70d72102011-08-12 11:24:35 -07002129 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002130 public boolean onPrepareOptionsMenu(Menu menu) {
2131 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002132 if (mLauncherCallbacks != null) {
2133 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2134 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002135 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002138 @Override
2139 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002140 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002141 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002142 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002143 }
2144
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 public boolean isWorkspaceLocked() {
2146 return mWorkspaceLoading || mWaitingForResult;
2147 }
2148
Adam Cohen517a7f52014-03-01 12:12:59 -08002149 public boolean isWorkspaceLoading() {
2150 return mWorkspaceLoading;
2151 }
2152
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002153 private void setWorkspaceLoading(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWorkspaceLoading = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
2161 private void setWaitingForResult(boolean value) {
2162 boolean isLocked = isWorkspaceLocked();
2163 mWaitingForResult = value;
2164 if (isLocked != isWorkspaceLocked()) {
2165 onWorkspaceLockedChanged();
2166 }
2167 }
2168
Adam Cohen9211d422014-10-07 18:14:53 -07002169 protected void onWorkspaceLockedChanged() {
2170 if (mLauncherCallbacks != null) {
2171 mLauncherCallbacks.onWorkspaceLockedChanged();
2172 }
2173 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002174
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002176 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2179 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002180 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002182
Tony Wickhama0628cc2015-10-14 15:23:04 -07002183 void addAppWidgetFromDropImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002184 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002185 if (LOGD) {
2186 Log.d(TAG, "Adding widget from drop");
2187 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07002188 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2189 }
2190
Sunny Goyale6b63a32015-01-16 16:14:29 -08002191 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002192 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2193 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002194 if (appWidgetInfo.configure != null) {
2195 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002196 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002197
Bjorn Bringert7984c942009-12-09 15:38:25 +00002198 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002199 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2200 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002203 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002204 Runnable onComplete = new Runnable() {
2205 @Override
2206 public void run() {
2207 // Exit spring loaded mode if necessary after adding the widget
2208 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2209 null);
2210 }
2211 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002213 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002214 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 }
2216 }
Romain Guycbb89e42009-06-08 15:52:54 -07002217
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002218 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002219 // Close any folders that may be open.
2220 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002221 mWorkspace.moveToCustomContentScreen(animate);
2222 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002223
2224 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2225 int[] cell, int spanX, int spanY) {
2226 switch (info.itemType) {
2227 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2228 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2229 int span[] = new int[2];
2230 span[0] = spanX;
2231 span[1] = spanY;
2232 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2233 container, screenId, cell, span);
2234 break;
2235 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2236 processShortcutFromDrop(info.componentName, container, screenId, cell);
2237 break;
2238 default:
2239 throw new IllegalStateException("Unknown item type: " + info.itemType);
2240 }
2241 }
2242
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 /**
2244 * Process a shortcut drop.
2245 *
2246 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002250 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2251 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255
2256 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.cellX = cell[0];
2258 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260
2261 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2262 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002263 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 }
2265
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 /**
2267 * Process a widget drop.
2268 *
2269 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002273 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2274 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002277 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002278 mPendingAddInfo.minSpanX = info.minSpanX;
2279 mPendingAddInfo.minSpanY = info.minSpanY;
2280
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.cellX = cell[0];
2283 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002285 if (span != null) {
2286 mPendingAddInfo.spanX = span[0];
2287 mPendingAddInfo.spanY = span[1];
2288 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289
Adam Cohened66b2b2012-01-23 17:28:51 -08002290 AppWidgetHostView hostView = info.boundWidget;
2291 int appWidgetId;
2292 if (hostView != null) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002293 // In the case where we've prebound the widget, we remove it from the DragLayer
2294 if (LOGD) {
2295 Log.d(TAG, "Removing widget view from drag layer and setting boundWidget to null");
2296 }
2297 getDragLayer().removeView(hostView);
2298
Adam Cohened66b2b2012-01-23 17:28:51 -08002299 appWidgetId = hostView.getAppWidgetId();
Tony Wickhama0628cc2015-10-14 15:23:04 -07002300 addAppWidgetFromDropImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002301
2302 // Clear the boundWidget so that it doesn't get destroyed.
2303 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002304 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 // In this case, we either need to start an activity to get permission to bind
2306 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002307 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002308 Bundle options = info.bindOptions;
2309
Sunny Goyalffe83f12014-08-14 17:39:34 -07002310 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2311 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002312 if (success) {
Tony Wickhama0628cc2015-10-14 15:23:04 -07002313 addAppWidgetFromDropImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002314 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002315 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002316 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2317 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2318 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002319 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2320 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002321 // TODO: we need to make sure that this accounts for the options bundle.
2322 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002323 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2324 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002325 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 }
2327
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002330 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 folderInfo.title = getText(R.string.folder_name);
2332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002334 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2335 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002336 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337
2338 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 FolderIcon newFolder =
2340 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002343 // Force measure the new folder icon
2344 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2345 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002346 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 }
Romain Guycbb89e42009-06-08 15:52:54 -07002348
Winsonc0b52fe2015-09-09 16:38:15 -07002349 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002350 * Unbinds the view for the specified item, and removes the item and all its children.
2351 *
2352 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002353 * @param itemInfo the {@link ItemInfo} for this view.
2354 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002355 */
Winson2949fb52015-09-24 09:56:11 -07002356 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002357 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002358 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002359 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2360 if (folderInfo != null) {
2361 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002362 } else {
2363 mWorkspace.removeWorkspaceItem(v);
2364 }
Winsonc0b52fe2015-09-09 16:38:15 -07002365 if (deleteFromDb) {
2366 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2367 }
2368 } else if (itemInfo instanceof FolderInfo) {
2369 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2370 unbindFolder(folderInfo);
2371 mWorkspace.removeWorkspaceItem(v);
2372 if (deleteFromDb) {
2373 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2374 }
2375 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2376 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002377 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002378 removeWidgetToAutoAdvance(widgetInfo.hostView);
2379 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002380 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002381 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002382 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002383
Winsonc0b52fe2015-09-09 16:38:15 -07002384 } else {
2385 return false;
2386 }
2387 return true;
2388 }
2389
2390 /**
2391 * Unbinds any launcher references to the folder.
2392 */
2393 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002394 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 }
2396
Winsonc0b52fe2015-09-09 16:38:15 -07002397 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002398 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002399 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002400 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002401 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002402 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002403 // Deleting an app widget ID is a void call but writes to disk before returning
2404 // to the caller...
2405 new AsyncTask<Void, Void, Void>() {
2406 public Void doInBackground(Void ... args) {
2407 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2408 return null;
2409 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002410 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002411 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002412 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002413 }
2414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 @Override
2416 public boolean dispatchKeyEvent(KeyEvent event) {
2417 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2418 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002420 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002421 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002422 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002423 dumpState();
2424 return true;
2425 }
2426 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 }
2428 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2429 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002430 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 return true;
2432 }
2433 }
2434
2435 return super.dispatchKeyEvent(event);
2436 }
2437
Joe Onorato88ec0992009-11-19 13:16:06 -08002438 @Override
2439 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002440 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2441 return;
2442 }
2443
Sunny Goyal45478022015-06-08 16:52:41 -07002444 if (mDragController.isDragging()) {
2445 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002446 return;
2447 }
2448
Winson Chungb745afb2015-03-02 11:51:23 -08002449 if (isAppsViewVisible()) {
2450 showWorkspace(true);
2451 } else if (isWidgetsViewVisible()) {
2452 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002453 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002454 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002456 Folder openFolder = mWorkspace.getOpenFolder();
2457 if (openFolder.isEditingName()) {
2458 openFolder.dismissEditingName();
2459 } else {
2460 closeFolder();
2461 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002463 mWorkspace.exitWidgetResizeMode();
2464
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 // Back button is a no-op here, but give at least some feedback for the button press
2466 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002467 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002468 }
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002471 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002472 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002473 @Override
2474 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002475 if (mAppWidgetHost != null) {
2476 mAppWidgetHost.startListening();
2477 }
Sunny Goyal655daae2015-12-08 09:28:23 -08002478
2479 // Recreate the QSB, as the widget has been reset.
2480 bindSearchProviderChanged();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002481 }
2482
2483 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 * Launches the intent referred by the clicked shortcut.
2485 *
2486 * @param v The view representing the clicked shortcut.
2487 */
2488 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002489 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2490 // view has detached (it's possible for this to happen if the view is removed mid touch).
2491 if (v.getWindowToken() == null) {
2492 return;
2493 }
2494
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002495 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002496 return;
2497 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002498
Adam Cohen1697b792013-09-17 19:08:21 -07002499 if (v instanceof Workspace) {
2500 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002501 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002502 }
2503 return;
2504 }
2505
2506 if (v instanceof CellLayout) {
2507 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002508 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2509 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002511 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002512 }
2513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002515 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002518 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002519 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002520 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002521 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002523 } else if (tag instanceof AppInfo) {
2524 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002525 } else if (tag instanceof LauncherAppWidgetInfo) {
2526 if (v instanceof PendingAppWidgetHostView) {
2527 onClickPendingWidget((PendingAppWidgetHostView) v);
2528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 }
2530 }
2531
Sunny Goyal70660032015-05-14 00:07:08 -07002532 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002533 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002534 return false;
2535 }
2536
Michael Jurkaaf442092010-06-10 17:01:57 -07002537 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002538 * Event handler for the app widget view which has not fully restored.
2539 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002541 if (mIsSafeModeEnabled) {
2542 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2543 return;
2544 }
2545
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002547 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002548 int widgetId = info.appWidgetId;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002549 LauncherAppWidgetProviderInfo appWidgetInfo =
2550 mAppWidgetManager.getLauncherAppWidgetInfo(widgetId);
Sunny Goyalff572272014-07-23 13:58:07 -07002551 if (appWidgetInfo != null) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08002552 mPendingAddWidgetInfo = appWidgetInfo;
Sunny Goyalff572272014-07-23 13:58:07 -07002553 mPendingAddInfo.copyFrom(info);
2554 mPendingAddWidgetId = widgetId;
2555
Sunny Goyalffe83f12014-08-14 17:39:34 -07002556 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2557 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002559 } else if (info.installProgress < 0) {
2560 // The install has not been queued
2561 final String packageName = info.providerName.getPackageName();
2562 showBrokenAppInstallDialog(packageName,
2563 new DialogInterface.OnClickListener() {
2564 public void onClick(DialogInterface dialog, int id) {
2565 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2566 }
2567 });
2568 } else {
2569 // Download has started.
2570 final String packageName = info.providerName.getPackageName();
2571 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002572 }
2573 }
2574
2575 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002576 * Event handler for the "grid" button that appears on the home screen, which
2577 * enters all apps mode.
2578 *
2579 * @param v The view that was clicked.
2580 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002581 protected void onClickAllAppsButton(View v) {
2582 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002583 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002584 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002585 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002586
2587 if (mLauncherCallbacks != null) {
2588 mLauncherCallbacks.onClickAllAppsButton(v);
2589 }
Winson Chung76648c52015-07-10 14:33:23 -07002590 }
2591 }
2592
2593 protected void onLongClickAllAppsButton(View v) {
2594 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2595 if (!isAppsViewVisible()) {
2596 showAppsView(true /* animated */, false /* resetListToTop */,
2597 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 }
2599 }
2600
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 private void showBrokenAppInstallDialog(final String packageName,
2602 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002603 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 .setTitle(R.string.abandoned_promises_title)
2605 .setMessage(R.string.abandoned_promise_explanation)
2606 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2607 .setNeutralButton(R.string.abandoned_clean_this,
2608 new DialogInterface.OnClickListener() {
2609 public void onClick(DialogInterface dialog, int id) {
2610 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2611 mWorkspace.removeAbandonedPromise(packageName, user);
2612 }
2613 })
2614 .create().show();
2615 return;
2616 }
2617
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 /**
2619 * Event handler for an app shortcut click.
2620 *
2621 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2622 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2625 Object tag = v.getTag();
2626 if (!(tag instanceof ShortcutInfo)) {
2627 throw new IllegalArgumentException("Input must be a Shortcut");
2628 }
2629
2630 // Open shortcut
2631 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632
2633 if (shortcut.isDisabled != 0) {
Kenny Guyff05f432016-01-22 17:48:29 +00002634 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SUSPENDED) != 0
2635 || (shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_QUIET_USER) != 0) {
Kenny Guy44cba692016-01-21 19:50:02 +00002636 // Launch activity anyway, framework will tell the user why the app is suspended.
2637 } else {
2638 int error = R.string.activity_not_available;
2639 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2640 error = R.string.safemode_shortcut_error;
2641 }
2642 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2643 return;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002644 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002645 }
2646
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002648 if ((v instanceof BubbleTextView)
2649 && shortcut.isPromise()
2650 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002651 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002652 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002653 new DialogInterface.OnClickListener() {
2654 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002655 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 return;
2659 }
2660
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002663
2664 if (mLauncherCallbacks != null) {
2665 mLauncherCallbacks.onClickAppShortcut(v);
2666 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 }
2668
Adam Cohen091440a2015-03-18 14:16:05 -07002669 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002671 final ShortcutInfo shortcut;
2672 final Intent intent;
2673 if (tag instanceof ShortcutInfo) {
2674 shortcut = (ShortcutInfo) tag;
2675 intent = shortcut.intent;
2676 int[] pos = new int[2];
2677 v.getLocationOnScreen(pos);
2678 intent.setSourceBounds(new Rect(pos[0], pos[1],
2679 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002680
Sunny Goyal508da152014-08-14 10:53:27 -07002681 } else if (tag instanceof AppInfo) {
2682 shortcut = null;
2683 intent = ((AppInfo) tag).intent;
2684 } else {
2685 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2686 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002687
2688 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002689 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690
2691 if (success && v instanceof BubbleTextView) {
2692 mWaitingForResume = (BubbleTextView) v;
2693 mWaitingForResume.setStayPressed(true);
2694 }
2695 }
2696
2697 /**
2698 * Event handler for a folder icon click.
2699 *
2700 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2701 */
2702 protected void onClickFolderIcon(View v) {
2703 if (LOGD) Log.d(TAG, "onClickFolder");
2704 if (!(v instanceof FolderIcon)){
2705 throw new IllegalArgumentException("Input must be a FolderIcon");
2706 }
2707
2708 FolderIcon folderIcon = (FolderIcon) v;
Sunny Goyal08442b82015-10-21 17:15:08 -07002709 if (!folderIcon.getFolderInfo().opened && !folderIcon.getFolder().isDestroyed()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002710 // Open the requested folder
Sunny Goyal08442b82015-10-21 17:15:08 -07002711 openFolder(folderIcon, true);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 }
Adam Cohen9211d422014-10-07 18:14:53 -07002713
2714 if (mLauncherCallbacks != null) {
2715 mLauncherCallbacks.onClickFolderIcon(v);
2716 }
Michael Jurka5130e402011-10-13 04:55:35 -07002717 }
2718
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002719 /**
2720 * Event handler for the (Add) Widgets button that appears after a long press
2721 * on the home screen.
2722 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002723 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002725 if (mIsSafeModeEnabled) {
2726 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2727 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002728 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002729 if (mLauncherCallbacks != null) {
2730 mLauncherCallbacks.onClickAddWidgetButton(view);
2731 }
Adam Cohen9211d422014-10-07 18:14:53 -07002732 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002733 }
2734
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 /**
2736 * Event handler for the wallpaper picker button that appears after a long press
2737 * on the home screen.
2738 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002739 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002741 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2742 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2743 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
Sunny Goyal6f866092016-03-17 17:04:15 -07002744 .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002745 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002746
2747 if (mLauncherCallbacks != null) {
2748 mLauncherCallbacks.onClickWallpaperPicker(v);
2749 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002750 }
2751
2752 /**
2753 * Event handler for a click on the settings button that appears after a long press
2754 * on the home screen.
2755 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002756 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002760 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002761 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002762 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002763 }
2764
Adam Cohen61f560d2013-09-30 15:58:20 -07002765 public View.OnTouchListener getHapticFeedbackTouchListener() {
2766 if (mHapticFeedbackTouchListener == null) {
2767 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002768 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002769 @Override
2770 public boolean onTouch(View v, MotionEvent event) {
2771 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2772 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2773 }
2774 return false;
2775 }
2776 };
2777 }
2778 return mHapticFeedbackTouchListener;
2779 }
2780
Adam Cohen9211d422014-10-07 18:14:53 -07002781 public void onDragStarted(View view) {
2782 if (isOnCustomContent()) {
2783 // Custom content screen doesn't participate in drag and drop. If on custom
2784 // content screen, move to default.
2785 moveWorkspaceToDefaultScreen();
2786 }
2787
2788 if (mLauncherCallbacks != null) {
2789 mLauncherCallbacks.onDragStarted(view);
2790 }
2791 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002792
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002793 /**
2794 * Called when the user stops interacting with the launcher.
2795 * This implies that the user is now on the homescreen and is not doing housekeeping.
2796 */
Adam Cohen9211d422014-10-07 18:14:53 -07002797 protected void onInteractionEnd() {
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onInteractionEnd();
2800 }
2801 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802
2803 /**
2804 * Called when the user starts interacting with the launcher.
2805 * The possible interactions are:
2806 * - open all apps
2807 * - reorder an app shortcut, or a widget
2808 * - open the overview mode.
2809 * This is a good time to stop doing things that only make sense
2810 * when the user is on the homescreen and not doing housekeeping.
2811 */
Adam Cohen9211d422014-10-07 18:14:53 -07002812 protected void onInteractionBegin() {
2813 if (mLauncherCallbacks != null) {
2814 mLauncherCallbacks.onInteractionBegin();
2815 }
2816 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002817
Winson Chungcd99cd32015-04-29 11:03:24 -07002818 /** Updates the interaction state. */
2819 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002820 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002821 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002822 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2823 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002824 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002825 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002826 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002827 onInteractionEnd();
2828 }
2829 }
2830
Kenny Guyf07af7b2014-07-31 11:39:16 +01002831 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002832 try {
2833 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002834 launcherApps.showAppDetailsForProfile(componentName, user);
2835 } catch (SecurityException e) {
2836 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2837 Log.e(TAG, "Launcher does not have permission to launch settings");
2838 } catch (ActivityNotFoundException e) {
2839 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2840 Log.e(TAG, "Unable to launch settings");
2841 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002842 }
2843
Michael Jurka1e2f4652013-07-08 18:03:46 -07002844 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002845 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2846 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002847 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002848 // System applications cannot be installed. For now, show a toast explaining that.
2849 // We may give them the option of disabling apps this way.
2850 int messageId = R.string.uninstall_system_app_text;
2851 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002852 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002853 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002854 String packageName = componentName.getPackageName();
2855 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002856 Intent intent = new Intent(
2857 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002858 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2859 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002860 if (user != null) {
2861 user.addToIntent(intent, Intent.EXTRA_USER);
2862 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002863 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002864 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002865 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002866 }
2867
Winson Chung8f1eff72015-05-28 17:33:40 -07002868 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002869 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002871 // Only launch using the new animation if the shortcut has not opted out (this is a
2872 // private contract between launcher and may be ignored in the future).
2873 boolean useLaunchAnimation = (v != null) &&
2874 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002875 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2876 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002877
Kenny Guy1317e2d2014-05-08 18:52:50 +01002878 UserHandleCompat user = null;
2879 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2880 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2881 user = userManager.getUserForSerialNumber(serialNumber);
2882 }
2883
2884 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002885 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002886 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002887 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002888 int left = 0, top = 0;
2889 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2890 if (v instanceof TextView) {
2891 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002892 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2893 if (icon != null) {
2894 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002895 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002896 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002897 width = bounds.width();
2898 height = bounds.height();
2899 }
2900 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002901 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2902 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002903 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002904 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002905 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002906 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002907 // On L devices, we use the device default slide-up transition.
2908 // On L MR1 devices, we a custom version of the slide-up transition which
2909 // doesn't have the delay present in the device default.
2910 opts = ActivityOptions.makeCustomAnimation(this,
2911 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002912 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002913 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002914 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002915
2916 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2917 // Could be launching some bookkeeping activity
2918 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002919 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002920 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002921 launcherApps.startActivityForProfile(intent.getComponent(), user,
2922 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002923 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002924 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002926 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2927 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2928 // corresponding permission. Show the appropriate permission prompt if that
2929 // is the case.
2930 if (intent.getComponent() == null
2931 && Intent.ACTION_CALL.equals(intent.getAction())
2932 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2933 PackageManager.PERMISSION_GRANTED) {
2934 // TODO: Rename sPendingAddItem to a generic name.
2935 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2936 0, (ItemInfo) tag);
2937 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2938 REQUEST_PERMISSION_CALL_PHONE);
2939 return false;
2940 }
2941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002943 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002945 "or use the exported attribute for this activity. "
2946 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002948 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002950
Winson Chungbedf9232015-07-10 12:38:30 -07002951 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002952 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002953 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2954 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2955 return false;
2956 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002957 try {
2958 success = startActivity(v, intent, tag);
2959 } catch (ActivityNotFoundException e) {
2960 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2961 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2962 }
2963 return success;
2964 }
2965
Adam Cohen268c4752012-06-06 17:47:33 -07002966 /**
2967 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2968 * in the DragLayer in the exact absolute location of the original FolderIcon.
2969 */
2970 private void copyFolderIconToImage(FolderIcon fi) {
2971 final int width = fi.getMeasuredWidth();
2972 final int height = fi.getMeasuredHeight();
2973
2974 // Lazy load ImageView, Bitmap and Canvas
2975 if (mFolderIconImageView == null) {
2976 mFolderIconImageView = new ImageView(this);
2977 }
2978 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2979 mFolderIconBitmap.getHeight() != height) {
2980 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2981 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2982 }
2983
2984 DragLayer.LayoutParams lp;
2985 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2986 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2987 } else {
2988 lp = new DragLayer.LayoutParams(width, height);
2989 }
2990
Adam Cohen307fe232012-08-16 17:55:58 -07002991 // The layout from which the folder is being opened may be scaled, adjust the starting
2992 // view size by this scale factor.
2993 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002994 lp.customPosition = true;
2995 lp.x = mRectForFolderAnimation.left;
2996 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002997 lp.width = (int) (scale * width);
2998 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002999
3000 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3001 fi.draw(mFolderIconCanvas);
3002 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003003 if (fi.getFolder() != null) {
3004 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3005 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003006 }
Adam Cohen268c4752012-06-06 17:47:33 -07003007 // Just in case this image view is still in the drag layer from a previous animation,
3008 // we remove it and re-add it.
3009 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3010 mDragLayer.removeView(mFolderIconImageView);
3011 }
3012 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003013 if (fi.getFolder() != null) {
3014 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003015 }
Adam Cohen268c4752012-06-06 17:47:33 -07003016 }
3017
Sunny Goyal08442b82015-10-21 17:15:08 -07003018 private void growAndFadeOutFolderIcon(FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003019 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003020 FolderInfo info = (FolderInfo) fi.getTag();
3021 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3022 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003023 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3024 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003025 }
3026
Adam Cohen268c4752012-06-06 17:47:33 -07003027 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3028 copyFolderIconToImage(fi);
3029 fi.setVisibility(View.INVISIBLE);
3030
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003031 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3032 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003033 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003034 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3035 }
3036 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 oa.start();
Sunny Goyal08442b82015-10-21 17:15:08 -07003038 if (!animate) {
Tony Wickham112ac952015-11-12 12:31:50 -08003039 oa.end();
3040 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 }
3042
Sunny Goyal935fca12015-10-13 10:19:01 -07003043 private void shrinkAndFadeInFolderIcon(final FolderIcon fi, boolean animate) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003044 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003045 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003046
Adam Cohen268c4752012-06-06 17:47:33 -07003047 // We remove and re-draw the FolderIcon in-case it has changed
3048 mDragLayer.removeView(mFolderIconImageView);
3049 copyFolderIconToImage(fi);
Adam Cohenefca0272016-02-24 19:19:06 -08003050
3051 if (cl != null) {
3052 cl.clearFolderLeaveBehind();
3053 }
3054
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003055 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003056 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003057 oa.addListener(new AnimatorListenerAdapter() {
3058 @Override
3059 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003060 if (cl != null) {
Adam Cohen268c4752012-06-06 17:47:33 -07003061 // Remove the ImageView copy of the FolderIcon and make the original visible.
3062 mDragLayer.removeView(mFolderIconImageView);
3063 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003064 }
3065 }
3066 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003067 oa.start();
Sunny Goyal935fca12015-10-13 10:19:01 -07003068 if (!animate) {
3069 oa.end();
3070 }
Adam Cohen2801caf2011-05-13 20:57:39 -07003071 }
3072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003074 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075 * is animated relative to the specified View. If the View is null, no animation
3076 * is played.
3077 *
Sunny Goyal08442b82015-10-21 17:15:08 -07003078 * @param folderIcon The FolderIcon describing the folder to open.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079 */
Sunny Goyal08442b82015-10-21 17:15:08 -07003080 public void openFolder(FolderIcon folderIcon, boolean animate) {
3081 animate &= !Utilities.isPowerSaverOn(this);
3082
Adam Cohenfb91f302012-06-11 15:45:18 -07003083 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003084 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3085 if (openFolder != null && openFolder != folder) {
3086 // Close any open folder before opening a folder.
3087 closeFolder();
3088 }
3089
Adam Cohena9cf38f2011-05-02 15:36:58 -07003090 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091
Adam Cohena9cf38f2011-05-02 15:36:58 -07003092 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093
Sunny Goyalf4066152015-04-15 09:42:19 -07003094 // While the folder is open, the position of the icon cannot change.
3095 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3096
Adam Cohen4554ee12011-08-03 16:13:21 -07003097 // Just verify that the folder hasn't already been added to the DragLayer.
3098 // There was a one-off crash where the folder had a parent already.
3099 if (folder.getParent() == null) {
3100 mDragLayer.addView(folder);
Sunny Goyal08442b82015-10-21 17:15:08 -07003101 mDragController.addDropTarget(folder);
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 } else {
3103 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3104 folder.getParent() + ").");
3105 }
Sunny Goyal08442b82015-10-21 17:15:08 -07003106 if (animate) {
3107 folder.animateOpen();
3108 } else {
3109 folder.open();
3110 }
3111 growAndFadeOutFolderIcon(folderIcon, animate);
Winson Chung83ca4802013-04-12 15:10:52 -07003112
3113 // Notify the accessibility manager that this folder "window" has appeared and occluded
3114 // the workspace items
3115 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3116 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 }
3118
3119 public void closeFolder() {
Sunny Goyal935fca12015-10-13 10:19:01 -07003120 closeFolder(true);
3121 }
3122
3123 public void closeFolder(boolean animate) {
Adam Cohen37c717f2013-11-26 11:50:15 -08003124 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003126 if (folder.isEditingName()) {
3127 folder.dismissEditingName();
3128 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003129 closeFolder(folder, animate);
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 }
3131 }
3132
Sunny Goyal935fca12015-10-13 10:19:01 -07003133 public void closeFolder(Folder folder, boolean animate) {
Sunny Goyal08442b82015-10-21 17:15:08 -07003134 animate &= !Utilities.isPowerSaverOn(this);
3135
Adam Cohen4554ee12011-08-03 16:13:21 -07003136 folder.getInfo().opened = false;
3137
3138 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3139 if (parent != null) {
3140 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Sunny Goyal935fca12015-10-13 10:19:01 -07003141 shrinkAndFadeInFolderIcon(fi, animate);
Sunny Goyalf4066152015-04-15 09:42:19 -07003142 if (fi != null) {
3143 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3144 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003145 }
Sunny Goyal935fca12015-10-13 10:19:01 -07003146 if (animate) {
3147 folder.animateClosed();
3148 } else {
Tony Wickham30bdd6e2015-11-17 17:59:26 -08003149 folder.close(false);
Sunny Goyal935fca12015-10-13 10:19:01 -07003150 }
Winson Chung83ca4802013-04-12 15:10:52 -07003151
Sunny Goyal935fca12015-10-13 10:19:01 -07003152 // Notify the accessibility manager that this folder "window" has disappeared and no
3153 // longer occludes the workspace items
Winson Chung83ca4802013-04-12 15:10:52 -07003154 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 }
3156
Tony Wickhamdadb3042016-02-24 11:07:00 -08003157 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003159 if (!isDraggingEnabled()) return false;
3160 if (isWorkspaceLocked()) return false;
3161 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162
Winson Chung76648c52015-07-10 14:33:23 -07003163 if (v == mAllAppsButton) {
3164 onLongClickAllAppsButton(v);
3165 return true;
3166 }
3167
Adam Cohen1697b792013-09-17 19:08:21 -07003168 if (v instanceof Workspace) {
3169 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003170 if (!mWorkspace.isTouchActive()) {
3171 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003172 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3173 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3174 return true;
3175 } else {
3176 return false;
3177 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003178 } else {
3179 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003180 }
Adam Cohen1697b792013-09-17 19:08:21 -07003181 }
3182
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003183 CellLayout.CellInfo longClickCellInfo = null;
3184 View itemUnderLongClick = null;
3185 if (v.getTag() instanceof ItemInfo) {
3186 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003187 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003188 itemUnderLongClick = longClickCellInfo.cell;
3189 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 }
3191
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 // The hotseat touch handling does not go through Workspace, and we always allow long press
3193 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003194 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003195 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003196 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003197 // User long pressed on empty space
3198 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3199 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003200 if (mWorkspace.isInOverviewMode()) {
3201 mWorkspace.startReordering(v);
3202 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003203 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003206 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3207 mHotseat.getOrderInHotseat(
3208 longClickCellInfo.cellX,
3209 longClickCellInfo.cellY));
3210 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003212 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 }
3214 }
3215 }
3216 return true;
3217 }
3218
Winson Chung3d503fb2011-07-13 17:25:49 -07003219 boolean isHotseatLayout(View layout) {
3220 return mHotseat != null && layout != null &&
3221 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3222 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003223
Winson Chung3d503fb2011-07-13 17:25:49 -07003224 /**
3225 * Returns the CellLayout of the specified container at the specified screen.
3226 */
Sunny Goyal92820592015-03-02 11:31:35 -08003227 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003228 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3229 if (mHotseat != null) {
3230 return mHotseat.getLayout();
3231 } else {
3232 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003233 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003234 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003235 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003236 }
3237 }
3238
Winson Chungb745afb2015-03-02 11:51:23 -08003239 /**
3240 * For overridden classes.
3241 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003242 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003243 return isAppsViewVisible();
3244 }
3245
3246 public boolean isAppsViewVisible() {
3247 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3248 }
3249
3250 public boolean isWidgetsViewVisible() {
3251 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003252 }
3253
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003254 private void setWorkspaceBackground(int background) {
3255 switch (background) {
3256 case WORKSPACE_BACKGROUND_TRANSPARENT:
3257 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3258 break;
3259 case WORKSPACE_BACKGROUND_BLACK:
3260 getWindow().setBackgroundDrawable(null);
3261 break;
3262 default:
3263 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3264 }
Craig Mautner360310b2012-10-26 15:13:08 -07003265 }
3266
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003267 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003268 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3269 int curflags = getWindow().getAttributes().flags
3270 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3271 if (wpflags != curflags) {
3272 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3273 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003274 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003275 }
3276
Michael Jurkae326f182011-11-21 14:05:46 -08003277 @Override
3278 public void onTrimMemory(int level) {
3279 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003280 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3281 // The widget preview db can result in holding onto over
3282 // 3MB of memory for caching which isn't necessary.
3283 SQLiteDatabase.releaseMemory();
3284
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003285 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003286 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003287 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003288 if (mLauncherCallbacks != null) {
3289 mLauncherCallbacks.onTrimMemory(level);
3290 }
Michael Jurkae326f182011-11-21 14:05:46 -08003291 }
3292
Winson5c6bdbb2015-09-03 11:36:19 -07003293 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003294 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003295 }
3296
Winson5c6bdbb2015-09-03 11:36:19 -07003297 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003298 boolean changed = mState != State.WORKSPACE ||
3299 mWorkspace.getState() != Workspace.State.NORMAL;
3300 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003302 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003303 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003304
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 // Set focus to the AppsCustomize button
3306 if (mAllAppsButton != null) {
3307 mAllAppsButton.requestFocus();
3308 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003309 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003310
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003311 // Change the state *after* we've called all the transition code
3312 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003313
Winson Chung5fb63472011-02-02 17:03:37 -08003314 // Resume the auto-advance of widgets
3315 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003316 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003317
Winson Chung0f785722015-04-08 10:27:49 -07003318 if (changed) {
3319 // Send an accessibility event to announce the context change
3320 getWindow().getDecorView()
3321 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003322 }
Winson5c6bdbb2015-09-03 11:36:19 -07003323 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003324 }
3325
Winsone9f27272015-10-13 10:47:51 -07003326 /**
3327 * Shows the overview button.
3328 */
Adam Cohened307df2013-10-02 09:37:31 -07003329 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003330 showOverviewMode(animated, false);
3331 }
3332
3333 /**
3334 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3335 * onto one of the overview panel buttons.
3336 */
3337 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3338 Runnable postAnimRunnable = null;
3339 if (requestButtonFocus) {
3340 postAnimRunnable = new Runnable() {
3341 @Override
3342 public void run() {
3343 // Hitting the menu button when in touch mode does not trigger touch mode to
3344 // be disabled, so if requested, force focus on one of the overview panel
3345 // buttons.
3346 mOverviewPanel.requestFocusFromTouch();
3347 }
3348 };
3349 }
Adam Cohened307df2013-10-02 09:37:31 -07003350 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003351 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson6e6a9b42015-10-13 17:52:17 -07003352 Workspace.State.OVERVIEW, animated, postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003353 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003354 }
3355
Winson Chungb745afb2015-03-02 11:51:23 -08003356 /**
3357 * Shows the apps view.
3358 */
Winson Chung76648c52015-07-10 14:33:23 -07003359 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3360 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003361 if (resetListToTop) {
3362 mAppsView.scrollToTop();
3363 }
Winson Chung4ac30062015-05-08 17:34:17 -07003364 if (updatePredictedApps) {
3365 tryAndUpdatePredictedApps();
3366 }
Winson Chung76648c52015-07-10 14:33:23 -07003367 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the widgets view.
3372 */
3373 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003374 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003375 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003377 }
Winson Chung76648c52015-07-10 14:33:23 -07003378 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003379
3380 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003381 @Override
3382 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003384 }
3385 });
Winson Chungb745afb2015-03-02 11:51:23 -08003386 }
3387
3388 /**
3389 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003390 *
3391 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003392 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003393 // TODO: calling method should use the return value so that when {@code false} is returned
3394 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003395 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003396 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3397 mState != State.WIDGETS_SPRING_LOADED) {
3398 return false;
3399 }
3400 if (toState != State.APPS && toState != State.WIDGETS) {
3401 return false;
3402 }
Winson Chungb745afb2015-03-02 11:51:23 -08003403
3404 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003405 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3406 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003407 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003408 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003409 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003412 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
3414 // Pause the auto-advance of widgets until we are out of AllApps
3415 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003416 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417 closeFolder();
3418
3419 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003420 getWindow().getDecorView()
3421 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003422 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423 }
3424
Winson Chungcd99cd32015-04-29 11:03:24 -07003425 /**
3426 * Updates the workspace and interaction state on state change, and return the animation to this
3427 * new state.
3428 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003429 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003430 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003431 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003432 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003433 updateInteraction(fromState, toState);
3434 return anim;
3435 }
3436
Jun Mukaif0033da2015-08-04 18:34:30 -07003437 public void onLauncherClingShown() {
3438 // When a launcher cling appears, it should cover the underlying layers, so their focus
3439 // should be blocked.
3440 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3441 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3442 }
3443 }
3444
3445 public void onLauncherClingDismissed() {
3446 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3447 }
3448
Hyunyoung Song3f471442015-04-08 19:01:34 -07003449 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003450 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003451 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003452 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003453 }
Winson Chungb745afb2015-03-02 11:51:23 -08003454
Winson Chung006ee262015-08-03 14:40:11 -07003455 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003456 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003457 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003458
3459 if (isAppsViewVisible()) {
3460 mState = State.APPS_SPRING_LOADED;
3461 } else if (isWidgetsViewVisible()) {
3462 mState = State.WIDGETS_SPRING_LOADED;
Tony Wickhame0c33232016-02-08 11:37:04 -08003463 } else if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003464 mState = State.WORKSPACE_SPRING_LOADED;
Tony Wickham0f97b782015-12-02 17:55:07 -08003465 } else {
3466 mState = State.WORKSPACE;
Tony Wickham94e0d372015-09-11 12:17:48 -07003467 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003468 }
Adam Cohen7777d962011-08-18 18:58:38 -07003469
Hyunyoung Song3f471442015-04-08 19:01:34 -07003470 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003471 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003472 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003473
Winson Chunge7a03942011-08-05 15:05:12 -07003474 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003475 @Override
3476 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003477 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003478 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3479 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003480 // Before we show workspace, hide all apps again because
3481 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3482 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003483 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003484 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003485 } else {
3486 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003487 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003488 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003489 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003490 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003491
Tony Wickhame0c33232016-02-08 11:37:04 -08003492 boolean isStateSpringLoaded() {
Tony Wickham94e0d372015-09-11 12:17:48 -07003493 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3494 || mState == State.WIDGETS_SPRING_LOADED;
3495 }
3496
Michael Jurkad3ef3062010-11-23 16:23:58 -08003497 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003499 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003500 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003501 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003502 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003503 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3504 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003505 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003506 }
3507
Winson Chung4ac30062015-05-08 17:34:17 -07003508 /**
3509 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3510 * resumed.
3511 */
3512 private void tryAndUpdatePredictedApps() {
3513 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003514 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003515 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003516 mAppsView.setPredictedApps(apps);
3517 }
3518 }
3519 }
3520
Michael Jurkab3e22d92011-10-31 15:58:33 -07003521 void lockAllApps() {
3522 // TODO
3523 }
3524
3525 void unlockAllApps() {
3526 // TODO
3527 }
3528
Winsonf768d932015-09-25 16:12:35 -07003529 public boolean launcherCallbacksProvidesSearch() {
3530 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3531 }
3532
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003533 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003534 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003535 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003536 }
3537
Adam Cohen24ce0b32014-01-14 16:18:14 -08003538 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003539 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3540 if (searchProvider == null) {
3541 return null;
3542 }
3543
3544 Bundle opts = new Bundle();
3545 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003546 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003547
Tony Wickham3a3517f2015-10-06 11:27:04 -07003548 // Determine the min and max dimensions of the widget.
3549 LauncherAppState app = LauncherAppState.getInstance();
3550 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3551 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3552 float density = getResources().getDisplayMetrics().density;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003553 Point searchDimens = portraitProfile.getSearchBarDimensForWidgetOpts(getResources());
3554 int maxHeight = (int) (searchDimens.y / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003555 int minHeight = maxHeight;
Tony Wickhama3c74d12015-10-23 11:43:47 -07003556 int maxWidth = (int) (searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003557 int minWidth = maxWidth;
3558 if (!landscapeProfile.isVerticalBarLayout()) {
Tony Wickhama3c74d12015-10-23 11:43:47 -07003559 searchDimens = landscapeProfile.getSearchBarDimensForWidgetOpts(getResources());
3560 maxHeight = (int) Math.max(maxHeight, searchDimens.y / density);
3561 minHeight = (int) Math.min(minHeight, searchDimens.y / density);
3562 maxWidth = (int) Math.max(maxWidth, searchDimens.x / density);
3563 minWidth = (int) Math.min(minWidth, searchDimens.x / density);
Tony Wickham3a3517f2015-10-06 11:27:04 -07003564 }
3565 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3566 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3567 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3568 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
Tony Wickhameef44322015-10-20 13:24:36 -07003569 if (LOGD) {
3570 Log.d(TAG, "QSB widget options: maxHeight=" + maxHeight + " minHeight=" + minHeight
3571 + " maxWidth=" + maxWidth + " minWidth=" + minWidth);
3572 }
Tony Wickham3a3517f2015-10-06 11:27:04 -07003573
Tony Wickham775455c2015-10-16 09:49:32 -07003574 if (mLauncherCallbacks != null) {
3575 opts.putAll(mLauncherCallbacks.getAdditionalSearchWidgetOptions());
3576 }
3577
Sunny Goyalf7258242015-10-19 16:59:07 -07003578 int widgetId = mSharedPrefs.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003579 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003580 if (!searchProvider.provider.flattenToString().equals(
Sunny Goyalf7258242015-10-19 16:59:07 -07003581 mSharedPrefs.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003582 || (widgetInfo == null)
3583 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003584 // A valid widget is not already bound.
3585 if (widgetId > -1) {
3586 mAppWidgetHost.deleteAppWidgetId(widgetId);
3587 widgetId = -1;
3588 }
3589
3590 // Try to bind a new widget
3591 widgetId = mAppWidgetHost.allocateAppWidgetId();
3592
3593 if (!AppWidgetManagerCompat.getInstance(this)
3594 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3595 mAppWidgetHost.deleteAppWidgetId(widgetId);
3596 widgetId = -1;
3597 }
3598
Sunny Goyalf7258242015-10-19 16:59:07 -07003599 mSharedPrefs.edit()
Sunny Goyal594d76d2014-11-06 10:12:54 -08003600 .putInt(QSB_WIDGET_ID, widgetId)
3601 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
Sunny Goyald8043982015-12-17 22:23:02 -08003602 .apply();
Sunny Goyal594d76d2014-11-06 10:12:54 -08003603 }
3604
Sunny Goyal8d595092015-07-06 12:22:14 -07003605 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003606 if (widgetId != -1) {
3607 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003608 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003609 mQsb.updateAppWidgetOptions(opts);
3610 mQsb.setPadding(0, 0, 0, 0);
3611 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003612 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003613 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003614 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003615 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003616 }
3617
Sunny Goyal22235bc2015-04-03 09:23:43 -07003618 private void reinflateQSBIfNecessary() {
3619 if (mQsb instanceof LauncherAppWidgetHostView &&
3620 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3621 mSearchDropTargetBar.removeView(mQsb);
3622 mQsb = null;
3623 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3624 }
3625 }
3626
Michael Jurkad7c28052012-04-27 15:43:36 -07003627 @Override
3628 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003629 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003630 final List<CharSequence> text = event.getText();
3631 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003632 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003633 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003634 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003635 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003636 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003637 } else if (mWorkspace != null) {
3638 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003639 } else {
3640 text.add(getString(R.string.all_apps_home_button_label));
3641 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003642 return result;
3643 }
3644
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003645 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003646 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003647 */
Adam Cohen091440a2015-03-18 14:16:05 -07003648 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003649 @Override
3650 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003651 closeSystemDialogs();
3652 }
3653 }
3654
3655 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 * If the activity is currently paused, signal that we need to run the passed Runnable
3657 * in onResume.
3658 *
3659 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003660 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3661 * wrong when we're not running, and if the activity comes back to what the configuration was
3662 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003663 *
3664 * Implementation of the method from LauncherModel.Callbacks.
3665 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003666 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003667 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003668 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003670 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003671 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003672 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003673 }
3674 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003675 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 return true;
3677 } else {
3678 return false;
3679 }
3680 }
3681
Michael Jurkac402cd92013-05-20 15:49:32 +02003682 private boolean waitUntilResume(Runnable run) {
3683 return waitUntilResume(run, false);
3684 }
3685
Michael Jurka1e2f4652013-07-08 18:03:46 -07003686 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003687 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003688 }
3689
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003691 * If the activity is currently paused, signal that we need to re-run the loader
3692 * in onResume.
3693 *
3694 * This needs to be called from incoming places where resources might have been loaded
3695 * while we are paused. That is becaues the Configuration might be wrong
3696 * when we're not running, and if it comes back to what it was when we
3697 * were paused, we are not restarted.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
3700 *
3701 * @return true if we are currently paused. The caller might be able to
3702 * skip some work in that case since we will come back again.
3703 */
3704 public boolean setLoadOnResume() {
3705 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003706 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003707 mOnResumeNeedsLoad = true;
3708 return true;
3709 } else {
3710 return false;
3711 }
3712 }
3713
3714 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003716 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003718 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003719 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003720 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003721 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003724
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003726 * Clear any pending bind callbacks. This is called when is loader is planning to
3727 * perform a full rebind from scratch.
3728 */
3729 @Override
3730 public void clearPendingBinds() {
3731 mBindOnResumeCallbacks.clear();
3732 if (mPendingExecutor != null) {
3733 mPendingExecutor.markCompleted();
3734 mPendingExecutor = null;
3735 }
3736 }
3737
3738 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 * Refreshes the shortcuts shown on the workspace.
3740 *
3741 * Implementation of the method from LauncherModel.Callbacks.
3742 */
3743 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003744 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003745
Winson Chungd64d1762013-08-20 14:37:16 -07003746 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003747 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003748 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003749
Michael Jurka05bf644e2011-11-30 20:28:53 -08003750 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003751 if (mHotseat != null) {
3752 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003753 }
3754 }
3755
Adam Cohendcd297f2013-06-18 13:13:40 -07003756 @Override
3757 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003758 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003759
Adam Cohen5084cba2013-09-03 12:01:16 -07003760 // If there are no screens, we need to have an empty screen
3761 if (orderedScreenIds.size() == 0) {
3762 mWorkspace.addExtraEmptyScreen();
3763 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003764
3765 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003766 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003767 if (hasCustomContentToLeft()) {
3768 mWorkspace.createCustomContentContainer();
3769 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003770 }
Winson Chung64359a52013-07-08 17:17:08 -07003771 }
3772
3773 @Override
3774 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003775 int count = orderedScreenIds.size();
3776 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003777 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003778 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003779 }
3780
Winson Chungd64d1762013-08-20 14:37:16 -07003781 public void bindAppsAdded(final ArrayList<Long> newScreens,
3782 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003783 final ArrayList<ItemInfo> addAnimated,
3784 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003785 Runnable r = new Runnable() {
3786 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003787 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003788 }
3789 };
3790 if (waitUntilResume(r)) {
3791 return;
3792 }
3793
Winson Chungd64d1762013-08-20 14:37:16 -07003794 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003795 if (newScreens != null) {
3796 bindAddScreens(newScreens);
3797 }
Winson Chungd64d1762013-08-20 14:37:16 -07003798
3799 // We add the items without animation on non-visible pages, and with
3800 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003801 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003802 bindItems(addNotAnimated, 0,
3803 addNotAnimated.size(), false);
3804 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003805 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003806 bindItems(addAnimated, 0,
3807 addAnimated.size(), true);
3808 }
Winson Chungc58497e2013-09-03 17:48:37 -07003809
Winson Chung87412982013-10-03 18:34:14 -07003810 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003811 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003812
Winson Chungb745afb2015-03-02 11:51:23 -08003813 if (addedApps != null && mAppsView != null) {
3814 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003815 }
Winson Chungd64d1762013-08-20 14:37:16 -07003816 }
3817
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 * Bind the items start-end from the list.
3820 *
3821 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003822 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003823 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003824 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3825 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 Runnable r = new Runnable() {
3827 public void run() {
3828 bindItems(shortcuts, start, end, forceAnimateIcons);
3829 }
3830 };
3831 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003832 return;
3833 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003834
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003836 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3837 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003838 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003839 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003840 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003843
3844 // Short circuit if we are loading dock items for a configuration which has no dock
3845 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3846 mHotseat == null) {
3847 continue;
3848 }
3849
Sunny Goyal639e9062015-08-19 19:17:06 -07003850 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 switch (item.itemType) {
3852 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3853 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003854 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003855 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003856
Winson Chung64359a52013-07-08 17:17:08 -07003857 /*
3858 * TODO: FIX collision case
3859 */
Winson Chungce376632013-07-11 16:12:41 -07003860 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3861 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3862 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003863 View v = cl.getChildAt(item.cellX, item.cellY);
3864 Object tag = v.getTag();
3865 String desc = "Collision while binding workspace item: " + item
3866 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003867 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003868 throw (new RuntimeException(desc));
3869 } else {
3870 Log.d(TAG, desc);
3871 }
Winson Chungce376632013-07-11 16:12:41 -07003872 }
Winson Chung64359a52013-07-08 17:17:08 -07003873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003875 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003876 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003877 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003878 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003880 default:
3881 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003883
3884 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3885 item.cellY, 1, 1);
3886 if (animateIcons) {
3887 // Animate all the applications up now
3888 view.setAlpha(0f);
3889 view.setScaleX(0f);
3890 view.setScaleY(0f);
3891 bounceAnims.add(createNewAppBounceAnimation(view, i));
3892 newShortcutsScreenId = item.screenId;
3893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003894 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003895
Winson Chung997a9232013-07-24 15:33:46 -07003896 if (animateIcons) {
3897 // Animate to the correct page
3898 if (newShortcutsScreenId > -1) {
3899 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003900 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003901 final Runnable startBounceAnimRunnable = new Runnable() {
3902 public void run() {
3903 anim.playTogether(bounceAnims);
3904 anim.start();
3905 }
3906 };
Winson Chung997a9232013-07-24 15:33:46 -07003907 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003908 // We post the animation slightly delayed to prevent slowdowns
3909 // when we are loading right after we return to launcher.
3910 mWorkspace.postDelayed(new Runnable() {
3911 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003912 if (mWorkspace != null) {
3913 mWorkspace.snapToPage(newScreenIndex);
3914 mWorkspace.postDelayed(startBounceAnimRunnable,
3915 NEW_APPS_ANIMATION_DELAY);
3916 }
Winson Chung94d67682013-09-25 16:29:40 -07003917 }
3918 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003919 } else {
3920 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003921 }
3922 }
Winson Chung64359a52013-07-08 17:17:08 -07003923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003925 }
3926
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 /**
3928 * Implementation of the method from LauncherModel.Callbacks.
3929 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003930 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003931 Runnable r = new Runnable() {
3932 public void run() {
3933 bindFolders(folders);
3934 }
3935 };
3936 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003937 return;
3938 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003939 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003941
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003942 private void bindSafeModeWidget(LauncherAppWidgetInfo item) {
3943 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item, true);
3944 view.updateIcon(mIconCache);
3945 item.hostView = view;
3946 item.hostView.updateAppWidget(null);
3947 item.hostView.setOnClickListener(this);
3948 addAppWidgetToWorkspace(item, null, false);
3949 mWorkspace.requestLayout();
3950 }
3951
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 /**
3953 * Add the views for a widget to the workspace.
3954 *
3955 * Implementation of the method from LauncherModel.Callbacks.
3956 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003957 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003958 Runnable r = new Runnable() {
3959 public void run() {
3960 bindAppWidget(item);
3961 }
3962 };
3963 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003964 return;
3965 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003966
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003967 if (mIsSafeModeEnabled) {
3968 bindSafeModeWidget(item);
3969 return;
3970 }
3971
Daniel Sandler843e8602010-06-07 14:59:01 -04003972 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3973 if (DEBUG_WIDGETS) {
3974 Log.d(TAG, "bindAppWidget: " + item);
3975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003977 final LauncherAppWidgetProviderInfo appWidgetInfo;
Adam Cohen59400422014-03-05 18:07:04 -08003978
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003979 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) {
3980 // If the provider is not ready, bind as a pending widget.
3981 appWidgetInfo = null;
3982 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
3983 // The widget id is not valid. Try to find the widget based on the provider info.
3984 appWidgetInfo = mAppWidgetManager.findProvider(item.providerName, item.user);
3985 } else {
3986 appWidgetInfo = mAppWidgetManager.getLauncherAppWidgetInfo(item.appWidgetId);
3987 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003988
Sunny Goyal2e1efb42016-03-03 16:58:55 -08003989 // If the provider is ready, but the width is not yet restored, try to restore it.
3990 if (!item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) &&
3991 (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003992 if (appWidgetInfo == null) {
3993 if (DEBUG_WIDGETS) {
3994 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3995 + " belongs to component " + item.providerName
3996 + ", as the povider is null");
3997 }
3998 LauncherModel.deleteItemFromDatabase(this, item);
3999 return;
4000 }
Sunny Goyalff572272014-07-23 13:58:07 -07004001
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004002 // If we do not have a valid id, try to bind an id.
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004003 if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004004 // Note: This assumes that the id remap broadcast is received before this step.
4005 // If that is not the case, the id remap will be ignored and user may see the
4006 // click to setup view.
Sunny Goyal16466f12016-03-10 05:34:30 -08004007 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo);
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004008 pendingInfo.spanX = item.spanX;
4009 pendingInfo.spanY = item.spanY;
4010 pendingInfo.minSpanX = item.minSpanX;
4011 pendingInfo.minSpanY = item.minSpanY;
Sunny Goyalb740f592015-12-17 23:22:42 -08004012 Bundle options = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004013
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004014 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
4015 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4016 newWidgetId, appWidgetInfo, options);
4017
4018 // TODO consider showing a permission dialog when the widget is clicked.
4019 if (!success) {
4020 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4021 if (DEBUG_WIDGETS) {
4022 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4023 + " belongs to component " + item.providerName
4024 + ", as the launcher is unable to bing a new widget id");
4025 }
4026 LauncherModel.deleteItemFromDatabase(this, item);
4027 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004028 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004029
4030 item.appWidgetId = newWidgetId;
4031
4032 // If the widget has a configure activity, it is still needs to set it up, otherwise
4033 // the widget is ready to go.
4034 item.restoreStatus = (appWidgetInfo.configure == null)
4035 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4036 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4037
4038 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004039 } else if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_UI_NOT_READY)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004040 && (appWidgetInfo.configure == null)) {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004041 // The widget was marked as UI not ready, but there is no configure activity to
4042 // update the UI.
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004043 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4044 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004045 }
Sunny Goyalff572272014-07-23 13:58:07 -07004046 }
4047
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004048 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004049 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004050 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4051 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004052 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004053
Sunny Goyal56c73602015-09-25 12:55:01 -07004054 // Verify that we own the widget
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004055 if (appWidgetInfo == null) {
4056 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
Sunny Goyal56c73602015-09-25 12:55:01 -07004057 deleteWidgetInfo(item);
4058 return;
4059 }
4060
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004061 item.hostView = mAppWidgetHost.createView(this, item.appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004062 item.minSpanX = appWidgetInfo.minSpanX;
4063 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004064 addAppWidgetToWorkspace(item, appWidgetInfo, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004065 } else {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004066 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4067 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004068 view.updateIcon(mIconCache);
4069 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004070 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004071 item.hostView.setOnClickListener(this);
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004072 addAppWidgetToWorkspace(item, null, false);
Sunny Goyal651077b2014-06-30 14:15:31 -07004073 }
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004074 mWorkspace.requestLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075
Daniel Sandler843e8602010-06-07 14:59:01 -04004076 if (DEBUG_WIDGETS) {
4077 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4078 + (SystemClock.uptimeMillis()-start) + "ms");
4079 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 }
4081
Sunny Goyalff572272014-07-23 13:58:07 -07004082 /**
4083 * Restores a pending widget.
4084 *
4085 * @param appWidgetId The app widget id
4086 * @param cellInfo The position on screen where to create the widget.
4087 */
4088 private void completeRestoreAppWidget(final int appWidgetId) {
4089 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4090 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4091 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4092 return;
4093 }
4094
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004095 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004096 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4097
4098 mWorkspace.reinflateWidgetsIfNecessary();
4099 LauncherModel.updateItemInDatabase(this, info);
4100 }
4101
Adam Cohen1462de32012-07-24 22:34:36 -07004102 public void onPageBoundSynchronously(int page) {
4103 mSynchronouslyBoundPages.add(page);
4104 }
4105
Sunny Goyal527c7d32015-08-28 15:19:36 -07004106 @Override
4107 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4108 if (mPendingExecutor != null) {
4109 mPendingExecutor.markCompleted();
4110 }
4111 mPendingExecutor = executor;
4112 executor.attachTo(this);
4113 }
4114
4115 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4116 if (mPendingExecutor == executor) {
4117 mPendingExecutor = null;
4118 }
4119 }
4120
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 /**
4122 * Callback saying that there aren't any more items to bind.
4123 *
4124 * Implementation of the method from LauncherModel.Callbacks.
4125 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004126 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004127 Runnable r = new Runnable() {
4128 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004129 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004130 }
4131 };
4132 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004133 return;
4134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004135 if (mSavedState != null) {
4136 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004137 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004138 }
Sunny Goyal08442b82015-10-21 17:15:08 -07004139
Joe Onorato9c1289c2009-08-17 11:03:03 -04004140 mSavedState = null;
4141 }
4142
Adam Cohen1462de32012-07-24 22:34:36 -07004143 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004145 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004146 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004147
4148 // If we received the result of any pending adds while the loader was running (e.g. the
4149 // widget configuration forced an orientation change), process them now.
4150 if (sPendingAddItem != null) {
4151 final long screenId = completeAdd(sPendingAddItem);
4152
4153 // TODO: this moves the user to the page where the pending item was added. Ideally,
4154 // the screen would be guaranteed to exist after bind, and the page would be set through
4155 // the workspace restore process.
4156 mWorkspace.post(new Runnable() {
4157 @Override
4158 public void run() {
4159 mWorkspace.snapToScreenId(screenId);
4160 }
4161 });
4162 sPendingAddItem = null;
4163 }
4164
Sunny Goyal756adbc2015-04-16 15:20:51 -07004165 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004166
4167 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004168 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004169 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004170 }
4171
Adam Cohen3ed316a2014-07-23 18:21:20 -07004172 private void sendLoadingCompleteBroadcastIfNecessary() {
4173 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4174 String permission =
4175 getResources().getString(R.string.receive_first_load_broadcast_permission);
4176 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4177 sendBroadcast(intent, permission);
4178 SharedPreferences.Editor editor = mSharedPrefs.edit();
4179 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4180 editor.apply();
4181 }
4182 }
4183
Winson Chunga0b7e862013-09-05 16:03:15 -07004184 public boolean isAllAppsButtonRank(int rank) {
4185 if (mHotseat != null) {
4186 return mHotseat.isAllAppsButtonRank(rank);
4187 }
4188 return false;
4189 }
4190
Winson Chunga2413752012-04-03 14:22:34 -07004191 private boolean canRunNewAppsAnimation() {
4192 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004193 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4194 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004195 }
4196
Winson Chungc9168342013-06-26 14:54:55 -07004197 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004198 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004199 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4200 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004201 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004202 return bounceAnim;
4203 }
4204
Adam Cohen27772732013-11-11 14:00:56 +00004205 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004206 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004207 }
4208
Tony Wickham3a3517f2015-10-06 11:27:04 -07004209 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004210 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004211 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004212 }
4213
Tony Wickham55616cd2015-09-23 14:55:17 -07004214 public int getSearchBarHeight() {
4215 if (mLauncherCallbacks != null) {
4216 return mLauncherCallbacks.getSearchBarHeight();
4217 }
4218 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4219 }
4220
Winson Chung88fa7412015-08-03 14:25:28 -07004221 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004222 if (mSearchDropTargetBar == null) {
4223 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004224 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004225 if (mQsb != null) {
4226 mSearchDropTargetBar.removeView(mQsb);
4227 mQsb = null;
4228 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004229 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004230 }
4231
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004232 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004233 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4234 * multiple calls to bind the same list.)
4235 */
4236 @Thunk ArrayList<AppInfo> mTmpAppsList;
4237 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4238 public void run() {
4239 bindAllApplications(mTmpAppsList);
4240 mTmpAppsList = null;
4241 }
4242 };
4243
4244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 * Add the icons for all apps.
4246 *
4247 * Implementation of the method from LauncherModel.Callbacks.
4248 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004249 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004250 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4251 mTmpAppsList = apps;
4252 return;
4253 }
4254
Winson Chungb745afb2015-03-02 11:51:23 -08004255 if (mAppsView != null) {
4256 mAppsView.setApps(apps);
4257 }
Adam Cohen9211d422014-10-07 18:14:53 -07004258 if (mLauncherCallbacks != null) {
4259 mLauncherCallbacks.bindAllApplications(apps);
4260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 }
4262
4263 /**
4264 * A package was updated.
4265 *
4266 * Implementation of the method from LauncherModel.Callbacks.
4267 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004268 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004269 Runnable r = new Runnable() {
4270 public void run() {
4271 bindAppsUpdated(apps);
4272 }
4273 };
4274 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004275 return;
4276 }
4277
Winson Chungb745afb2015-03-02 11:51:23 -08004278 if (mAppsView != null) {
4279 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004281 }
4282
Sunny Goyal4390ace2014-10-13 11:33:11 -07004283 @Override
4284 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4285 Runnable r = new Runnable() {
4286 public void run() {
4287 bindWidgetsRestored(widgets);
4288 }
4289 };
4290 if (waitUntilResume(r)) {
4291 return;
4292 }
4293 mWorkspace.widgetsRestored(widgets);
4294 }
4295
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004297 * Some shortcuts were updated in the background.
4298 *
4299 * Implementation of the method from LauncherModel.Callbacks.
4300 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004301 @Override
4302 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4303 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004304 Runnable r = new Runnable() {
4305 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004306 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004307 }
4308 };
4309 if (waitUntilResume(r)) {
4310 return;
4311 }
4312
Sunny Goyal4390ace2014-10-13 11:33:11 -07004313 if (!updated.isEmpty()) {
4314 mWorkspace.updateShortcuts(updated);
4315 }
4316
4317 if (!removed.isEmpty()) {
4318 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4319 for (ShortcutInfo si : removed) {
4320 removedComponents.add(si.getTargetComponent());
4321 }
4322 mWorkspace.removeItemsByComponentName(removedComponents, user);
4323 // Notify the drag controller
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004324 mDragController.onAppsRemoved(new HashSet<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004325 }
4326 }
4327
4328 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004329 * Update the state of a package, typically related to install state.
4330 *
4331 * Implementation of the method from LauncherModel.Callbacks.
4332 */
Sunny Goyale755d462014-07-22 13:48:29 -07004333 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004334 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4335 Runnable r = new Runnable() {
4336 public void run() {
4337 bindRestoreItemsChange(updates);
4338 }
4339 };
4340 if (waitUntilResume(r)) {
4341 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004342 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004343
Sunny Goyal756adbc2015-04-16 15:20:51 -07004344 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004345 }
4346
4347 /**
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004348 * A package was uninstalled/updated. We take both the super set of packageNames
Winson Chung83892cc2013-05-01 16:53:33 -07004349 * in addition to specific applications to remove, the reason being that
4350 * this can be called when a package is updated as well. In that scenario,
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004351 * we only remove specific components from the workspace and hotseat, where as
Winson Chung83892cc2013-05-01 16:53:33 -07004352 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004354 @Override
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004355 public void bindWorkspaceComponentsRemoved(
4356 final HashSet<String> packageNames, final HashSet<ComponentName> components,
4357 final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004358 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004359 public void run() {
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004360 bindWorkspaceComponentsRemoved(packageNames, components, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004361 }
Winson Chungd64d1762013-08-20 14:37:16 -07004362 };
4363 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004364 return;
4365 }
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004366 if (!packageNames.isEmpty()) {
4367 mWorkspace.removeItemsByPackageName(packageNames, user);
4368 }
4369 if (!components.isEmpty()) {
4370 mWorkspace.removeItemsByComponentName(components, user);
4371 }
4372 // Notify the drag controller
4373 mDragController.onAppsRemoved(packageNames, components);
Winson Chung83892cc2013-05-01 16:53:33 -07004374
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004375 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004376
Sunny Goyal3bbbabc2016-03-15 09:16:30 -07004377 @Override
4378 public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
4379 Runnable r = new Runnable() {
4380 public void run() {
4381 bindAppInfosRemoved(appInfos);
4382 }
4383 };
4384 if (waitUntilResume(r)) {
4385 return;
Joe Onorato36115782010-06-17 13:28:48 -04004386 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004387
Winson Chungdf95eb12013-10-16 14:57:07 -07004388 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004389 if (mAppsView != null) {
4390 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004392 }
Joe Onoratobe386092009-11-17 17:32:16 -08004393
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004394 private Runnable mBindWidgetModelRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004395 public void run() {
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004396 bindWidgetsModel(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004397 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004398 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004399
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004400 @Override
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004401 public void bindWidgetsModel(WidgetsModel model) {
4402 if (waitUntilResume(mBindWidgetModelRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004403 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004404 return;
4405 }
4406
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004407 if (mWidgetsView != null && model != null) {
4408 mWidgetsView.addWidgets(model);
4409 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004410 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004411 }
4412
Sunny Goyal2e1efb42016-03-03 16:58:55 -08004413 @Override
4414 public void notifyWidgetProvidersChanged() {
4415 if (mWorkspace.getState().shouldUpdateWidget) {
4416 mModel.refreshAndBindWidgetsAndShortcuts(this, mWidgetsView.isEmpty());
4417 }
4418 }
4419
Winson Chung400438b2011-01-16 17:53:48 -08004420 private int mapConfigurationOriActivityInfoOri(int configOri) {
4421 final Display d = getWindowManager().getDefaultDisplay();
4422 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4423 switch (d.getRotation()) {
4424 case Surface.ROTATION_0:
4425 case Surface.ROTATION_180:
4426 // We are currently in the same basic orientation as the natural orientation
4427 naturalOri = configOri;
4428 break;
4429 case Surface.ROTATION_90:
4430 case Surface.ROTATION_270:
4431 // We are currently in the other basic orientation to the natural orientation
4432 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4433 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4434 break;
4435 }
4436
4437 int[] oriMap = {
4438 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4439 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4440 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4441 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4442 };
4443 // Since the map starts at portrait, we need to offset if this device's natural orientation
4444 // is landscape.
4445 int indexOffset = 0;
4446 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4447 indexOffset = 1;
4448 }
4449 return oriMap[(d.getRotation() + indexOffset) % 4];
4450 }
Adam Cohen446e9402011-09-15 18:21:21 -07004451
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004452 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004453 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004454 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004455 if (Utilities.ATLEAST_JB_MR2) {
4456 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4457 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004458 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4459 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004460 }
Winson Chung4b919f82012-05-01 10:44:08 -07004461 }
4462 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004463
Winson Chung4b919f82012-05-01 10:44:08 -07004464 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004465 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004466 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004467 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004468 } else {
4469 mHandler.postDelayed(new Runnable() {
4470 public void run() {
4471 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4472 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004473 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004474 }
Winson Chung4b919f82012-05-01 10:44:08 -07004475 }
Winson Chung400438b2011-01-16 17:53:48 -08004476 }
4477
Winson Chunge43a1e72014-01-15 10:33:02 -08004478 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004479 if (mLauncherCallbacks != null) {
4480 return mLauncherCallbacks.isLauncherPreinstalled();
4481 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004482 PackageManager pm = getPackageManager();
4483 try {
4484 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4485 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4486 return true;
4487 } else {
4488 return false;
4489 }
4490 } catch (NameNotFoundException e) {
4491 e.printStackTrace();
4492 return false;
4493 }
4494 }
4495
Adam Cohenea90f832014-05-21 15:03:34 -07004496 /**
Adam Cohen432609a2014-03-13 17:03:22 -07004497 * To be overridden by subclasses to indicate that there is an activity to launch
4498 * before showing the standard launcher experience.
4499 */
4500 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004501 if (mLauncherCallbacks != null) {
4502 return mLauncherCallbacks.hasFirstRunActivity();
4503 }
Adam Cohen432609a2014-03-13 17:03:22 -07004504 return false;
4505 }
4506
4507 /**
4508 * To be overridden by subclasses to launch any first run activity
4509 */
4510 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004511 if (mLauncherCallbacks != null) {
4512 return mLauncherCallbacks.getFirstRunActivity();
4513 }
Adam Cohen432609a2014-03-13 17:03:22 -07004514 return null;
4515 }
4516
Winson Chunga6945242014-01-08 14:04:34 -08004517 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004518 return !ActivityManager.isRunningInTestHarness() &&
4519 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004520 }
4521
Adam Cohen4a9423d2014-03-28 14:22:31 -07004522 protected boolean hasRunFirstRunActivity() {
4523 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4524 }
4525
Adam Cohen432609a2014-03-13 17:03:22 -07004526 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004527 if (shouldRunFirstRunActivity() &&
4528 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004529 Intent firstRunIntent = getFirstRunActivity();
4530 if (firstRunIntent != null) {
4531 startActivity(firstRunIntent);
4532 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004533 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004534 }
4535 }
Adam Cohen432609a2014-03-13 17:03:22 -07004536 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004537 }
4538
4539 private void markFirstRunActivityShown() {
4540 SharedPreferences.Editor editor = mSharedPrefs.edit();
4541 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4542 editor.apply();
4543 }
4544
Adam Cohen432609a2014-03-13 17:03:22 -07004545 /**
4546 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4547 * screen that must be displayed and dismissed.
4548 */
4549 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004550 if (mLauncherCallbacks != null) {
4551 return mLauncherCallbacks.hasDismissableIntroScreen();
4552 }
Adam Cohen432609a2014-03-13 17:03:22 -07004553 return false;
4554 }
4555
4556 /**
4557 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4558 */
4559 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004560 if (mLauncherCallbacks != null) {
4561 return mLauncherCallbacks.getIntroScreen();
4562 }
Adam Cohen432609a2014-03-13 17:03:22 -07004563 return null;
4564 }
4565
4566 /**
4567 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4568 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4569 */
4570 private boolean shouldShowIntroScreen() {
4571 return hasDismissableIntroScreen() &&
4572 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4573 }
4574
4575 protected void showIntroScreen() {
4576 View introScreen = getIntroScreen();
4577 changeWallpaperVisiblity(false);
4578 if (introScreen != null) {
4579 mDragLayer.showOverlayView(introScreen);
4580 }
4581 }
4582
4583 public void dismissIntroScreen() {
4584 markIntroScreenDismissed();
4585 if (showFirstRunActivity()) {
4586 // We delay hiding the intro view until the first run activity is showing. This
4587 // avoids a blip.
4588 mWorkspace.postDelayed(new Runnable() {
4589 @Override
4590 public void run() {
4591 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004592 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004593 }
4594 }, ACTIVITY_START_DELAY);
4595 } else {
4596 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07004597 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004598 }
4599 changeWallpaperVisiblity(true);
4600 }
4601
4602 private void markIntroScreenDismissed() {
4603 SharedPreferences.Editor editor = mSharedPrefs.edit();
4604 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4605 editor.apply();
4606 }
4607
Adam Cohen091440a2015-03-18 14:16:05 -07004608 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4610 // on the device, then we always show the first run cling experience (or if there is no
4611 // launcher2). Otherwise, we prompt the user upon started for migration
4612 LauncherClings launcherClings = new LauncherClings(this);
4613 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004614 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004615 if (mModel.canMigrateFromOldLauncherDb(this)) {
4616 launcherClings.showMigrationCling();
4617 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004618 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004619 }
4620 }
4621 }
4622
Winson Chunga6945242014-01-08 14:04:34 -08004623 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004624 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4625 if (mHotseat != null) mHotseat.setAlpha(1f);
4626 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004627 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4628 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004629 }
4630
4631 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004632 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4633 if (mHotseat != null) mHotseat.setAlpha(0f);
4634 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004635 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4636 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004637 }
4638
Winson Chung5ffd51d2015-06-25 11:36:50 -07004639 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004640 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004641 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004642 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004643 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004644 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004645 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4646 if (userHandle != null) {
4647 user = UserHandleCompat.fromUser(userHandle);
4648 }
4649 }
4650 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004651 }
4652
4653 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004654 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004655 if (user == null) {
4656 user = UserHandleCompat.myUserHandle();
4657 }
4658
4659 // Called from search suggestion, add the profile extra to the intent to ensure that we
4660 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004661 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004662 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004663 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 return null;
4665 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004666 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004667 }
4668
Winson Chung5ffd51d2015-06-25 11:36:50 -07004669 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004670 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4671 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004672 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004673 UserHandleCompat.myUserHandle());
4674 }
4675
Winson Chung5ffd51d2015-06-25 11:36:50 -07004676 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004677 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4678 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004679 mWorkspace.onExternalDragStartedWithItem(dragView);
4680 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004681 }
4682
Winson Chung5ffd51d2015-06-25 11:36:50 -07004683 protected void moveWorkspaceToDefaultScreen() {
4684 mWorkspace.moveToDefaultScreen(false);
4685 }
4686
Anjali Koppalf5d29132014-02-28 13:33:27 -08004687 @Override
4688 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004689 if (mLauncherCallbacks != null) {
4690 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4691 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004692 }
4693
Winson Chung80baf5a2010-08-09 16:03:15 -07004694 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004695 * Returns a FastBitmapDrawable with the icon, accurately sized.
4696 */
4697 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4698 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4699 d.setFilterBitmap(true);
4700 resizeIconDrawable(d);
4701 return d;
4702 }
4703
4704 /**
4705 * Resizes an icon drawable to the correct icon size.
4706 */
Winson5fbe0742015-09-30 15:33:00 -07004707 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004708 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004709 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004710 }
4711
4712 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004713 * Prints out out state for debugging.
4714 */
4715 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004716 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004717 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004718 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4719 Log.d(TAG, "mRestoring=" + mRestoring);
4720 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004721 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004722 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004723 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004724
Daniel Sandler325dc232013-06-05 22:57:57 -04004725 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004726 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004727
4728 @Override
4729 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4730 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004731 // Dump workspace
4732 writer.println(prefix + "Workspace Items");
4733 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4734 writer.println(prefix + " Homescreen " + i);
4735
4736 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4737 for (int j = 0; j < layout.getChildCount(); j++) {
4738 Object tag = layout.getChildAt(j).getTag();
4739 if (tag != null) {
4740 writer.println(prefix + " " + tag.toString());
4741 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004743 }
Sunny Goyala1365452015-10-01 15:46:24 -07004744
4745 writer.println(prefix + " Hotseat");
4746 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4747 for (int j = 0; j < layout.getChildCount(); j++) {
4748 Object tag = layout.getChildAt(j).getTag();
4749 if (tag != null) {
4750 writer.println(prefix + " " + tag.toString());
4751 }
4752 }
4753
4754 synchronized (sDumpLogs) {
4755 writer.println();
4756 writer.println(prefix + "Debug logs");
4757 for (String log : sDumpLogs) {
4758 writer.println(prefix + " " + log);
4759 }
4760 }
4761
Adam Cohen9211d422014-10-07 18:14:53 -07004762 if (mLauncherCallbacks != null) {
4763 mLauncherCallbacks.dump(prefix, fd, writer, args);
4764 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004765 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004766
Sunny Goyala1365452015-10-01 15:46:24 -07004767 public static void addDumpLog(String tag, String log) {
4768 Log.d(tag, log);
4769 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004770 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004771 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004772 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004773 }
4774
Adam Cohen59400422014-03-05 18:07:04 -08004775 public static CustomAppWidget getCustomAppWidget(String name) {
4776 return sCustomAppWidgets.get(name);
4777 }
4778
4779 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4780 return sCustomAppWidgets;
4781 }
4782
Sunny Goyal29538332016-02-18 09:10:19 -08004783 public static List<View> getFolderContents(View icon) {
4784 if (icon instanceof FolderIcon) {
4785 return ((FolderIcon) icon).getFolder().getItemsInReadingOrder();
4786 } else {
4787 return Collections.EMPTY_LIST;
4788 }
4789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004790}
Michael Jurka2763be32011-02-24 11:19:57 -08004791
Dan Sandlerd5024042014-01-09 15:01:33 -05004792interface DebugIntents {
4793 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4794 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004795}