blob: 8736b3ab752309f70f8001595d617f06b5824e53 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
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;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700104import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700105import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700106import com.android.launcher3.util.LongArrayMap;
Sunny Goyald3060552015-06-25 19:35:49 -0700107import com.android.launcher3.util.TestingUtils;
Adam Cohen091440a2015-03-18 14:16:05 -0700108import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700110import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700112
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700124import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126/**
127 * Default launcher application.
128 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100129public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700130 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700131 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700132 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700133 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Daniel Sandlerf061f822013-06-27 13:59:36 -0400135 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700136 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700137 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700139 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700140
Dan Sandlerd5024042014-01-09 15:01:33 -0500141 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700149 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700150
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700151 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
152 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
153 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
154
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700155 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800163 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 // To turn on these properties, type
166 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chung2672ff92012-05-04 16:22:30 -0700169 // The Intent extra that defines whether to ignore the launch animation
170 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400171 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
179 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Adam Cohen432609a2014-03-13 17:03:22 -0700194 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800195 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
196
Adam Cohen3ed316a2014-07-23 18:21:20 -0700197 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
198 static final String ACTION_FIRST_LOAD_COMPLETE =
199 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
200
Sunny Goyal594d76d2014-11-06 10:12:54 -0800201 private static final String QSB_WIDGET_ID = "qsb_widget_id";
202 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700207 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
208
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
Adam Cohenc2d6e892014-10-16 09:49:24 -0700214 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
215 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700216 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700217
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700219 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
220 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700221 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222
Winson Chunga2413752012-04-03 14:22:34 -0700223 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700224 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
225 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700227
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800228 private final BroadcastReceiver mCloseSystemDialogsReceiver
229 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private LayoutInflater mInflater;
232
Adam Cohen091440a2015-03-18 14:16:05 -0700233 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800235 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragController mDragController;
Sunny Goyald3060552015-06-25 19:35:49 -0700238
239 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Sunny Goyalffe83f12014-08-14 17:39:34 -0700241 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800245 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800246 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700247
Michael Jurka0280c3b2010-09-17 15:00:07 -0700248 private int[] mTmpAddItemCellCoordinates = new int[2];
249
Sunny Goyal316490e2015-06-02 09:38:28 -0700250 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800251 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700254 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700255
Winson Chungc51db6a2011-10-05 11:44:49 -0700256 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
258 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700259 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700260
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700261 // Main container view and the model for the widget tray screen.
262 @Thunk WidgetsContainerView mWidgetsView;
263 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700264
Winson Chungf0ea4d32011-06-06 14:27:16 -0700265 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800266 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700269 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
270 // scroll issues (because the workspace may not have been measured yet) and extra work.
271 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
272 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
274 private SpannableStringBuilder mDefaultKeySsb = null;
275
Adam Cohen091440a2015-03-18 14:16:05 -0700276 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800278 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private boolean mRestoring;
280 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700281 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Michael Jurka1e2f4652013-07-08 18:03:46 -0700283 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700284 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private Bundle mSavedInstanceState;
287
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800289 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700290 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700292 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Sunny Goyal639e9062015-08-19 19:17:06 -0700295 private LauncherClings mClings;
296
Sunny Goyale2df0622015-04-24 11:27:00 -0700297 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700298
Adam Cohen61f560d2013-09-30 15:58:20 -0700299 private View.OnTouchListener mHapticFeedbackTouchListener;
300
Adam Cohended9f8d2010-11-03 13:25:16 -0700301 // Related to the auto-advancing of widgets
302 private final int ADVANCE_MSG = 1;
303 private final int mAdvanceInterval = 20000;
304 private final int mAdvanceStagger = 250;
305 private long mAutoAdvanceSentTime;
306 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700308 new HashMap<View, AppWidgetProviderInfo>();
309
Winson Chung400438b2011-01-16 17:53:48 -0800310 // Determines how long to wait after a rotation before restoring the screen orientation to
311 // match the sensor state.
312 private final int mRestoreScreenOrientationDelay = 500;
313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700315
Adam Cohen1462de32012-07-24 22:34:36 -0700316 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700317 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700318
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700320 static Date sDateStamp = new Date();
321 static DateFormat sDateFormat =
322 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
323 static long sRunStart = System.currentTimeMillis();
324 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325
Winson Chung46353de2012-02-16 14:05:10 -0800326 // We only want to get the SharedPreferences once since it does an FS stat each time we get
327 // it from the context.
328 private SharedPreferences mSharedPrefs;
329
Winson Chungf0c6ae02012-03-21 16:10:31 -0700330 // Holds the page that we need to animate to, and the icon views that we need to animate up
331 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700332 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700333 private Bitmap mFolderIconBitmap;
334 private Canvas mFolderIconCanvas;
335 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700336
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700337 private DeviceProfile mDeviceProfile;
338
Adam Cohen4b66bf32015-09-18 12:15:19 -0700339 private boolean mMoveToDefaultScreenFromNewIntent;
340
Winson Chung13eb5272015-05-11 16:30:13 -0700341 // This is set to the view that launched the activity that navigated the user away from
342 // launcher. Since there is no callback for when the activity has finished launching, enable
343 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800344 private BubbleTextView mWaitingForResume;
345
Adam Cohen59400422014-03-05 18:07:04 -0800346 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
347 new HashMap<String, CustomAppWidget>();
348
Adam Cohen59400422014-03-05 18:07:04 -0800349 static {
Sunny Goyald3060552015-06-25 19:35:49 -0700350 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
351 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800352 }
353 }
354
Adam Cohen091440a2015-03-18 14:16:05 -0700355 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700356 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700357 if (mWorkspace != null) {
358 mWorkspace.buildPageHardwareLayers();
359 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 }
361 };
362
Adam Cohendb364c32014-05-20 14:23:40 -0700363 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364
Adam Cohen091440a2015-03-18 14:16:05 -0700365 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int requestCode;
367 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700368 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700369 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800370 int cellX;
371 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700372 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373 }
374
Daniel Sandlerff02d492013-08-05 02:12:05 -0400375 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700376 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700377 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400378
379 @Thunk void setOrientation() {
380 if (mRotationEnabled) {
381 unlockScreenOrientation(true);
382 } else {
383 setRequestedOrientation(
384 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700385 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400386 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700387
Sunny Goyal7779d622015-06-11 16:18:39 -0700388 private Runnable mUpdateOrientationRunnable = new Runnable() {
389 public void run() {
390 setOrientation();
391 }
392 };
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 @Override
395 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700396 if (DEBUG_STRICT_MODE) {
397 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
398 .detectDiskReads()
399 .detectDiskWrites()
400 .detectNetwork() // or .detectAll() for all detectable problems
401 .penaltyLog()
402 .build());
403 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
404 .detectLeakedSqlLiteObjects()
405 .detectLeakedClosableObjects()
406 .penaltyLog()
407 .penaltyDeath()
408 .build());
409 }
410
Adam Cohen9211d422014-10-07 18:14:53 -0700411 if (mLauncherCallbacks != null) {
412 mLauncherCallbacks.preOnCreate();
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Adam Cohen2e6da152015-05-06 11:42:25 -0700420 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700421 mDeviceProfile = getResources().getConfiguration().orientation
422 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700423 app.getInvariantDeviceProfile().landscapeProfile
424 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700425
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400426 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700427 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700428 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mModel = app.setLauncher(this);
430 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700431
Joe Onorato41a12d22009-10-31 18:30:00 -0400432 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 mInflater = getLayoutInflater();
Winson Chung006ee262015-08-03 14:40:11 -0700434 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Daniel Sandlerff02d492013-08-05 02:12:05 -0400436 mStats = new Stats(this);
437
Sunny Goyalffe83f12014-08-14 17:39:34 -0700438 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
441 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700443 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
444 // this also ensures that any synchronous binding below doesn't re-trigger another
445 // LauncherModel load.
446 mPaused = false;
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200449 android.os.Debug.startMethodTracing(
450 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700456 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457
Joe Onorato7c312c12009-08-13 21:36:53 -0700458 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 mSavedState = savedInstanceState;
461 restoreState(mSavedState);
462
463 if (PROFILE_STARTUP) {
464 android.os.Debug.stopMethodTracing();
465 }
466
467 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700468 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 // If the user leaves launcher, then we should just load items asynchronously when
470 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700471 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 } else {
473 // We only load the page synchronously if the user rotates (or triggers a
474 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700475 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478
479 // For handling default keys
480 mDefaultKeySsb = new SpannableStringBuilder();
481 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800482
483 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
484 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800485
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700486 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
487 // In case we are on a device with locked rotation, we should look at preferences to check
488 // if the user has specifically allowed rotation.
489 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400490 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700491 }
492
493 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
494 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400495 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700496
Adam Cohen9211d422014-10-07 18:14:53 -0700497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700499 if (mLauncherCallbacks.hasLauncherOverlay()) {
500 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700501 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
502 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
503 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700504 mWorkspace.setLauncherOverlay(mLauncherOverlay);
505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700507
508 if (shouldShowIntroScreen()) {
509 showIntroScreen();
510 } else {
511 showFirstRunActivity();
512 showFirstRunClings();
513 }
Adam Cohen9211d422014-10-07 18:14:53 -0700514 }
515
Sunny Goyal7779d622015-06-11 16:18:39 -0700516 @Override
517 public void onSettingsChanged(String settings, boolean value) {
518 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
519 mRotationEnabled = value;
520 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
521 mUpdateOrientationRunnable.run();
522 }
523 }
524 }
525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 private LauncherCallbacks mLauncherCallbacks;
527
528 public void onPostCreate(Bundle savedInstanceState) {
529 super.onPostCreate(savedInstanceState);
530 if (mLauncherCallbacks != null) {
531 mLauncherCallbacks.onPostCreate(savedInstanceState);
532 }
533 }
534
535 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
536 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 private boolean mWorkspaceImportanceStored = false;
539 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700540 private int mWorkspaceImportanceForAccessibility =
541 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
542 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
543
544 @Override
545 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700546 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700547 return;
548 }
549 // The underlying workspace and hotseat are temporarily suppressed by the search
550 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 if (mWorkspace != null) {
552 mWorkspaceImportanceForAccessibility =
553 mWorkspace.getImportantForAccessibility();
554 mWorkspace.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mWorkspaceImportanceStored = true;
557 }
558 if (mHotseat != null) {
559 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
560 mHotseat.setImportantForAccessibility(
561 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
562 mHotseatImportanceStored = true;
563 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565
566 @Override
567 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700568 if (mWorkspaceImportanceStored && mWorkspace != null) {
569 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
570 }
571 if (mHotseatImportanceStored && mHotseat != null) {
572 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
573 }
574 mWorkspaceImportanceStored = false;
575 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700576 }
577 });
Adam Cohen9211d422014-10-07 18:14:53 -0700578 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700579 }
580
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700582 public void onLauncherProviderChange() {
583 if (mLauncherCallbacks != null) {
584 mLauncherCallbacks.onLauncherProviderChange();
585 }
586 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700587
Winson Chungef7f8742015-06-04 17:18:17 -0700588 /**
589 * Updates the bounds of all the overlays to match the new fixed bounds.
590 */
591 public void updateOverlayBounds(Rect newBounds) {
592 mAppsView.setSearchBarBounds(newBounds);
593 mWidgetsView.setSearchBarBounds(newBounds);
594 }
595
Adam Cohen9211d422014-10-07 18:14:53 -0700596 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700597 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700598 if (mLauncherCallbacks != null) {
599 return mLauncherCallbacks.hasCustomContentToLeft();
600 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 return false;
602 }
603
Dave Hawkeya8881582013-09-17 15:55:33 -0600604 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500605 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600606 * {@link #addToCustomContentPage}. This will only be invoked if
607 * {@link #hasCustomContentToLeft()} is {@code true}.
608 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700610 if (mLauncherCallbacks != null) {
611 mLauncherCallbacks.populateCustomContentContainer();
612 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 }
614
615 /**
616 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
617 * ensure the custom content page is added or removed if necessary.
618 */
619 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600620 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 // Not bound yet, wait for bindScreens to be called.
622 return;
623 }
624
625 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
626 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500627 mWorkspace.createCustomContentContainer();
628 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600629 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
630 mWorkspace.removeCustomContentPage();
631 }
632 }
633
Anton Hanssona2f665f2013-09-26 12:18:32 +0100634 public Stats getStats() {
635 return mStats;
636 }
637
Bjorn Bringertc459e522013-06-07 19:36:01 +0100638 public LayoutInflater getInflater() {
639 return mInflater;
640 }
641
Hyunyoung Song3f471442015-04-08 19:01:34 -0700642 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700643 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
644 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700645 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700646 }
647
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000648 public int getViewIdForItem(ItemInfo info) {
Sunny Goyald1a0e8b2015-08-27 17:45:46 -0700649 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
650 // This cast is safe as long as the id < 0x00FFFFFF
651 // Since we jail all the dynamically generated views, there should be no clashes
652 // with any other views.
653 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000654 }
655
656 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700657 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
658 * a configuration step, this allows the proper animations to run after other transitions.
659 */
Adam Cohendb364c32014-05-20 14:23:40 -0700660 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700661 long screenId = args.screenId;
662 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
663 // When the screen id represents an actual screen (as opposed to a rank) we make sure
664 // that the drop page actually exists.
665 screenId = ensurePendingDropLayoutExists(args.screenId);
666 }
Adam Cohendb364c32014-05-20 14:23:40 -0700667
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800668 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700670 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700671 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700672 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700674 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700675 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700676 case REQUEST_RECONFIGURE_APPWIDGET:
677 completeRestoreAppWidget(args.appWidgetId);
678 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 }
Michael Jurka27614d22012-04-02 06:40:22 -0700680 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
681 // if you turned the screen off and then back while in All Apps, Launcher would not
682 // return to the workspace. Clearing mAddInfo.container here fixes this issue
683 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700684 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 }
686
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800687 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700689 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700690 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700691 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800692 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700693
Adam Cohenad4e15c2013-10-17 16:21:35 -0700694 Runnable exitSpringLoaded = new Runnable() {
695 @Override
696 public void run() {
697 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
698 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
699 }
700 };
701
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700703 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
706 if (resultCode == RESULT_CANCELED) {
707 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700708 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700709 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800711 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700713
714 // When the user has granted permission to bind widgets, we should check to see if
715 // we can inflate the default search bar widget.
716 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 }
718 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200719 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
720 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickham6ed058c2015-09-09 18:21:49 -0700721 // User could have free-scrolled between pages before picking a wallpaper; make sure
722 // we move to the closest one now to avoid visual jump.
723 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700724 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200725 }
726 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728
Adam Cohened66b2b2012-01-23 17:28:51 -0800729 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700730 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700731
Adam Cohendb364c32014-05-20 14:23:40 -0700732 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 // We have special handling for widgets
734 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800735 final int appWidgetId;
736 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
737 : -1;
738 if (widgetId < 0) {
739 appWidgetId = pendingAddWidgetId;
740 } else {
741 appWidgetId = widgetId;
742 }
743
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800745 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700746 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
747 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 result = RESULT_CANCELED;
749 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700750 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 @Override
752 public void run() {
753 exitSpringLoadedDragModeDelayed(false, 0, null);
754 }
755 };
Adam Cohendb364c32014-05-20 14:23:40 -0700756 if (workspaceLocked) {
757 // No need to remove the empty screen if we're mid-binding, as the
758 // the bind will not add the empty screen.
759 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
760 } else {
761 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
762 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
763 }
Winson Chung5aaab772012-04-27 15:24:02 -0700764 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700765 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700766 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
767 // When the screen id represents an actual screen (as opposed to a rank)
768 // we make sure that the drop page actually exists.
769 mPendingAddInfo.screenId =
770 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
771 }
Adam Cohendb364c32014-05-20 14:23:40 -0700772 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
773
774 dropLayout.setDropPending(true);
775 final Runnable onComplete = new Runnable() {
776 @Override
777 public void run() {
778 completeTwoStageWidgetDrop(resultCode, appWidgetId);
779 dropLayout.setDropPending(false);
780 }
781 };
782 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
784 } else {
785 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
786 mPendingAddInfo);
787 sPendingAddItem = args;
788 }
Winson Chung5aaab772012-04-27 15:24:02 -0700789 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 return;
791 }
792
Sunny Goyalff572272014-07-23 13:58:07 -0700793 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
794 if (resultCode == RESULT_OK) {
795 // Update the widget view.
796 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
797 pendingAddWidgetId, mPendingAddInfo);
798 if (workspaceLocked) {
799 sPendingAddItem = args;
800 } else {
801 completeAdd(args);
802 }
803 }
804 // Leave the widget in the pending state if the user canceled the configure.
805 return;
806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 // The pattern used here is that a user PICKs a specific application,
809 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
812 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700813 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
815 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800816 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700817 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800818 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700820 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
821 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
Adam Cohen00074722013-10-11 17:46:09 -0700823 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700824 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700825 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800827 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800828
829 }
830
831 @Override
832 protected void onActivityResult(
833 final int requestCode, final int resultCode, final Intent data) {
834 handleActivityResult(requestCode, resultCode, data);
835 if (mLauncherCallbacks != null) {
836 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
837 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 }
839
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600840 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700841 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600842 int[] grantResults) {
843 if (mLauncherCallbacks != null) {
844 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
845 grantResults);
846 }
847 }
848
Adam Cohendb364c32014-05-20 14:23:40 -0700849 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
850 appWidgetId, ItemInfo info) {
851 PendingAddArguments args = new PendingAddArguments();
852 args.requestCode = requestCode;
853 args.intent = data;
854 args.container = info.container;
855 args.screenId = info.screenId;
856 args.cellX = info.cellX;
857 args.cellY = info.cellY;
858 args.appWidgetId = appWidgetId;
859 return args;
860 }
861
862 /**
863 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
864 *
865 * @param screenId the screen id to check
866 * @return the new screen, or screenId if it exists
867 */
868 private long ensurePendingDropLayoutExists(long screenId) {
869 CellLayout dropLayout =
870 (CellLayout) mWorkspace.getScreenWithId(screenId);
871 if (dropLayout == null) {
872 // it's possible that the add screen was removed because it was
873 // empty and a re-bind occurred
874 mWorkspace.addExtraEmptyScreen();
875 return mWorkspace.commitExtraEmptyScreen();
876 } else {
877 return screenId;
878 }
879 }
880
Adam Cohen091440a2015-03-18 14:16:05 -0700881 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700882 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700883 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 Runnable onCompleteRunnable = null;
885 int animationType = 0;
886
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700887 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 if (resultCode == RESULT_OK) {
889 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
890 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 mPendingAddWidgetInfo);
892 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 onCompleteRunnable = new Runnable() {
894 @Override
895 public void run() {
896 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700897 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
899 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 }
901 };
902 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800903 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 if (mDragLayer.getAnimatedView() != null) {
907 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
908 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
909 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700911 // The animated view may be null in the case of a rotation during widget configuration
912 onCompleteRunnable.run();
913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 }
915
916 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700917 protected void onStop() {
918 super.onStop();
919 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700920
921 if (mLauncherCallbacks != null) {
922 mLauncherCallbacks.onStop();
923 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700924 }
925
926 @Override
927 protected void onStart() {
928 super.onStart();
929 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700930
931 if (mLauncherCallbacks != null) {
932 mLauncherCallbacks.onStart();
933 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700934 }
935
936 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200938 long startTime = 0;
939 if (DEBUG_RESUME_TIME) {
940 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400941 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200942 }
Adam Cohen9211d422014-10-07 18:14:53 -0700943
944 if (mLauncherCallbacks != null) {
945 mLauncherCallbacks.preOnResume();
946 }
947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700949
Winson Chung4a2afa32012-07-19 14:53:05 -0700950 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700951 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700952 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800953 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700954 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700955 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700956 // view after launching an app, as they may be depending on the UI to be static to
957 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700958 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700959 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800960 } else if (mOnResumeState == State.WIDGETS) {
961 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 }
963 mOnResumeState = State.NONE;
964
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700965 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700966 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
967 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700968
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800969 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700970 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700971 setWorkspaceLoading(true);
Sunny Goyal22aa3812015-08-19 16:24:27 -0700972
973 // If we're starting binding all over again, clear any bind calls we'd postponed in
974 // the past (see waitUntilResume) -- we don't need them since we're starting binding
975 // from scratch again
976 mBindOnResumeCallbacks.clear();
977
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700978 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700980 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700982 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200983 // We might have postponed some bind calls until onResume (see waitUntilResume) --
984 // execute them here
985 long startTimeCallbacks = 0;
986 if (DEBUG_RESUME_TIME) {
987 startTimeCallbacks = System.currentTimeMillis();
988 }
989
Michael Jurka1e2f4652013-07-08 18:03:46 -0700990 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
991 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200992 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700993 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200994 if (DEBUG_RESUME_TIME) {
995 Log.d(TAG, "Time spent processing callbacks in onResume: " +
996 (System.currentTimeMillis() - startTimeCallbacks));
997 }
Michael Jurka447bf842013-05-15 14:52:15 +0200998 }
Michael Jurka54554252013-08-01 12:52:23 +0200999 if (mOnResumeCallbacks.size() > 0) {
1000 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1001 mOnResumeCallbacks.get(i).run();
1002 }
1003 mOnResumeCallbacks.clear();
1004 }
Winson Chunge4e50662012-01-23 14:45:13 -08001005
1006 // Reset the pressed state of icons that were locked in the press state while activities
1007 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001008 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001009 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001010 mWaitingForResume.setStayPressed(false);
1011 }
Winson Chung82963d52013-10-09 11:20:57 -07001012
Adam Cohen06dff352012-06-01 17:17:08 -07001013 // It is possible that widgets can receive updates while launcher is not in the foreground.
1014 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1015 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1016 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -07001017 if (!isWorkspaceLoading()) {
1018 getWorkspace().reinflateWidgetsIfNecessary();
1019 }
Sunny Goyal22235bc2015-04-03 09:23:43 -07001020 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001021
Michael Jurka447bf842013-05-15 14:52:15 +02001022 if (DEBUG_RESUME_TIME) {
1023 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1024 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001025
Adam Cohen4b66bf32015-09-18 12:15:19 -07001026 // We want to suppress callbacks about CustomContent being shown if we have just received
1027 // onNewIntent while the user was present within launcher. In that case, we post a call
1028 // to move the user to the main screen (which will occur after onResume). We don't want to
1029 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1030 // suppress here.
1031 if (mWorkspace.getCustomContentCallbacks() != null
1032 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001033 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001034 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001035 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1036 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001037 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001038 }
1039 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001040 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001041 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001042 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001043
Sunny Goyal756adbc2015-04-16 15:20:51 -07001044 if (!isWorkspaceLoading()) {
1045 // Process any items that were added while Launcher was away.
1046 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1047 }
Adam Cohen9211d422014-10-07 18:14:53 -07001048
1049 if (mLauncherCallbacks != null) {
1050 mLauncherCallbacks.onResume();
1051 }
Adam Cohen06dff352012-06-01 17:17:08 -07001052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001056 // Ensure that items added to Launcher are queued until Launcher returns
1057 InstallShortcutReceiver.enableInstallQueue();
1058
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001060 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001061 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001062 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001063
1064 // We call onHide() aggressively. The custom content callbacks should be able to
1065 // debounce excess onHide calls.
1066 if (mWorkspace.getCustomContentCallbacks() != null) {
1067 mWorkspace.getCustomContentCallbacks().onHide();
1068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Adam Cohen9211d422014-10-07 18:14:53 -07001070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001072 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 }
1074
1075 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1077 // by a onResume or by scrolling otherwise.
1078 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001079
1080 // Custom content is completely hidden
1081 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001082
1083 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1084 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001085
1086 // Indicates whether the user is allowed to scroll away from the custom content.
1087 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001088 }
1089
Adam Cohenc2d6e892014-10-16 09:49:24 -07001090 public interface LauncherOverlay {
1091
1092 /**
1093 * Touch interaction leading to overscroll has begun
1094 */
1095 public void onScrollInteractionBegin();
1096
1097 /**
1098 * Touch interaction related to overscroll has ended
1099 */
1100 public void onScrollInteractionEnd();
1101
1102 /**
1103 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1104 * screen (or in the case of RTL, the rightmost screen).
1105 */
1106 public void onScrollChange(int progress, boolean rtl);
1107
1108 /**
1109 * Screen has stopped scrolling
1110 */
1111 public void onScrollSettled();
1112
1113 /**
1114 * This method can be called by the Launcher in order to force the LauncherOverlay
1115 * to exit fully immersive mode.
1116 */
1117 public void forceExitFullImmersion();
1118 }
1119
Jun Mukai8af0cd82015-05-12 12:32:12 -07001120 public interface LauncherSearchCallbacks {
1121 /**
1122 * Called when the search overlay is shown.
1123 */
1124 public void onSearchOverlayOpened();
1125
1126 /**
1127 * Called when the search overlay is dismissed.
1128 */
1129 public void onSearchOverlayClosed();
1130 }
1131
Adam Cohenc2d6e892014-10-16 09:49:24 -07001132 public interface LauncherOverlayCallbacks {
1133 /**
1134 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1135 * however it doesn't modify any state within the launcher.
1136 */
1137 public boolean canEnterFullImmersion();
1138
1139 /**
1140 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1141 * eg. by occupying the full screen and handling all touch events.
1142 *
1143 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1144 * case, Launcher will modify any necessary state and assumes the overlay is
1145 * handling all interaction. If false, the LauncherOverlay should cancel any
1146 *
1147 */
1148 public boolean enterFullImmersion();
1149
1150 /**
1151 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1152 * full control over UI and state.
1153 */
1154 public void exitFullImmersion();
1155 }
1156
1157 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1158
1159 @Override
1160 public boolean canEnterFullImmersion() {
1161 return mState == State.WORKSPACE;
1162 }
1163
1164 @Override
1165 public boolean enterFullImmersion() {
1166 if (mState == State.WORKSPACE) {
1167 // When fully immersed, disregard any touches which fall through.
1168 mDragLayer.setBlockTouch(true);
1169 return true;
1170 }
1171 return false;
1172 }
1173
1174 @Override
1175 public void exitFullImmersion() {
1176 mDragLayer.setBlockTouch(false);
1177 }
1178 }
1179
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001181 if (mLauncherCallbacks != null) {
1182 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001183 } else {
1184 // On devices with a locked orientation, we will at least have the allow rotation
1185 // setting.
1186 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001187 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 }
1189
Adam Cohen9211d422014-10-07 18:14:53 -07001190 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001191 CustomContentCallbacks callbacks, String description) {
1192 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001193 }
1194
Adam Cohenedb40762013-07-18 16:45:45 -07001195 // The custom content needs to offset its content to account for the QSB
1196 public int getTopOffsetForCustomContent() {
1197 return mWorkspace.getPaddingTop();
1198 }
1199
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001200 @Override
1201 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001203 if (mModel.isCurrentCallbacks(this)) {
1204 mModel.stopLoader();
1205 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001206 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1207
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 @Override
1213 public void onWindowFocusChanged(boolean hasFocus) {
1214 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001215 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001216
1217 if (mLauncherCallbacks != null) {
1218 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 private boolean acceptFilter() {
1223 final InputMethodManager inputManager = (InputMethodManager)
1224 getSystemService(Context.INPUT_METHOD_SERVICE);
1225 return !inputManager.isFullscreenMode();
1226 }
1227
1228 @Override
1229 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001230 final int uniChar = event.getUnicodeChar();
1231 final boolean handled = super.onKeyDown(keyCode, event);
1232 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1233 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1235 keyCode, event);
1236 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001238 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // showSearchDialog()
1240 // If there are multiple keystrokes before the search dialog takes focus,
1241 // onSearchRequested() will be called for every keystroke,
1242 // but it is idempotent, so it's fine.
1243 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Joe Onorato8a9625e2010-01-28 15:55:35 -08001247 // Eat the long press event so the keyboard doesn't come up.
1248 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1249 return true;
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return handled;
1253 }
1254
Winson46163472015-09-22 17:31:56 -07001255 @Override
1256 public boolean onKeyUp(int keyCode, KeyEvent event) {
1257 if (keyCode == KeyEvent.KEYCODE_MENU) {
1258 // Ignore the menu key if we are currently dragging or are on the custom content screen
1259 if (!isOnCustomContent() && !mDragController.isDragging()) {
1260 // Close any open folders
1261 closeFolder();
1262
1263 // Stop resizing any widgets
1264 mWorkspace.exitWidgetResizeMode();
1265
1266 // Show the overview mode if we are on the workspace
1267 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1268 !mWorkspace.isSwitchingState()) {
1269 mOverviewPanel.requestFocus();
Winsone9f27272015-10-13 10:47:51 -07001270 showOverviewMode(true, true /* requestButtonFocus */);
Winson46163472015-09-22 17:31:56 -07001271 }
1272 }
1273 return true;
1274 }
1275 return super.onKeyUp(keyCode, event);
1276 }
1277
Karl Rosaen138a0412009-04-23 19:00:21 -07001278 private String getTypedText() {
1279 return mDefaultKeySsb.toString();
1280 }
1281
1282 private void clearTypedText() {
1283 mDefaultKeySsb.clear();
1284 mDefaultKeySsb.clearSpans();
1285 Selection.setSelection(mDefaultKeySsb, 0);
1286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001289 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1290 * State
1291 */
1292 private static State intToState(int stateOrdinal) {
1293 State state = State.WORKSPACE;
1294 final State[] stateValues = State.values();
1295 for (int i = 0; i < stateValues.length; i++) {
1296 if (stateValues[i].ordinal() == stateOrdinal) {
1297 state = stateValues[i];
1298 break;
1299 }
1300 }
1301 return state;
1302 }
1303
1304 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 * Restores the previous state, if it exists.
1306 *
1307 * @param savedState The previous state.
1308 */
1309 private void restoreState(Bundle savedState) {
1310 if (savedState == null) {
1311 return;
1312 }
1313
Michael Jurka883f55b2010-10-21 15:47:14 -07001314 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001315 if (state == State.APPS || state == State.WIDGETS) {
1316 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001317 }
1318
Adam Cohen21cd0022013-10-09 18:57:02 -07001319 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1320 PagedView.INVALID_RESTORE_PAGE);
1321 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001322 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324
Winson Chung3d503fb2011-07-13 17:25:49 -07001325 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001326 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327
Winson Chung3d503fb2011-07-13 17:25:49 -07001328 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1329 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001330 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001331 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1332 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001333 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1334 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001335 AppWidgetProviderInfo info = savedState.getParcelable(
1336 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001337 mPendingAddWidgetInfo = info == null ?
1338 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1339
Adam Cohen4637b5a2013-11-04 18:21:24 -08001340 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001341 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 mRestoring = true;
1343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 /**
1347 * Finds all the views we need and configure them properly.
1348 */
1349 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001350 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001351
Craig Mautner360310b2012-10-26 15:13:08 -07001352 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001353 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001354 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1355 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001356 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001357 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001358
John Spurlock77e1f472013-09-11 10:09:51 -04001359 mLauncherView.setSystemUiVisibility(
1360 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001361 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362
Winson Chung4c98d922011-05-31 16:50:48 -07001363 // Setup the drag layer
1364 mDragLayer.setup(this, dragController);
1365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 // Setup the hotseat
1367 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1368 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001369 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001370 }
1371
Winsone9f27272015-10-13 10:47:51 -07001372 // Setup the overview panel
1373 setupOverviewPanel();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001374
Winson Chung4c98d922011-05-31 16:50:48 -07001375 // Setup the workspace
1376 mWorkspace.setHapticFeedbackEnabled(false);
1377 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001378 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001379 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001380
Winson Chungf0ea4d32011-06-06 14:27:16 -07001381 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001382 mSearchDropTargetBar = (SearchDropTargetBar)
1383 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001384
Winson Chungef7f8742015-06-04 17:18:17 -07001385 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001386 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001387 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001388 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1389 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1390 } else {
1391 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1392 }
Craig Mautner360310b2012-10-26 15:13:08 -07001393
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001395 dragController.setDragScoller(mWorkspace);
1396 dragController.setScrollView(mDragLayer);
1397 dragController.setMoveTarget(mWorkspace);
1398 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001399 if (mSearchDropTargetBar != null) {
1400 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001401 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001402 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001403
Sunny Goyald3060552015-06-25 19:35:49 -07001404 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1405 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Winsone9f27272015-10-13 10:47:51 -07001409 private void setupOverviewPanel() {
1410 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
1411
1412 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1413 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1414 @Override
1415 public boolean onLongClick(View v) {
1416 return v.performClick();
1417 }
1418 };
1419
1420 // Bind wallpaper button actions
1421 View wallpaperButton = findViewById(R.id.wallpaper_button);
1422 wallpaperButton.setOnClickListener(new OnClickListener() {
1423 @Override
1424 public void onClick(View view) {
1425 if (!mWorkspace.isSwitchingState()) {
1426 onClickWallpaperPicker(view);
1427 }
1428 }
1429 });
1430 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
1431 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1432
1433 // Bind widget button actions
1434 mWidgetsButton = findViewById(R.id.widget_button);
1435 mWidgetsButton.setOnClickListener(new OnClickListener() {
1436 @Override
1437 public void onClick(View view) {
1438 if (!mWorkspace.isSwitchingState()) {
1439 onClickAddWidgetButton(view);
1440 }
1441 }
1442 });
1443 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
1444 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1445
1446 // Bind settings actions
1447 View settingsButton = findViewById(R.id.settings_button);
1448 boolean hasSettings = hasSettings();
1449 if (hasSettings) {
1450 settingsButton.setOnClickListener(new OnClickListener() {
1451 @Override
1452 public void onClick(View view) {
1453 if (!mWorkspace.isSwitchingState()) {
1454 onClickSettingsButton(view);
1455 }
1456 }
1457 });
1458 settingsButton.setOnLongClickListener(performClickOnLongClick);
1459 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1460 } else {
1461 settingsButton.setVisibility(View.GONE);
1462 }
1463
1464 mOverviewPanel.setAlpha(0f);
1465 }
1466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 * Sets the all apps button. This method is called from {@link Hotseat}.
1469 */
1470 public void setAllAppsButton(View allAppsButton) {
1471 mAllAppsButton = allAppsButton;
1472 }
1473
1474 public View getAllAppsButton() {
1475 return mAllAppsButton;
1476 }
1477
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001478 public View getWidgetsButton() {
1479 return mWidgetsButton;
1480 }
1481
Anjali Koppal5ad44842014-03-10 20:34:39 -07001482 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 * Creates a view representing a shortcut.
1484 *
1485 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001487 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001488 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 /**
1492 * Creates a view representing a shortcut inflated from the specified resource.
1493 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 * @param parent The group the shortcut belongs to.
1495 * @param info The data structure describing the shortcut.
1496 *
1497 * @return A View inflated from layoutResId.
1498 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001499 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001500 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001501 parent, false);
1502 favorite.applyFromShortcutInfo(info, mIconCache);
1503 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001505 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 return favorite;
1507 }
1508
1509 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 * Add a shortcut to the workspace.
1511 *
1512 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001515 int cellY) {
1516 int[] cellXY = mTmpAddItemCellCoordinates;
1517 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Sunny Goyal5c97f512015-05-19 16:03:28 -07001520 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001521 if (info == null) {
1522 return;
1523 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 final View view = createShortcut(info);
1525
Sunny Goyal5c97f512015-05-19 16:03:28 -07001526 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 // First we check if we already know the exact location where we want to add this item.
1528 if (cellX >= 0 && cellY >= 0) {
1529 cellXY[0] = cellX;
1530 cellXY[1] = cellY;
1531 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001532
1533 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 true, null,null)) {
1536 return;
1537 }
1538 DragObject dragObject = new DragObject();
1539 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1541 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 return;
1543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 foundCellSpan = (result != null);
1548 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 }
1551
1552 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001553 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 return;
1555 }
1556
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001557 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
1564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001566 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001568 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 */
Adam Cohen091440a2015-03-18 14:16:05 -07001570 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001571 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1572
1573 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001574 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001575 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1576 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Adam Cohen59400422014-03-05 18:07:04 -08001579 if (appWidgetInfo.isCustomWidget) {
1580 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001581 }
1582
Adam Cohen59400422014-03-05 18:07:04 -08001583 LauncherAppWidgetInfo launcherInfo;
1584 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1585 launcherInfo.spanX = info.spanX;
1586 launcherInfo.spanY = info.spanY;
1587 launcherInfo.minSpanX = info.minSpanX;
1588 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001589 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001592 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001595 if (hostView == null) {
1596 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001597 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1598 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001599 } else {
1600 // The AppWidgetHostView has already been inflated and instantiated
1601 launcherInfo.hostView = hostView;
1602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001604 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001605 launcherInfo.notifyWidgetSizeChanged(this);
1606
Adam Cohen59400422014-03-05 18:07:04 -08001607 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1608 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001609
1610 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001612 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
Romain Guycbb89e42009-06-08 15:52:54 -07001614
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1616 @Override
1617 public void onReceive(Context context, Intent intent) {
1618 final String action = intent.getAction();
1619 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1620 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001621 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001622 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001623
Winson Chungc100e8e2011-08-09 16:02:43 -07001624 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001625 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001626 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001627 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001628 if (!showWorkspace(false)) {
1629 // If we are already on the workspace, then manually reset all apps
1630 mAppsView.reset();
1631 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001632 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1634 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001635 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001636 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1637 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001638 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001639 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1640 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1641 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001642 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001643 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1644 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646 }
1647 };
1648
1649 @Override
1650 public void onAttachedToWindow() {
1651 super.onAttachedToWindow();
1652
1653 // Listen for broadcasts related to user-presence
1654 final IntentFilter filter = new IntentFilter();
1655 filter.addAction(Intent.ACTION_SCREEN_OFF);
1656 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001657 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001658 if (ENABLE_DEBUG_INTENTS) {
1659 filter.addAction(DebugIntents.DELETE_DATABASE);
1660 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1661 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001662 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001663 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001664 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001665 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 mVisible = true;
1667 }
1668
Adam Cohen0b395352014-06-09 22:54:36 +00001669 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001670 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001671 * This method is a no-op for other platform versions.
1672 */
Sunny Goyald0091012015-01-20 15:55:34 -08001673 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001674 private void setupTransparentSystemBarsForLollipop() {
1675 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001676 Window window = getWindow();
1677 window.getAttributes().systemUiVisibility |=
1678 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1679 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1680 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1681 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1682 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1683 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1684 window.setStatusBarColor(Color.TRANSPARENT);
1685 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001686 }
1687 }
1688
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 @Override
1690 public void onDetachedFromWindow() {
1691 super.onDetachedFromWindow();
1692 mVisible = false;
1693
Adam Cohend113e0c2010-11-11 10:48:05 -08001694 if (mAttached) {
1695 unregisterReceiver(mReceiver);
1696 mAttached = false;
1697 }
Winson Chungb745afb2015-03-02 11:51:23 -08001698 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 }
1700
1701 public void onWindowVisibilityChanged(int visibility) {
1702 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001703 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 // The following code used to be in onResume, but it turns out onResume is called when
1705 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1706 // is a more appropriate event to handle
1707 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001708 if (!mWorkspaceLoading) {
1709 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001710 // We want to let Launcher draw itself at least once before we force it to build
1711 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001712 // apps is nice and speedy.
1713 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001714 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001715 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001716 if (mStarted) return;
1717 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001718 // We delay the layer building a bit in order to give
1719 // other message processing a time to run. In particular
1720 // this avoids a delay in hiding the IME if it was
1721 // currently shown, because doing that may involve
1722 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001723 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001724 final ViewTreeObserver.OnDrawListener listener = this;
1725 mWorkspace.post(new Runnable() {
1726 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001727 if (mWorkspace != null &&
1728 mWorkspace.getViewTreeObserver() != null) {
1729 mWorkspace.getViewTreeObserver().
1730 removeOnDrawListener(listener);
1731 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001732 }
1733 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001734 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001735 }
1736 });
1737 }
1738 clearTypedText();
1739 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741
Adam Cohen091440a2015-03-18 14:16:05 -07001742 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 mHandler.removeMessages(ADVANCE_MSG);
1744 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1745 mHandler.sendMessageDelayed(msg, delay);
1746 mAutoAdvanceSentTime = System.currentTimeMillis();
1747 }
1748
Adam Cohen091440a2015-03-18 14:16:05 -07001749 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1751 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1752 mAutoAdvanceRunning = autoAdvanceRunning;
1753 if (autoAdvanceRunning) {
1754 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1755 sendAdvanceMessage(delay);
1756 } else {
1757 if (!mWidgetsToAdvance.isEmpty()) {
1758 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1759 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1760 }
1761 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001762 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
1764 }
1765 }
1766
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001767 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1768
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001770 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 if (msg.what == ADVANCE_MSG) {
1772 int i = 0;
1773 for (View key: mWidgetsToAdvance.keySet()) {
1774 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1775 final int delay = mAdvanceStagger * i;
1776 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001777 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 public void run() {
1779 ((Advanceable) v).advance();
1780 }
1781 }, delay);
1782 }
1783 i++;
1784 }
1785 sendAdvanceMessage(mAdvanceInterval);
1786 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001787 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001789 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001790
Winsonc0b52fe2015-09-09 16:38:15 -07001791 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001792 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1794 if (v instanceof Advanceable) {
1795 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001796 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001797 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 }
1799 }
1800
Winsonc0b52fe2015-09-09 16:38:15 -07001801 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 if (mWidgetsToAdvance.containsKey(hostView)) {
1803 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001804 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 }
1807
Hyunyoung Song3f471442015-04-08 19:01:34 -07001808 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001809 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1810 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001811 }
1812
Winson Chunga6945242014-01-08 14:04:34 -08001813 public DragLayer getDragLayer() {
1814 return mDragLayer;
1815 }
1816
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001817 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001818 return mAppsView;
1819 }
1820
Hyunyoung Song3f471442015-04-08 19:01:34 -07001821 public WidgetsContainerView getWidgetsView() {
1822 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001823 }
1824
Winson Chunga6945242014-01-08 14:04:34 -08001825 public Workspace getWorkspace() {
1826 return mWorkspace;
1827 }
1828
1829 public Hotseat getHotseat() {
1830 return mHotseat;
1831 }
1832
Jorim Jaggid017f882014-01-14 17:08:48 -08001833 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001834 return mOverviewPanel;
1835 }
1836
Winson Chung006ee262015-08-03 14:40:11 -07001837 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001838 return mSearchDropTargetBar;
1839 }
1840
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001841 public LauncherAppWidgetHost getAppWidgetHost() {
1842 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 }
Romain Guycbb89e42009-06-08 15:52:54 -07001844
Winson Chunga9abd0e2010-10-27 17:18:37 -07001845 public LauncherModel getModel() {
1846 return mModel;
1847 }
1848
Winson Chunga6945242014-01-08 14:04:34 -08001849 protected SharedPreferences getSharedPrefs() {
1850 return mSharedPrefs;
1851 }
1852
Adam Cohen2e6da152015-05-06 11:42:25 -07001853 public DeviceProfile getDeviceProfile() {
1854 return mDeviceProfile;
1855 }
1856
Bjorn Bringertc459e522013-06-07 19:36:01 +01001857 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001858 getWindow().closeAllPanels();
1859
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001860 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001861 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 @Override
1865 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001866 long startTime = 0;
1867 if (DEBUG_RESUME_TIME) {
1868 startTime = System.currentTimeMillis();
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 super.onNewIntent(intent);
1871
1872 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001873 Folder openFolder = mWorkspace.getOpenFolder();
1874 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1875 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1876 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1877 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1878 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001879 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001880 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001881
Adam Cohen6fecd412013-10-02 17:41:50 -07001882 if (mWorkspace == null) {
1883 // Can be cases where mWorkspace is null, this prevents a NPE
1884 return;
1885 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001886 // In all these cases, only animate if we're already on home
1887 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001888
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 closeFolder();
1890 exitSpringLoadedDragMode();
1891
1892 // If we are already on home, then just animate back to the workspace,
1893 // otherwise, just wait until onResume to set the state back to Workspace
1894 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001895 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001896 } else {
1897 mOnResumeState = State.WORKSPACE;
1898 }
1899
1900 final View v = getWindow().peekDecorView();
1901 if (v != null && v.getWindowToken() != null) {
1902 InputMethodManager imm = (InputMethodManager)getSystemService(
1903 INPUT_METHOD_SERVICE);
1904 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1905 }
1906
Winson Chungb745afb2015-03-02 11:51:23 -08001907 // Reset the apps view
1908 if (!alreadyOnHome && mAppsView != null) {
1909 mAppsView.scrollToTop();
1910 }
1911
Hyunyoung Song3f471442015-04-08 19:01:34 -07001912 // Reset the widgets view
1913 if (!alreadyOnHome && mWidgetsView != null) {
1914 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001915 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001916
Adam Cohen9211d422014-10-07 18:14:53 -07001917 if (mLauncherCallbacks != null) {
1918 mLauncherCallbacks.onHomeIntent();
1919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
Adam Cohened307df2013-10-02 09:37:31 -07001921
Adam Cohen9211d422014-10-07 18:14:53 -07001922 if (mLauncherCallbacks != null) {
1923 mLauncherCallbacks.onNewIntent(intent);
1924 }
Winson15f8b172015-08-19 11:02:39 -07001925
1926 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1927 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1928 // animation.
1929 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001930 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1931 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001932 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1933 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001934
1935 // We use this flag to suppress noisy callbacks above custom content state
1936 // from onResume.
1937 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001938 mWorkspace.post(new Runnable() {
1939 @Override
1940 public void run() {
1941 mWorkspace.moveToDefaultScreen(true);
1942 }
1943 });
1944 }
1945 }
1946
1947 if (DEBUG_RESUME_TIME) {
1948 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1949 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001950 }
1951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001953 public void onRestoreInstanceState(Bundle state) {
1954 super.onRestoreInstanceState(state);
1955 for (int page: mSynchronouslyBoundPages) {
1956 mWorkspace.restoreInstanceStateForChild(page);
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959
1960 @Override
1961 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001962 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001963 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1964 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001965 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001966 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Michael Jurka883f55b2010-10-21 15:47:14 -07001968 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001969 // We close any open folder since it will not be re-opened, and we need to make sure
1970 // this state is reflected.
1971 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 mWaitingForResult) {
1975 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1978 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001979 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1980 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1981 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001982 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
Hyunyoung Song3f471442015-04-08 19:01:34 -07001985 // Save the current widgets tray?
1986 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001987
1988 if (mLauncherCallbacks != null) {
1989 mLauncherCallbacks.onSaveInstanceState(outState);
1990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992
1993 @Override
1994 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Winson Chunge7a03942011-08-05 15:05:12 -07001997 // Remove all pending runnables
1998 mHandler.removeMessages(ADVANCE_MSG);
1999 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002000 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002001
Winson Chungcd2b0142011-06-08 16:02:26 -07002002 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002003 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002004
2005 // It's possible to receive onDestroy after a new Launcher activity has
2006 // been created. In this case, don't interfere with the new Launcher.
2007 if (mModel.isCurrentCallbacks(this)) {
2008 mModel.stopLoader();
2009 app.setLauncher(null);
2010 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002013 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002015 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002017 mAppWidgetHost = null;
2018
2019 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020
2021 TextKeyListener.getInstance().release();
2022
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002023 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002024
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002025 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002026 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002027 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002028 mWorkspace = null;
2029 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002030
Michael Jurka2ecf9952012-06-18 12:52:28 -07002031 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002032
2033 if (mLauncherCallbacks != null) {
2034 mLauncherCallbacks.onDestroy();
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
Adam Cohena9cf38f2011-05-02 15:36:58 -07002038 public DragController getDragController() {
2039 return mDragController;
2040 }
2041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
2043 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002044 onStartForResult(requestCode);
2045 super.startActivityForResult(intent, requestCode);
2046 }
2047
2048 @Override
2049 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2050 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2051 onStartForResult(requestCode);
2052 try {
2053 super.startIntentSenderForResult(intent, requestCode,
2054 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2055 } catch (IntentSender.SendIntentException e) {
2056 throw new ActivityNotFoundException();
2057 }
2058 }
2059
2060 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002061 if (requestCode >= 0) {
2062 setWaitingForResult(true);
2063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065
Winson Chung70d72102011-08-12 11:24:35 -07002066 /**
2067 * Indicates that we want global search for this activity by setting the globalSearch
2068 * argument for {@link #startSearch} to true.
2069 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002071 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002073
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 if (initialQuery == null) {
2075 // Use any text typed in the launcher as the initial query
2076 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 if (appSearchData == null) {
2079 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002080 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
Winson Chungadf0c182012-08-23 14:59:07 -07002082 Rect sourceBounds = new Rect();
2083 if (mSearchDropTargetBar != null) {
2084 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2085 }
Romain Guycbb89e42009-06-08 15:52:54 -07002086
Ian Parkinson047036e2014-09-01 15:40:53 +01002087 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002089 if (clearTextImmediately) {
2090 clearTypedText();
2091 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002092
2093 // We need to show the workspace after starting the search
2094 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002095 }
2096
Ian Parkinson047036e2014-09-01 15:40:53 +01002097 /**
2098 * Start a text search.
2099 *
2100 * @return {@code true} if the search will start immediately, so any further keypresses
2101 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2102 * to buffer keypresses.
2103 */
2104 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002105 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002106 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2107 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2108 sourceBounds);
2109 }
2110
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002112 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002113 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002114 }
2115
2116 /**
2117 * Starts the global search activity. This code is a copied from SearchManager
2118 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002119 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002120 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002121 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002122 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2123 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2124 if (globalSearchActivity == null) {
2125 Log.w(TAG, "No global search activity found.");
2126 return;
2127 }
2128 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2129 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2130 intent.setComponent(globalSearchActivity);
2131 // Make sure that we have a Bundle to put source in
2132 if (appSearchData == null) {
2133 appSearchData = new Bundle();
2134 } else {
2135 appSearchData = new Bundle(appSearchData);
2136 }
Adam Cohen9211d422014-10-07 18:14:53 -07002137 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 if (!appSearchData.containsKey("source")) {
2139 appSearchData.putString("source", getPackageName());
2140 }
2141 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2142 if (!TextUtils.isEmpty(initialQuery)) {
2143 intent.putExtra(SearchManager.QUERY, initialQuery);
2144 }
2145 if (selectInitialQuery) {
2146 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2147 }
2148 intent.setSourceBounds(sourceBounds);
2149 try {
2150 startActivity(intent);
2151 } catch (ActivityNotFoundException ex) {
2152 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
2155
Winson Chungbedf9232015-07-10 12:38:30 -07002156 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2157 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2158 return;
2159 }
2160
2161 // If not handled, then just start the provided search intent
2162 startActivitySafely(v, searchIntent, null);
2163 }
2164
Yura4f93ec62014-02-04 14:15:21 +00002165 public boolean isOnCustomContent() {
2166 return mWorkspace.isOnOrMovingToCustomContent();
2167 }
2168
Winson Chung70d72102011-08-12 11:24:35 -07002169 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002170 public boolean onPrepareOptionsMenu(Menu menu) {
2171 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002172 if (mLauncherCallbacks != null) {
2173 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2174 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002175 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002178 @Override
2179 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002180 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002181 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002182 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002183 }
2184
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 public boolean isWorkspaceLocked() {
2186 return mWorkspaceLoading || mWaitingForResult;
2187 }
2188
Adam Cohen517a7f52014-03-01 12:12:59 -08002189 public boolean isWorkspaceLoading() {
2190 return mWorkspaceLoading;
2191 }
2192
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002193 private void setWorkspaceLoading(boolean value) {
2194 boolean isLocked = isWorkspaceLocked();
2195 mWorkspaceLoading = value;
2196 if (isLocked != isWorkspaceLocked()) {
2197 onWorkspaceLockedChanged();
2198 }
2199 }
2200
2201 private void setWaitingForResult(boolean value) {
2202 boolean isLocked = isWorkspaceLocked();
2203 mWaitingForResult = value;
2204 if (isLocked != isWorkspaceLocked()) {
2205 onWorkspaceLockedChanged();
2206 }
2207 }
2208
Adam Cohen9211d422014-10-07 18:14:53 -07002209 protected void onWorkspaceLockedChanged() {
2210 if (mLauncherCallbacks != null) {
2211 mLauncherCallbacks.onWorkspaceLockedChanged();
2212 }
2213 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002214
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002216 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002217 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002218 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2219 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002220 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002221 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002222 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002223
Sunny Goyale6b63a32015-01-16 16:14:29 -08002224 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002225 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002226 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2227 }
2228
Sunny Goyale6b63a32015-01-16 16:14:29 -08002229 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002230 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2231 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002232 if (appWidgetInfo.configure != null) {
2233 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002234 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002235
Bjorn Bringert7984c942009-12-09 15:38:25 +00002236 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002237 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2238 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002241 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002242 Runnable onComplete = new Runnable() {
2243 @Override
2244 public void run() {
2245 // Exit spring loaded mode if necessary after adding the widget
2246 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2247 null);
2248 }
2249 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002251 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002252 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 }
2254 }
Romain Guycbb89e42009-06-08 15:52:54 -07002255
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002256 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002257 // Close any folders that may be open.
2258 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002259 mWorkspace.moveToCustomContentScreen(animate);
2260 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002261
2262 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2263 int[] cell, int spanX, int spanY) {
2264 switch (info.itemType) {
2265 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2266 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2267 int span[] = new int[2];
2268 span[0] = spanX;
2269 span[1] = spanY;
2270 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2271 container, screenId, cell, span);
2272 break;
2273 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2274 processShortcutFromDrop(info.componentName, container, screenId, cell);
2275 break;
2276 default:
2277 throw new IllegalStateException("Unknown item type: " + info.itemType);
2278 }
2279 }
2280
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 /**
2282 * Process a shortcut drop.
2283 *
2284 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002285 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002288 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2289 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002290 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002293 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294
2295 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 mPendingAddInfo.cellX = cell[0];
2297 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002298 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002299
2300 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2301 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002302 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002303 }
2304
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 /**
2306 * Process a widget drop.
2307 *
2308 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002310 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002312 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2313 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002317 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002318 mPendingAddInfo.minSpanX = info.minSpanX;
2319 mPendingAddInfo.minSpanY = info.minSpanY;
2320
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 mPendingAddInfo.cellX = cell[0];
2323 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002325 if (span != null) {
2326 mPendingAddInfo.spanX = span[0];
2327 mPendingAddInfo.spanY = span[1];
2328 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329
Adam Cohened66b2b2012-01-23 17:28:51 -08002330 AppWidgetHostView hostView = info.boundWidget;
2331 int appWidgetId;
2332 if (hostView != null) {
2333 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002334 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002335
2336 // Clear the boundWidget so that it doesn't get destroyed.
2337 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002338 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002339 // In this case, we either need to start an activity to get permission to bind
2340 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002341 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002342 Bundle options = info.bindOptions;
2343
Sunny Goyalffe83f12014-08-14 17:39:34 -07002344 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2345 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002346 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002347 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002348 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002350 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2351 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2352 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002353 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2354 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002355 // TODO: we need to make sure that this accounts for the options bundle.
2356 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002357 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2358 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002359 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 }
2361
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002364 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 folderInfo.title = getText(R.string.folder_name);
2366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002368 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2369 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002370 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371
2372 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 FolderIcon newFolder =
2374 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002375 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002377 // Force measure the new folder icon
2378 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2379 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002380 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 }
Romain Guycbb89e42009-06-08 15:52:54 -07002382
Winsonc0b52fe2015-09-09 16:38:15 -07002383 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002384 * Unbinds the view for the specified item, and removes the item and all its children.
2385 *
2386 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002387 * @param itemInfo the {@link ItemInfo} for this view.
2388 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002389 */
Winson2949fb52015-09-24 09:56:11 -07002390 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002391 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002392 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002393 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2394 if (folderInfo != null) {
2395 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002396 } else {
2397 mWorkspace.removeWorkspaceItem(v);
2398 }
Winsonc0b52fe2015-09-09 16:38:15 -07002399 if (deleteFromDb) {
2400 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2401 }
2402 } else if (itemInfo instanceof FolderInfo) {
2403 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2404 unbindFolder(folderInfo);
2405 mWorkspace.removeWorkspaceItem(v);
2406 if (deleteFromDb) {
2407 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2408 }
2409 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2410 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002411 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002412 removeWidgetToAutoAdvance(widgetInfo.hostView);
2413 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002414 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002415 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002416 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002417
Winsonc0b52fe2015-09-09 16:38:15 -07002418 } else {
2419 return false;
2420 }
2421 return true;
2422 }
2423
2424 /**
2425 * Unbinds any launcher references to the folder.
2426 */
2427 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002428 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 }
2430
Winsonc0b52fe2015-09-09 16:38:15 -07002431 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002432 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002433 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002434 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002435 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002436 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002437 // Deleting an app widget ID is a void call but writes to disk before returning
2438 // to the caller...
2439 new AsyncTask<Void, Void, Void>() {
2440 public Void doInBackground(Void ... args) {
2441 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2442 return null;
2443 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002444 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002445 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002446 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 @Override
2450 public boolean dispatchKeyEvent(KeyEvent event) {
2451 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2452 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002455 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002456 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002457 dumpState();
2458 return true;
2459 }
2460 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002461 }
2462 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2463 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002464 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 return true;
2466 }
2467 }
2468
2469 return super.dispatchKeyEvent(event);
2470 }
2471
Joe Onorato88ec0992009-11-19 13:16:06 -08002472 @Override
2473 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002474 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2475 return;
2476 }
2477
Sunny Goyal45478022015-06-08 16:52:41 -07002478 if (mDragController.isDragging()) {
2479 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002480 return;
2481 }
2482
Winson Chungb745afb2015-03-02 11:51:23 -08002483 if (isAppsViewVisible()) {
2484 showWorkspace(true);
2485 } else if (isWidgetsViewVisible()) {
2486 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002487 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002488 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002489 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002490 Folder openFolder = mWorkspace.getOpenFolder();
2491 if (openFolder.isEditingName()) {
2492 openFolder.dismissEditingName();
2493 } else {
2494 closeFolder();
2495 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002496 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002497 mWorkspace.exitWidgetResizeMode();
2498
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002499 // Back button is a no-op here, but give at least some feedback for the button press
2500 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002501 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002502 }
2503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002505 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002507 @Override
2508 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002509 if (mAppWidgetHost != null) {
2510 mAppWidgetHost.startListening();
2511 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002512 }
2513
2514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 * Launches the intent referred by the clicked shortcut.
2516 *
2517 * @param v The view representing the clicked shortcut.
2518 */
2519 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2521 // view has detached (it's possible for this to happen if the view is removed mid touch).
2522 if (v.getWindowToken() == null) {
2523 return;
2524 }
2525
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002526 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002527 return;
2528 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002529
Adam Cohen1697b792013-09-17 19:08:21 -07002530 if (v instanceof Workspace) {
2531 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 return;
2535 }
2536
2537 if (v instanceof CellLayout) {
2538 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002539 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002540 }
2541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002544 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002547 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002552 } else if (tag instanceof AppInfo) {
2553 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 } else if (tag instanceof LauncherAppWidgetInfo) {
2555 if (v instanceof PendingAppWidgetHostView) {
2556 onClickPendingWidget((PendingAppWidgetHostView) v);
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559 }
2560
Sunny Goyal70660032015-05-14 00:07:08 -07002561 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002562 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002563 return false;
2564 }
2565
Michael Jurkaaf442092010-06-10 17:01:57 -07002566 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002567 * Event handler for the app widget view which has not fully restored.
2568 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002569 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002570 if (mIsSafeModeEnabled) {
2571 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2572 return;
2573 }
2574
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002576 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002577 int widgetId = info.appWidgetId;
2578 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2579 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002580 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2581 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002582 mPendingAddInfo.copyFrom(info);
2583 mPendingAddWidgetId = widgetId;
2584
Sunny Goyalffe83f12014-08-14 17:39:34 -07002585 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2586 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002587 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002588 } else if (info.installProgress < 0) {
2589 // The install has not been queued
2590 final String packageName = info.providerName.getPackageName();
2591 showBrokenAppInstallDialog(packageName,
2592 new DialogInterface.OnClickListener() {
2593 public void onClick(DialogInterface dialog, int id) {
2594 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2595 }
2596 });
2597 } else {
2598 // Download has started.
2599 final String packageName = info.providerName.getPackageName();
2600 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002601 }
2602 }
2603
2604 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002605 * Event handler for the "grid" button that appears on the home screen, which
2606 * enters all apps mode.
2607 *
2608 * @param v The view that was clicked.
2609 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 protected void onClickAllAppsButton(View v) {
2611 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002612 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002613 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002614 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002615
2616 if (mLauncherCallbacks != null) {
2617 mLauncherCallbacks.onClickAllAppsButton(v);
2618 }
Winson Chung76648c52015-07-10 14:33:23 -07002619 }
2620 }
2621
2622 protected void onLongClickAllAppsButton(View v) {
2623 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2624 if (!isAppsViewVisible()) {
2625 showAppsView(true /* animated */, false /* resetListToTop */,
2626 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 }
2628 }
2629
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 private void showBrokenAppInstallDialog(final String packageName,
2631 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002632 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 .setTitle(R.string.abandoned_promises_title)
2634 .setMessage(R.string.abandoned_promise_explanation)
2635 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2636 .setNeutralButton(R.string.abandoned_clean_this,
2637 new DialogInterface.OnClickListener() {
2638 public void onClick(DialogInterface dialog, int id) {
2639 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2640 mWorkspace.removeAbandonedPromise(packageName, user);
2641 }
2642 })
2643 .create().show();
2644 return;
2645 }
2646
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647 /**
2648 * Event handler for an app shortcut click.
2649 *
2650 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2651 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2654 Object tag = v.getTag();
2655 if (!(tag instanceof ShortcutInfo)) {
2656 throw new IllegalArgumentException("Input must be a Shortcut");
2657 }
2658
2659 // Open shortcut
2660 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002661
2662 if (shortcut.isDisabled != 0) {
2663 int error = R.string.activity_not_available;
2664 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2665 error = R.string.safemode_shortcut_error;
2666 }
2667 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2668 return;
2669 }
2670
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002672 if ((v instanceof BubbleTextView)
2673 && shortcut.isPromise()
2674 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002675 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002676 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 new DialogInterface.OnClickListener() {
2678 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 return;
2683 }
2684
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002686 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002687
2688 if (mLauncherCallbacks != null) {
2689 mLauncherCallbacks.onClickAppShortcut(v);
2690 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 }
2692
Adam Cohen091440a2015-03-18 14:16:05 -07002693 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002695 final ShortcutInfo shortcut;
2696 final Intent intent;
2697 if (tag instanceof ShortcutInfo) {
2698 shortcut = (ShortcutInfo) tag;
2699 intent = shortcut.intent;
2700 int[] pos = new int[2];
2701 v.getLocationOnScreen(pos);
2702 intent.setSourceBounds(new Rect(pos[0], pos[1],
2703 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002704
Sunny Goyal508da152014-08-14 10:53:27 -07002705 } else if (tag instanceof AppInfo) {
2706 shortcut = null;
2707 intent = ((AppInfo) tag).intent;
2708 } else {
2709 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2710 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711
2712 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002713 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714
2715 if (success && v instanceof BubbleTextView) {
2716 mWaitingForResume = (BubbleTextView) v;
2717 mWaitingForResume.setStayPressed(true);
2718 }
2719 }
2720
2721 /**
2722 * Event handler for a folder icon click.
2723 *
2724 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2725 */
2726 protected void onClickFolderIcon(View v) {
2727 if (LOGD) Log.d(TAG, "onClickFolder");
2728 if (!(v instanceof FolderIcon)){
2729 throw new IllegalArgumentException("Input must be a FolderIcon");
2730 }
2731
Sunny Goyal317698b2015-07-29 11:45:41 -07002732 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 FolderIcon folderIcon = (FolderIcon) v;
2734 final FolderInfo info = folderIcon.getFolderInfo();
2735 Folder openFolder = mWorkspace.getFolderForTag(info);
2736
2737 // If the folder info reports that the associated folder is open, then verify that
2738 // it is actually opened. There have been a few instances where this gets out of sync.
2739 if (info.opened && openFolder == null) {
2740 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2741 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2742 info.opened = false;
2743 }
2744
2745 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2746 // Close any open folder
2747 closeFolder();
2748 // Open the requested folder
2749 openFolder(folderIcon);
2750 } else {
2751 // Find the open folder...
2752 int folderScreen;
2753 if (openFolder != null) {
2754 folderScreen = mWorkspace.getPageForView(openFolder);
2755 // .. and close it
2756 closeFolder(openFolder);
2757 if (folderScreen != mWorkspace.getCurrentPage()) {
2758 // Close any folder open on the current screen
2759 closeFolder();
2760 // Pull the folder onto this screen
2761 openFolder(folderIcon);
2762 }
2763 }
2764 }
Adam Cohen9211d422014-10-07 18:14:53 -07002765
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onClickFolderIcon(v);
2768 }
Michael Jurka5130e402011-10-13 04:55:35 -07002769 }
2770
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002771 /**
2772 * Event handler for the (Add) Widgets button that appears after a long press
2773 * on the home screen.
2774 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002775 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002777 if (mIsSafeModeEnabled) {
2778 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2779 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002780 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002781 if (mLauncherCallbacks != null) {
2782 mLauncherCallbacks.onClickAddWidgetButton(view);
2783 }
Adam Cohen9211d422014-10-07 18:14:53 -07002784 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002785 }
2786
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 /**
2788 * Event handler for the wallpaper picker button that appears after a long press
2789 * on the home screen.
2790 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002791 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham3e776a82015-09-09 18:21:49 -07002793 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2794 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2795 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2796 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002797 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002798
2799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickWallpaperPicker(v);
2801 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 }
2803
2804 /**
2805 * Event handler for a click on the settings button that appears after a long press
2806 * on the home screen.
2807 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002808 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002812 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002813 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002814 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002815 }
2816
Adam Cohen61f560d2013-09-30 15:58:20 -07002817 public View.OnTouchListener getHapticFeedbackTouchListener() {
2818 if (mHapticFeedbackTouchListener == null) {
2819 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002820 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002821 @Override
2822 public boolean onTouch(View v, MotionEvent event) {
2823 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2825 }
2826 return false;
2827 }
2828 };
2829 }
2830 return mHapticFeedbackTouchListener;
2831 }
2832
Adam Cohen9211d422014-10-07 18:14:53 -07002833 public void onDragStarted(View view) {
2834 if (isOnCustomContent()) {
2835 // Custom content screen doesn't participate in drag and drop. If on custom
2836 // content screen, move to default.
2837 moveWorkspaceToDefaultScreen();
2838 }
2839
2840 if (mLauncherCallbacks != null) {
2841 mLauncherCallbacks.onDragStarted(view);
2842 }
2843 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002844
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002845 /**
2846 * Called when the user stops interacting with the launcher.
2847 * This implies that the user is now on the homescreen and is not doing housekeeping.
2848 */
Adam Cohen9211d422014-10-07 18:14:53 -07002849 protected void onInteractionEnd() {
2850 if (mLauncherCallbacks != null) {
2851 mLauncherCallbacks.onInteractionEnd();
2852 }
2853 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002854
2855 /**
2856 * Called when the user starts interacting with the launcher.
2857 * The possible interactions are:
2858 * - open all apps
2859 * - reorder an app shortcut, or a widget
2860 * - open the overview mode.
2861 * This is a good time to stop doing things that only make sense
2862 * when the user is on the homescreen and not doing housekeeping.
2863 */
Adam Cohen9211d422014-10-07 18:14:53 -07002864 protected void onInteractionBegin() {
2865 if (mLauncherCallbacks != null) {
2866 mLauncherCallbacks.onInteractionBegin();
2867 }
2868 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002869
Winson Chungcd99cd32015-04-29 11:03:24 -07002870 /** Updates the interaction state. */
2871 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002872 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002873 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002874 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2875 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002876 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002877 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002878 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002879 onInteractionEnd();
2880 }
2881 }
2882
Kenny Guyf07af7b2014-07-31 11:39:16 +01002883 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002884 try {
2885 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002886 launcherApps.showAppDetailsForProfile(componentName, user);
2887 } catch (SecurityException e) {
2888 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2889 Log.e(TAG, "Launcher does not have permission to launch settings");
2890 } catch (ActivityNotFoundException e) {
2891 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2892 Log.e(TAG, "Unable to launch settings");
2893 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002894 }
2895
Michael Jurka1e2f4652013-07-08 18:03:46 -07002896 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002897 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2898 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002899 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002900 // System applications cannot be installed. For now, show a toast explaining that.
2901 // We may give them the option of disabling apps this way.
2902 int messageId = R.string.uninstall_system_app_text;
2903 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002904 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002905 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002906 String packageName = componentName.getPackageName();
2907 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 Intent intent = new Intent(
2909 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002910 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2911 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002912 if (user != null) {
2913 user.addToIntent(intent, Intent.EXTRA_USER);
2914 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002915 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002916 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002917 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002918 }
2919
Winson Chung8f1eff72015-05-28 17:33:40 -07002920 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002921 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002923 // Only launch using the new animation if the shortcut has not opted out (this is a
2924 // private contract between launcher and may be ignored in the future).
2925 boolean useLaunchAnimation = (v != null) &&
2926 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002927 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2928 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002929
Kenny Guy1317e2d2014-05-08 18:52:50 +01002930 UserHandleCompat user = null;
2931 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2932 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2933 user = userManager.getUserForSerialNumber(serialNumber);
2934 }
2935
2936 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002937 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002938 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002939 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002940 int left = 0, top = 0;
2941 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2942 if (v instanceof TextView) {
2943 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002944 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2945 if (icon != null) {
2946 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002947 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002948 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002949 width = bounds.width();
2950 height = bounds.height();
2951 }
2952 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002953 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2954 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002955 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002956 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002957 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002958 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002959 // On L devices, we use the device default slide-up transition.
2960 // On L MR1 devices, we a custom version of the slide-up transition which
2961 // doesn't have the delay present in the device default.
2962 opts = ActivityOptions.makeCustomAnimation(this,
2963 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002964 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002965 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002966 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002967
2968 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2969 // Could be launching some bookkeeping activity
2970 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002971 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002972 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002973 launcherApps.startActivityForProfile(intent.getComponent(), user,
2974 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002975 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002976 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 } catch (SecurityException e) {
2978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002979 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002981 "or use the exported attribute for this activity. "
2982 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002984 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002986
Winson Chungbedf9232015-07-10 12:38:30 -07002987 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002988 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002989 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2990 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2991 return false;
2992 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002993 try {
2994 success = startActivity(v, intent, tag);
2995 } catch (ActivityNotFoundException e) {
2996 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2997 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2998 }
2999 return success;
3000 }
3001
Adam Cohen268c4752012-06-06 17:47:33 -07003002 /**
3003 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3004 * in the DragLayer in the exact absolute location of the original FolderIcon.
3005 */
3006 private void copyFolderIconToImage(FolderIcon fi) {
3007 final int width = fi.getMeasuredWidth();
3008 final int height = fi.getMeasuredHeight();
3009
3010 // Lazy load ImageView, Bitmap and Canvas
3011 if (mFolderIconImageView == null) {
3012 mFolderIconImageView = new ImageView(this);
3013 }
3014 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3015 mFolderIconBitmap.getHeight() != height) {
3016 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3017 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3018 }
3019
3020 DragLayer.LayoutParams lp;
3021 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3022 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3023 } else {
3024 lp = new DragLayer.LayoutParams(width, height);
3025 }
3026
Adam Cohen307fe232012-08-16 17:55:58 -07003027 // The layout from which the folder is being opened may be scaled, adjust the starting
3028 // view size by this scale factor.
3029 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003030 lp.customPosition = true;
3031 lp.x = mRectForFolderAnimation.left;
3032 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003033 lp.width = (int) (scale * width);
3034 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003035
3036 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3037 fi.draw(mFolderIconCanvas);
3038 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003039 if (fi.getFolder() != null) {
3040 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3041 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003042 }
Adam Cohen268c4752012-06-06 17:47:33 -07003043 // Just in case this image view is still in the drag layer from a previous animation,
3044 // we remove it and re-add it.
3045 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3046 mDragLayer.removeView(mFolderIconImageView);
3047 }
3048 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003049 if (fi.getFolder() != null) {
3050 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003051 }
Adam Cohen268c4752012-06-06 17:47:33 -07003052 }
3053
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003055 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3057 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3058 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3059
Adam Cohenc51934b2011-07-26 21:07:43 -07003060 FolderInfo info = (FolderInfo) fi.getTag();
3061 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3062 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003063 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3064 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003065 }
3066
Adam Cohen268c4752012-06-06 17:47:33 -07003067 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3068 copyFolderIconToImage(fi);
3069 fi.setVisibility(View.INVISIBLE);
3070
Michael Jurka2ecf9952012-06-18 12:52:28 -07003071 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003072 scaleX, scaleY);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003073 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003074 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3075 }
3076 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003077 oa.start();
3078 }
3079
Adam Cohen268c4752012-06-06 17:47:33 -07003080 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003081 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003082 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3083 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3084 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3085
Adam Cohen268c4752012-06-06 17:47:33 -07003086 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003087
Adam Cohen268c4752012-06-06 17:47:33 -07003088 // We remove and re-draw the FolderIcon in-case it has changed
3089 mDragLayer.removeView(mFolderIconImageView);
3090 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003091 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003092 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003093 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003094 oa.addListener(new AnimatorListenerAdapter() {
3095 @Override
3096 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003097 if (cl != null) {
3098 cl.clearFolderLeaveBehind();
3099 // Remove the ImageView copy of the FolderIcon and make the original visible.
3100 mDragLayer.removeView(mFolderIconImageView);
3101 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003102 }
3103 }
3104 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003105 oa.start();
3106 }
3107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003109 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 * is animated relative to the specified View. If the View is null, no animation
3111 * is played.
3112 *
3113 * @param folderInfo The FolderInfo describing the folder to open.
3114 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003115 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003116 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003117 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3118 if (openFolder != null && openFolder != folder) {
3119 // Close any open folder before opening a folder.
3120 closeFolder();
3121 }
3122
Adam Cohena9cf38f2011-05-02 15:36:58 -07003123 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124
Adam Cohena9cf38f2011-05-02 15:36:58 -07003125 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126
Sunny Goyalf4066152015-04-15 09:42:19 -07003127 // While the folder is open, the position of the icon cannot change.
3128 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3129
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 // Just verify that the folder hasn't already been added to the DragLayer.
3131 // There was a one-off crash where the folder had a parent already.
3132 if (folder.getParent() == null) {
3133 mDragLayer.addView(folder);
3134 mDragController.addDropTarget((DropTarget) folder);
3135 } else {
3136 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3137 folder.getParent() + ").");
3138 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003139 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003140 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003141
3142 // Notify the accessibility manager that this folder "window" has appeared and occluded
3143 // the workspace items
3144 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3145 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003146 }
3147
3148 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003149 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003150 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003151 if (folder.isEditingName()) {
3152 folder.dismissEditingName();
3153 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003154 closeFolder(folder);
3155 }
3156 }
3157
Sunny Goyal83a8f042015-05-19 12:52:12 -07003158 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003159 folder.getInfo().opened = false;
3160
3161 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3162 if (parent != null) {
3163 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3164 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003165 if (fi != null) {
3166 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3167 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003168 }
3169 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003170
3171 // Notify the accessibility manager that this folder "window" has disappeard and no
3172 // longer occludeds the workspace items
3173 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
3175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003177 if (!isDraggingEnabled()) return false;
3178 if (isWorkspaceLocked()) return false;
3179 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180
Winson Chung76648c52015-07-10 14:33:23 -07003181 if (v == mAllAppsButton) {
3182 onLongClickAllAppsButton(v);
3183 return true;
3184 }
3185
Adam Cohen1697b792013-09-17 19:08:21 -07003186 if (v instanceof Workspace) {
3187 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003188 if (!mWorkspace.isTouchActive()) {
3189 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3192 return true;
3193 } else {
3194 return false;
3195 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003196 } else {
3197 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003198 }
Adam Cohen1697b792013-09-17 19:08:21 -07003199 }
3200
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003201 CellLayout.CellInfo longClickCellInfo = null;
3202 View itemUnderLongClick = null;
3203 if (v.getTag() instanceof ItemInfo) {
3204 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003205 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003206 itemUnderLongClick = longClickCellInfo.cell;
3207 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 }
3209
Winson Chung3d503fb2011-07-13 17:25:49 -07003210 // The hotseat touch handling does not go through Workspace, and we always allow long press
3211 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003212 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003213 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003214 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003215 // User long pressed on empty space
3216 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3217 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003218 if (mWorkspace.isInOverviewMode()) {
3219 mWorkspace.startReordering(v);
3220 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003221 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003224 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3225 mHotseat.getOrderInHotseat(
3226 longClickCellInfo.cellX,
3227 longClickCellInfo.cellY));
3228 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003230 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 }
3232 }
3233 }
3234 return true;
3235 }
3236
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 boolean isHotseatLayout(View layout) {
3238 return mHotseat != null && layout != null &&
3239 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3240 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003241
Winson Chung3d503fb2011-07-13 17:25:49 -07003242 /**
3243 * Returns the CellLayout of the specified container at the specified screen.
3244 */
Sunny Goyal92820592015-03-02 11:31:35 -08003245 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3247 if (mHotseat != null) {
3248 return mHotseat.getLayout();
3249 } else {
3250 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003251 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003252 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003253 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003254 }
3255 }
3256
Winson Chungb745afb2015-03-02 11:51:23 -08003257 /**
3258 * For overridden classes.
3259 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003260 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003261 return isAppsViewVisible();
3262 }
3263
3264 public boolean isAppsViewVisible() {
3265 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3266 }
3267
3268 public boolean isWidgetsViewVisible() {
3269 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003270 }
3271
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003272 private void setWorkspaceBackground(int background) {
3273 switch (background) {
3274 case WORKSPACE_BACKGROUND_TRANSPARENT:
3275 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3276 break;
3277 case WORKSPACE_BACKGROUND_BLACK:
3278 getWindow().setBackgroundDrawable(null);
3279 break;
3280 default:
3281 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3282 }
Craig Mautner360310b2012-10-26 15:13:08 -07003283 }
3284
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003285 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003286 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3287 int curflags = getWindow().getAttributes().flags
3288 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3289 if (wpflags != curflags) {
3290 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3291 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003292 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003293 }
3294
Michael Jurkae326f182011-11-21 14:05:46 -08003295 @Override
3296 public void onTrimMemory(int level) {
3297 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003298 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3299 // The widget preview db can result in holding onto over
3300 // 3MB of memory for caching which isn't necessary.
3301 SQLiteDatabase.releaseMemory();
3302
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003303 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003304 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003305 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003306 if (mLauncherCallbacks != null) {
3307 mLauncherCallbacks.onTrimMemory(level);
3308 }
Michael Jurkae326f182011-11-21 14:05:46 -08003309 }
3310
Winson5c6bdbb2015-09-03 11:36:19 -07003311 /**
3312 * @return whether or not the Launcher state changed.
3313 */
3314 public boolean showWorkspace(boolean animated) {
3315 return showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003316 }
3317
Winson5c6bdbb2015-09-03 11:36:19 -07003318 /**
3319 * @return whether or not the Launcher state changed.
3320 */
3321 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3322 return showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003323 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003324 }
3325
Winson5c6bdbb2015-09-03 11:36:19 -07003326 /**
3327 * @return whether or not the Launcher state changed.
3328 */
3329 protected boolean showWorkspace(int snapToPage, boolean animated) {
3330 return showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003331 }
3332
Winson5c6bdbb2015-09-03 11:36:19 -07003333 /**
3334 * @return whether or not the Launcher state changed.
3335 */
3336 boolean showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003337 boolean changed = mState != State.WORKSPACE ||
3338 mWorkspace.getState() != Workspace.State.NORMAL;
3339 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003340 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003341 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3342 Workspace.State.NORMAL, snapToPage, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003343
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 // Set focus to the AppsCustomize button
3345 if (mAllAppsButton != null) {
3346 mAllAppsButton.requestFocus();
3347 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003348 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003349
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003350 // Change the state *after* we've called all the transition code
3351 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003352
Winson Chung5fb63472011-02-02 17:03:37 -08003353 // Resume the auto-advance of widgets
3354 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003355 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003356
Winson Chung0f785722015-04-08 10:27:49 -07003357 if (changed) {
3358 // Send an accessibility event to announce the context change
3359 getWindow().getDecorView()
3360 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003361 }
Winson5c6bdbb2015-09-03 11:36:19 -07003362 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003363 }
3364
Winsone9f27272015-10-13 10:47:51 -07003365 /**
3366 * Shows the overview button.
3367 */
Adam Cohened307df2013-10-02 09:37:31 -07003368 void showOverviewMode(boolean animated) {
Winsone9f27272015-10-13 10:47:51 -07003369 showOverviewMode(animated, false);
3370 }
3371
3372 /**
3373 * Shows the overview button, and if {@param requestButtonFocus} is set, will force the focus
3374 * onto one of the overview panel buttons.
3375 */
3376 void showOverviewMode(boolean animated, boolean requestButtonFocus) {
3377 Runnable postAnimRunnable = null;
3378 if (requestButtonFocus) {
3379 postAnimRunnable = new Runnable() {
3380 @Override
3381 public void run() {
3382 // Hitting the menu button when in touch mode does not trigger touch mode to
3383 // be disabled, so if requested, force focus on one of the overview panel
3384 // buttons.
3385 mOverviewPanel.requestFocusFromTouch();
3386 }
3387 };
3388 }
Adam Cohened307df2013-10-02 09:37:31 -07003389 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003390 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3391 Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003392 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winsone9f27272015-10-13 10:47:51 -07003393 postAnimRunnable);
Adam Cohened307df2013-10-02 09:37:31 -07003394 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003395 }
3396
Winson Chungb745afb2015-03-02 11:51:23 -08003397 /**
3398 * Shows the apps view.
3399 */
Winson Chung76648c52015-07-10 14:33:23 -07003400 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3401 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003402 if (resetListToTop) {
3403 mAppsView.scrollToTop();
3404 }
Winson Chung4ac30062015-05-08 17:34:17 -07003405 if (updatePredictedApps) {
3406 tryAndUpdatePredictedApps();
3407 }
Winson Chung76648c52015-07-10 14:33:23 -07003408 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003409 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410
Winson Chungb745afb2015-03-02 11:51:23 -08003411 /**
3412 * Shows the widgets view.
3413 */
3414 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003415 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003416 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003417 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003418 }
Winson Chung76648c52015-07-10 14:33:23 -07003419 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420
3421 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003422 @Override
3423 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003424 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003425 }
3426 });
Winson Chungb745afb2015-03-02 11:51:23 -08003427 }
3428
3429 /**
3430 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003431 *
3432 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003433 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003434 // TODO: calling method should use the return value so that when {@code false} is returned
3435 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003436 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003437 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3438 mState != State.WIDGETS_SPRING_LOADED) {
3439 return false;
3440 }
3441 if (toState != State.APPS && toState != State.WIDGETS) {
3442 return false;
3443 }
Winson Chungb745afb2015-03-02 11:51:23 -08003444
3445 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003446 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3447 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003448 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003449 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003450 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003451
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003453 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454
3455 // Pause the auto-advance of widgets until we are out of AllApps
3456 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003457 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458 closeFolder();
3459
3460 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003461 getWindow().getDecorView()
3462 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003463 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464 }
3465
Winson Chungcd99cd32015-04-29 11:03:24 -07003466 /**
3467 * Updates the workspace and interaction state on state change, and return the animation to this
3468 * new state.
3469 */
3470 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chung006ee262015-08-03 14:40:11 -07003471 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003472 Workspace.State fromState = mWorkspace.getState();
Winson Chung006ee262015-08-03 14:40:11 -07003473 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003474 updateInteraction(fromState, toState);
3475 return anim;
3476 }
3477
Hyunyoung Song3f471442015-04-08 19:01:34 -07003478 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003479 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003480 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3481 mState == State.WIDGETS_SPRING_LOADED) {
3482 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003483 }
Winson Chungb745afb2015-03-02 11:51:23 -08003484
Winson Chung006ee262015-08-03 14:40:11 -07003485 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3486 Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003487 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3488 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003489 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003490 }
Adam Cohen7777d962011-08-18 18:58:38 -07003491
Hyunyoung Song3f471442015-04-08 19:01:34 -07003492 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003493 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003494 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003495
Winson Chunge7a03942011-08-05 15:05:12 -07003496 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003497 @Override
3498 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003499 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003500 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3501 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003502 // Before we show workspace, hide all apps again because
3503 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3504 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003505 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003506 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003507 } else {
3508 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003509 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003510 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003511 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003512 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513
Michael Jurkad3ef3062010-11-23 16:23:58 -08003514 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003515 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003516 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003517 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003518 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003519 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003520 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521 }
3522
Winson Chung4ac30062015-05-08 17:34:17 -07003523 /**
3524 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3525 * resumed.
3526 */
3527 private void tryAndUpdatePredictedApps() {
3528 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003529 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003530 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003531 mAppsView.setPredictedApps(apps);
3532 }
3533 }
3534 }
3535
Michael Jurkab3e22d92011-10-31 15:58:33 -07003536 void lockAllApps() {
3537 // TODO
3538 }
3539
3540 void unlockAllApps() {
3541 // TODO
3542 }
3543
Sunny Goyal594d76d2014-11-06 10:12:54 -08003544 protected void disableVoiceButtonProxy(boolean disable) {
3545 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003546 }
3547
Winsonf768d932015-09-25 16:12:35 -07003548 public boolean launcherCallbacksProvidesSearch() {
3549 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3550 }
3551
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003552 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003553 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003554 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003555 }
3556
Adam Cohen24ce0b32014-01-14 16:18:14 -08003557 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003558 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3559 if (searchProvider == null) {
3560 return null;
3561 }
3562
3563 Bundle opts = new Bundle();
3564 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003565 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003566
Tony Wickham3a3517f2015-10-06 11:27:04 -07003567 // Determine the min and max dimensions of the widget.
3568 LauncherAppState app = LauncherAppState.getInstance();
3569 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3570 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3571 float density = getResources().getDisplayMetrics().density;
3572 Rect searchBounds = portraitProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3573 int maxHeight = (int) (searchBounds.height() / density);
3574 int minHeight = maxHeight;
3575 int maxWidth = (int) (searchBounds.width() / density);
3576 int minWidth = maxWidth;
3577 if (!landscapeProfile.isVerticalBarLayout()) {
3578 searchBounds = landscapeProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3579 maxHeight = (int) Math.max(maxHeight, searchBounds.height() / density);
3580 minHeight = (int) Math.min(minHeight, searchBounds.height() / density);
3581 maxWidth = (int) Math.max(maxWidth, searchBounds.width() / density);
3582 minWidth = (int) Math.min(minWidth, searchBounds.width() / density);
3583 }
3584 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3585 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3586 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3587 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
3588
Sunny Goyal594d76d2014-11-06 10:12:54 -08003589 SharedPreferences sp = getSharedPreferences(
3590 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3591 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003592 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003593 if (!searchProvider.provider.flattenToString().equals(
3594 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003595 || (widgetInfo == null)
3596 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003597 // A valid widget is not already bound.
3598 if (widgetId > -1) {
3599 mAppWidgetHost.deleteAppWidgetId(widgetId);
3600 widgetId = -1;
3601 }
3602
3603 // Try to bind a new widget
3604 widgetId = mAppWidgetHost.allocateAppWidgetId();
3605
3606 if (!AppWidgetManagerCompat.getInstance(this)
3607 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3608 mAppWidgetHost.deleteAppWidgetId(widgetId);
3609 widgetId = -1;
3610 }
3611
3612 sp.edit()
3613 .putInt(QSB_WIDGET_ID, widgetId)
3614 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3615 .commit();
3616 }
3617
Sunny Goyal8d595092015-07-06 12:22:14 -07003618 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003619 if (widgetId != -1) {
3620 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003621 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003622 mQsb.updateAppWidgetOptions(opts);
3623 mQsb.setPadding(0, 0, 0, 0);
3624 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003625 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003626 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003627 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003628 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003629 }
3630
Sunny Goyal22235bc2015-04-03 09:23:43 -07003631 private void reinflateQSBIfNecessary() {
3632 if (mQsb instanceof LauncherAppWidgetHostView &&
3633 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3634 mSearchDropTargetBar.removeView(mQsb);
3635 mQsb = null;
3636 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3637 }
3638 }
3639
Michael Jurkad7c28052012-04-27 15:43:36 -07003640 @Override
3641 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003642 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003643 final List<CharSequence> text = event.getText();
3644 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003645 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003646 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003647 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003648 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003649 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003650 } else if (mWorkspace != null) {
3651 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003652 } else {
3653 text.add(getString(R.string.all_apps_home_button_label));
3654 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003655 return result;
3656 }
3657
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003658 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003659 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003660 */
Adam Cohen091440a2015-03-18 14:16:05 -07003661 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003662 @Override
3663 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003664 closeSystemDialogs();
3665 }
3666 }
3667
3668 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 * If the activity is currently paused, signal that we need to run the passed Runnable
3670 * in onResume.
3671 *
3672 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003673 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3674 * wrong when we're not running, and if the activity comes back to what the configuration was
3675 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 *
3677 * Implementation of the method from LauncherModel.Callbacks.
3678 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003679 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003680 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003681 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003683 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003684 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003685 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003686 }
3687 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003688 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003689 return true;
3690 } else {
3691 return false;
3692 }
3693 }
3694
Michael Jurkac402cd92013-05-20 15:49:32 +02003695 private boolean waitUntilResume(Runnable run) {
3696 return waitUntilResume(run, false);
3697 }
3698
Michael Jurka1e2f4652013-07-08 18:03:46 -07003699 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003700 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003701 }
3702
Michael Jurka7607c2f2013-04-03 14:33:19 -07003703 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003704 * If the activity is currently paused, signal that we need to re-run the loader
3705 * in onResume.
3706 *
3707 * This needs to be called from incoming places where resources might have been loaded
3708 * while we are paused. That is becaues the Configuration might be wrong
3709 * when we're not running, and if it comes back to what it was when we
3710 * were paused, we are not restarted.
3711 *
3712 * Implementation of the method from LauncherModel.Callbacks.
3713 *
3714 * @return true if we are currently paused. The caller might be able to
3715 * skip some work in that case since we will come back again.
3716 */
3717 public boolean setLoadOnResume() {
3718 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003719 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003720 mOnResumeNeedsLoad = true;
3721 return true;
3722 } else {
3723 return false;
3724 }
3725 }
3726
3727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003729 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003731 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003732 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003733 } else {
3734 return SCREEN_COUNT / 2;
3735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003737
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 /**
3739 * 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
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 // If we're starting binding all over again, clear any bind calls we'd postponed in
3747 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3748 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003749 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003750
Winson Chungd64d1762013-08-20 14:37:16 -07003751 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003752 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003753 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003754
Michael Jurka05bf644e2011-11-30 20:28:53 -08003755 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003756 if (mHotseat != null) {
3757 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 }
3759 }
3760
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 @Override
3762 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003763 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003764
Adam Cohen5084cba2013-09-03 12:01:16 -07003765 // If there are no screens, we need to have an empty screen
3766 if (orderedScreenIds.size() == 0) {
3767 mWorkspace.addExtraEmptyScreen();
3768 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003769
3770 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003771 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003772 if (hasCustomContentToLeft()) {
3773 mWorkspace.createCustomContentContainer();
3774 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003775 }
Winson Chung64359a52013-07-08 17:17:08 -07003776 }
3777
3778 @Override
3779 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 int count = orderedScreenIds.size();
3781 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003782 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 }
3785
Winson Chungd64d1762013-08-20 14:37:16 -07003786 public void bindAppsAdded(final ArrayList<Long> newScreens,
3787 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003788 final ArrayList<ItemInfo> addAnimated,
3789 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003790 Runnable r = new Runnable() {
3791 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003792 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003793 }
3794 };
3795 if (waitUntilResume(r)) {
3796 return;
3797 }
3798
Winson Chungd64d1762013-08-20 14:37:16 -07003799 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003800 if (newScreens != null) {
3801 bindAddScreens(newScreens);
3802 }
Winson Chungd64d1762013-08-20 14:37:16 -07003803
3804 // We add the items without animation on non-visible pages, and with
3805 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003806 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 bindItems(addNotAnimated, 0,
3808 addNotAnimated.size(), false);
3809 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003810 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003811 bindItems(addAnimated, 0,
3812 addAnimated.size(), true);
3813 }
Winson Chungc58497e2013-09-03 17:48:37 -07003814
Winson Chung87412982013-10-03 18:34:14 -07003815 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003816 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003817
Winson Chungb745afb2015-03-02 11:51:23 -08003818 if (addedApps != null && mAppsView != null) {
3819 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003820 }
Winson Chungd64d1762013-08-20 14:37:16 -07003821 }
3822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003823 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824 * Bind the items start-end from the list.
3825 *
3826 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003827 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003828 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003829 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3830 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003831 Runnable r = new Runnable() {
3832 public void run() {
3833 bindItems(shortcuts, start, end, forceAnimateIcons);
3834 }
3835 };
3836 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003837 return;
3838 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003839
Winson Chungf0c6ae02012-03-21 16:10:31 -07003840 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003841 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3842 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003843 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003844 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003845 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003846 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003848
3849 // Short circuit if we are loading dock items for a configuration which has no dock
3850 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3851 mHotseat == null) {
3852 continue;
3853 }
3854
Sunny Goyal639e9062015-08-19 19:17:06 -07003855 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 switch (item.itemType) {
3857 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3858 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003859 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003860 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003861
Winson Chung64359a52013-07-08 17:17:08 -07003862 /*
3863 * TODO: FIX collision case
3864 */
Winson Chungce376632013-07-11 16:12:41 -07003865 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3866 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3867 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003868 View v = cl.getChildAt(item.cellX, item.cellY);
3869 Object tag = v.getTag();
3870 String desc = "Collision while binding workspace item: " + item
3871 + ". Collides with " + tag;
3872 if (LauncherAppState.isDogfoodBuild()) {
3873 throw (new RuntimeException(desc));
3874 } else {
3875 Log.d(TAG, desc);
3876 }
Winson Chungce376632013-07-11 16:12:41 -07003877 }
Winson Chung64359a52013-07-08 17:17:08 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003880 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003881 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003882 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003883 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003885 default:
3886 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003888
3889 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3890 item.cellY, 1, 1);
3891 if (animateIcons) {
3892 // Animate all the applications up now
3893 view.setAlpha(0f);
3894 view.setScaleX(0f);
3895 view.setScaleY(0f);
3896 bounceAnims.add(createNewAppBounceAnimation(view, i));
3897 newShortcutsScreenId = item.screenId;
3898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003899 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003900
Winson Chung997a9232013-07-24 15:33:46 -07003901 if (animateIcons) {
3902 // Animate to the correct page
3903 if (newShortcutsScreenId > -1) {
3904 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003905 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003906 final Runnable startBounceAnimRunnable = new Runnable() {
3907 public void run() {
3908 anim.playTogether(bounceAnims);
3909 anim.start();
3910 }
3911 };
Winson Chung997a9232013-07-24 15:33:46 -07003912 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003913 // We post the animation slightly delayed to prevent slowdowns
3914 // when we are loading right after we return to launcher.
3915 mWorkspace.postDelayed(new Runnable() {
3916 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003917 if (mWorkspace != null) {
3918 mWorkspace.snapToPage(newScreenIndex);
3919 mWorkspace.postDelayed(startBounceAnimRunnable,
3920 NEW_APPS_ANIMATION_DELAY);
3921 }
Winson Chung94d67682013-09-25 16:29:40 -07003922 }
3923 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003924 } else {
3925 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003926 }
3927 }
Winson Chung64359a52013-07-08 17:17:08 -07003928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003930 }
3931
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932 /**
3933 * Implementation of the method from LauncherModel.Callbacks.
3934 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003935 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003936 Runnable r = new Runnable() {
3937 public void run() {
3938 bindFolders(folders);
3939 }
3940 };
3941 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003942 return;
3943 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003944 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003945 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946
3947 /**
3948 * Add the views for a widget to the workspace.
3949 *
3950 * Implementation of the method from LauncherModel.Callbacks.
3951 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003952 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003953 Runnable r = new Runnable() {
3954 public void run() {
3955 bindAppWidget(item);
3956 }
3957 };
3958 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003959 return;
3960 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003961
Daniel Sandler843e8602010-06-07 14:59:01 -04003962 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3963 if (DEBUG_WIDGETS) {
3964 Log.d(TAG, "bindAppWidget: " + item);
3965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 final Workspace workspace = mWorkspace;
3967
Adam Cohen59400422014-03-05 18:07:04 -08003968 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003969 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003970
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003971 if (!mIsSafeModeEnabled
3972 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003973 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3974
Sunny Goyalff572272014-07-23 13:58:07 -07003975 if (appWidgetInfo == null) {
3976 if (DEBUG_WIDGETS) {
3977 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3978 + " belongs to component " + item.providerName
3979 + ", as the povider is null");
3980 }
3981 LauncherModel.deleteItemFromDatabase(this, item);
3982 return;
3983 }
Sunny Goyalff572272014-07-23 13:58:07 -07003984
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003985 // If we do not have a valid id, try to bind an id.
3986 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3987 // Note: This assumes that the id remap broadcast is received before this step.
3988 // If that is not the case, the id remap will be ignored and user may see the
3989 // click to setup view.
3990 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3991 pendingInfo.spanX = item.spanX;
3992 pendingInfo.spanY = item.spanY;
3993 pendingInfo.minSpanX = item.minSpanX;
3994 pendingInfo.minSpanY = item.minSpanY;
3995 Bundle options = null;
3996 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003997
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003998 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3999 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4000 newWidgetId, appWidgetInfo, options);
4001
4002 // TODO consider showing a permission dialog when the widget is clicked.
4003 if (!success) {
4004 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4005 if (DEBUG_WIDGETS) {
4006 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4007 + " belongs to component " + item.providerName
4008 + ", as the launcher is unable to bing a new widget id");
4009 }
4010 LauncherModel.deleteItemFromDatabase(this, item);
4011 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004012 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004013
4014 item.appWidgetId = newWidgetId;
4015
4016 // If the widget has a configure activity, it is still needs to set it up, otherwise
4017 // the widget is ready to go.
4018 item.restoreStatus = (appWidgetInfo.configure == null)
4019 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4020 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4021
4022 LauncherModel.updateItemInDatabase(this, item);
4023 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4024 && (appWidgetInfo.configure == null)) {
4025 // If the ID is already valid, verify if we need to configure or not.
4026 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4027 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004028 }
Sunny Goyalff572272014-07-23 13:58:07 -07004029 }
4030
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004031 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004032 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004034 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4035 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004036 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004037
Sunny Goyal56c73602015-09-25 12:55:01 -07004038 // Verify that we own the widget
4039 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4040 if (info == null || appWidgetInfo == null ||
4041 !info.provider.equals(appWidgetInfo.provider)) {
4042 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
4043 deleteWidgetInfo(item);
4044 return;
4045 }
4046
Sunny Goyal651077b2014-06-30 14:15:31 -07004047 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004048 item.minSpanX = appWidgetInfo.minSpanX;
4049 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 } else {
4051 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004052 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4053 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004054 view.updateIcon(mIconCache);
4055 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004056 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004057 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004061 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062
Adam Cohendcd297f2013-06-18 13:13:40 -07004063 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004065 if (!item.isCustomWidget()) {
4066 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4067 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004068
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 workspace.requestLayout();
4070
Daniel Sandler843e8602010-06-07 14:59:01 -04004071 if (DEBUG_WIDGETS) {
4072 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4073 + (SystemClock.uptimeMillis()-start) + "ms");
4074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 }
4076
Sunny Goyalff572272014-07-23 13:58:07 -07004077 /**
4078 * Restores a pending widget.
4079 *
4080 * @param appWidgetId The app widget id
4081 * @param cellInfo The position on screen where to create the widget.
4082 */
4083 private void completeRestoreAppWidget(final int appWidgetId) {
4084 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4085 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4086 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4087 return;
4088 }
4089
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004090 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004091 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4092
4093 mWorkspace.reinflateWidgetsIfNecessary();
4094 LauncherModel.updateItemInDatabase(this, info);
4095 }
4096
Adam Cohen1462de32012-07-24 22:34:36 -07004097 public void onPageBoundSynchronously(int page) {
4098 mSynchronouslyBoundPages.add(page);
4099 }
4100
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 /**
4102 * Callback saying that there aren't any more items to bind.
4103 *
4104 * Implementation of the method from LauncherModel.Callbacks.
4105 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004106 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004107 Runnable r = new Runnable() {
4108 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004109 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004110 }
4111 };
4112 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004113 return;
4114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 if (mSavedState != null) {
4116 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004117 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 mSavedState = null;
4120 }
4121
Adam Cohen1462de32012-07-24 22:34:36 -07004122 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004124 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004125 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004126
4127 // If we received the result of any pending adds while the loader was running (e.g. the
4128 // widget configuration forced an orientation change), process them now.
4129 if (sPendingAddItem != null) {
4130 final long screenId = completeAdd(sPendingAddItem);
4131
4132 // TODO: this moves the user to the page where the pending item was added. Ideally,
4133 // the screen would be guaranteed to exist after bind, and the page would be set through
4134 // the workspace restore process.
4135 mWorkspace.post(new Runnable() {
4136 @Override
4137 public void run() {
4138 mWorkspace.snapToScreenId(screenId);
4139 }
4140 });
4141 sPendingAddItem = null;
4142 }
4143
Sunny Goyal756adbc2015-04-16 15:20:51 -07004144 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004145
4146 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004147 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004148 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004149 }
4150
Adam Cohen3ed316a2014-07-23 18:21:20 -07004151 private void sendLoadingCompleteBroadcastIfNecessary() {
4152 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4153 String permission =
4154 getResources().getString(R.string.receive_first_load_broadcast_permission);
4155 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4156 sendBroadcast(intent, permission);
4157 SharedPreferences.Editor editor = mSharedPrefs.edit();
4158 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4159 editor.apply();
4160 }
4161 }
4162
Winson Chunga0b7e862013-09-05 16:03:15 -07004163 public boolean isAllAppsButtonRank(int rank) {
4164 if (mHotseat != null) {
4165 return mHotseat.isAllAppsButtonRank(rank);
4166 }
4167 return false;
4168 }
4169
Winson Chunga2413752012-04-03 14:22:34 -07004170 private boolean canRunNewAppsAnimation() {
4171 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004172 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4173 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004174 }
4175
Winson Chungc9168342013-06-26 14:54:55 -07004176 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4177 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4178 PropertyValuesHolder.ofFloat("alpha", 1f),
4179 PropertyValuesHolder.ofFloat("scaleX", 1f),
4180 PropertyValuesHolder.ofFloat("scaleY", 1f));
4181 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4182 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004183 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004184 return bounceAnim;
4185 }
4186
Adam Cohen27772732013-11-11 14:00:56 +00004187 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004188 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004189 }
4190
Tony Wickham3a3517f2015-10-06 11:27:04 -07004191 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004192 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004193 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004194 }
4195
Tony Wickham55616cd2015-09-23 14:55:17 -07004196 public int getSearchBarHeight() {
4197 if (mLauncherCallbacks != null) {
4198 return mLauncherCallbacks.getSearchBarHeight();
4199 }
4200 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4201 }
4202
Winson Chung88fa7412015-08-03 14:25:28 -07004203 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004204 if (mSearchDropTargetBar == null) {
4205 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004206 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004207 if (mQsb != null) {
4208 mSearchDropTargetBar.removeView(mQsb);
4209 mQsb = null;
4210 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004211 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004212 }
4213
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004214 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004215 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4216 * multiple calls to bind the same list.)
4217 */
4218 @Thunk ArrayList<AppInfo> mTmpAppsList;
4219 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4220 public void run() {
4221 bindAllApplications(mTmpAppsList);
4222 mTmpAppsList = null;
4223 }
4224 };
4225
4226 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004227 * Add the icons for all apps.
4228 *
4229 * Implementation of the method from LauncherModel.Callbacks.
4230 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004231 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004232 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4233 mTmpAppsList = apps;
4234 return;
4235 }
4236
Winson Chungb745afb2015-03-02 11:51:23 -08004237 if (mAppsView != null) {
4238 mAppsView.setApps(apps);
4239 }
Adam Cohen9211d422014-10-07 18:14:53 -07004240 if (mLauncherCallbacks != null) {
4241 mLauncherCallbacks.bindAllApplications(apps);
4242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004243 }
4244
4245 /**
4246 * A package was updated.
4247 *
4248 * Implementation of the method from LauncherModel.Callbacks.
4249 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004250 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004251 Runnable r = new Runnable() {
4252 public void run() {
4253 bindAppsUpdated(apps);
4254 }
4255 };
4256 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004257 return;
4258 }
4259
Winson Chungb745afb2015-03-02 11:51:23 -08004260 if (mAppsView != null) {
4261 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004262 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004263 }
4264
Sunny Goyal4390ace2014-10-13 11:33:11 -07004265 @Override
4266 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4267 Runnable r = new Runnable() {
4268 public void run() {
4269 bindWidgetsRestored(widgets);
4270 }
4271 };
4272 if (waitUntilResume(r)) {
4273 return;
4274 }
4275 mWorkspace.widgetsRestored(widgets);
4276 }
4277
Joe Onorato9c1289c2009-08-17 11:03:03 -04004278 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004279 * Some shortcuts were updated in the background.
4280 *
4281 * Implementation of the method from LauncherModel.Callbacks.
4282 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004283 @Override
4284 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4285 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004286 Runnable r = new Runnable() {
4287 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004288 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004289 }
4290 };
4291 if (waitUntilResume(r)) {
4292 return;
4293 }
4294
Sunny Goyal4390ace2014-10-13 11:33:11 -07004295 if (!updated.isEmpty()) {
4296 mWorkspace.updateShortcuts(updated);
4297 }
4298
4299 if (!removed.isEmpty()) {
4300 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4301 for (ShortcutInfo si : removed) {
4302 removedComponents.add(si.getTargetComponent());
4303 }
4304 mWorkspace.removeItemsByComponentName(removedComponents, user);
4305 // Notify the drag controller
4306 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004307 }
4308 }
4309
4310 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004311 * Update the state of a package, typically related to install state.
4312 *
4313 * Implementation of the method from LauncherModel.Callbacks.
4314 */
Sunny Goyale755d462014-07-22 13:48:29 -07004315 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004316 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4317 Runnable r = new Runnable() {
4318 public void run() {
4319 bindRestoreItemsChange(updates);
4320 }
4321 };
4322 if (waitUntilResume(r)) {
4323 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004324 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004325
Sunny Goyal756adbc2015-04-16 15:20:51 -07004326 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004327 }
4328
4329 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004330 * A package was uninstalled. We take both the super set of packageNames
4331 * in addition to specific applications to remove, the reason being that
4332 * this can be called when a package is updated as well. In that scenario,
4333 * we only remove specific components from the workspace, where as
4334 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004335 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004337 * Implementation of the method from LauncherModel.Callbacks.
4338 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004339 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004340 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004341 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004342 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004343 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004344 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004345 }
Winson Chungd64d1762013-08-20 14:37:16 -07004346 };
4347 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004348 return;
4349 }
4350
Sunny Goyal1a745e82014-10-02 15:58:31 -07004351 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004352 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4353 for (AppInfo info : appInfos) {
4354 removedComponents.add(info.componentName);
4355 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004356 if (!packageNames.isEmpty()) {
4357 mWorkspace.removeItemsByPackageName(packageNames, user);
4358 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004359 if (!removedComponents.isEmpty()) {
4360 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004361 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004362 // Notify the drag controller
4363 mDragController.onAppsRemoved(packageNames, removedComponents);
4364
Sunny Goyal1a745e82014-10-02 15:58:31 -07004365 } else {
4366 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004367 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004368
Winson Chungdf95eb12013-10-16 14:57:07 -07004369 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004370 if (mAppsView != null) {
4371 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004372 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004373 }
Joe Onoratobe386092009-11-17 17:32:16 -08004374
Michael Jurkac402cd92013-05-20 15:49:32 +02004375 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004376 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004377 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004378 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004379 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004380
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004381 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004382 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004383 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004384 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004385 return;
4386 }
4387
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004388 if (mWidgetsView != null && model != null) {
4389 mWidgetsView.addWidgets(model);
4390 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004391 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004392 }
4393
Winson Chung400438b2011-01-16 17:53:48 -08004394 private int mapConfigurationOriActivityInfoOri(int configOri) {
4395 final Display d = getWindowManager().getDefaultDisplay();
4396 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4397 switch (d.getRotation()) {
4398 case Surface.ROTATION_0:
4399 case Surface.ROTATION_180:
4400 // We are currently in the same basic orientation as the natural orientation
4401 naturalOri = configOri;
4402 break;
4403 case Surface.ROTATION_90:
4404 case Surface.ROTATION_270:
4405 // We are currently in the other basic orientation to the natural orientation
4406 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4407 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4408 break;
4409 }
4410
4411 int[] oriMap = {
4412 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4413 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4414 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4415 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4416 };
4417 // Since the map starts at portrait, we need to offset if this device's natural orientation
4418 // is landscape.
4419 int indexOffset = 0;
4420 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4421 indexOffset = 1;
4422 }
4423 return oriMap[(d.getRotation() + indexOffset) % 4];
4424 }
Adam Cohen446e9402011-09-15 18:21:21 -07004425
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004426 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004427 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004428 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004429 if (Utilities.ATLEAST_JB_MR2) {
4430 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4431 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004432 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4433 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004434 }
Winson Chung4b919f82012-05-01 10:44:08 -07004435 }
4436 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004437
Winson Chung4b919f82012-05-01 10:44:08 -07004438 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004439 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004440 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004441 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004442 } else {
4443 mHandler.postDelayed(new Runnable() {
4444 public void run() {
4445 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4446 }
4447 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004448 }
Winson Chung4b919f82012-05-01 10:44:08 -07004449 }
Winson Chung400438b2011-01-16 17:53:48 -08004450 }
4451
Winson Chunge43a1e72014-01-15 10:33:02 -08004452 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004453 if (mLauncherCallbacks != null) {
4454 return mLauncherCallbacks.isLauncherPreinstalled();
4455 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004456 PackageManager pm = getPackageManager();
4457 try {
4458 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4459 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4460 return true;
4461 } else {
4462 return false;
4463 }
4464 } catch (NameNotFoundException e) {
4465 e.printStackTrace();
4466 return false;
4467 }
4468 }
4469
Adam Cohenea90f832014-05-21 15:03:34 -07004470 /**
4471 * This method indicates whether or not we should suggest default wallpaper dimensions
4472 * when our wallpaper cropper was not yet used to set a wallpaper.
4473 */
4474 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004475 if (mLauncherCallbacks != null) {
4476 return mLauncherCallbacks.overrideWallpaperDimensions();
4477 }
Adam Cohenea90f832014-05-21 15:03:34 -07004478 return true;
4479 }
4480
Adam Cohen432609a2014-03-13 17:03:22 -07004481 /**
4482 * To be overridden by subclasses to indicate that there is an activity to launch
4483 * before showing the standard launcher experience.
4484 */
4485 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004486 if (mLauncherCallbacks != null) {
4487 return mLauncherCallbacks.hasFirstRunActivity();
4488 }
Adam Cohen432609a2014-03-13 17:03:22 -07004489 return false;
4490 }
4491
4492 /**
4493 * To be overridden by subclasses to launch any first run activity
4494 */
4495 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004496 if (mLauncherCallbacks != null) {
4497 return mLauncherCallbacks.getFirstRunActivity();
4498 }
Adam Cohen432609a2014-03-13 17:03:22 -07004499 return null;
4500 }
4501
Winson Chunga6945242014-01-08 14:04:34 -08004502 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004503 return !ActivityManager.isRunningInTestHarness() &&
4504 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004505 }
4506
Adam Cohen4a9423d2014-03-28 14:22:31 -07004507 protected boolean hasRunFirstRunActivity() {
4508 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4509 }
4510
Adam Cohen432609a2014-03-13 17:03:22 -07004511 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004512 if (shouldRunFirstRunActivity() &&
4513 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004514 Intent firstRunIntent = getFirstRunActivity();
4515 if (firstRunIntent != null) {
4516 startActivity(firstRunIntent);
4517 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004518 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004519 }
4520 }
Adam Cohen432609a2014-03-13 17:03:22 -07004521 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004522 }
4523
4524 private void markFirstRunActivityShown() {
4525 SharedPreferences.Editor editor = mSharedPrefs.edit();
4526 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4527 editor.apply();
4528 }
4529
Adam Cohen432609a2014-03-13 17:03:22 -07004530 /**
4531 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4532 * screen that must be displayed and dismissed.
4533 */
4534 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004535 if (mLauncherCallbacks != null) {
4536 return mLauncherCallbacks.hasDismissableIntroScreen();
4537 }
Adam Cohen432609a2014-03-13 17:03:22 -07004538 return false;
4539 }
4540
4541 /**
4542 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4543 */
4544 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004545 if (mLauncherCallbacks != null) {
4546 return mLauncherCallbacks.getIntroScreen();
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 return null;
4549 }
4550
4551 /**
4552 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4553 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4554 */
4555 private boolean shouldShowIntroScreen() {
4556 return hasDismissableIntroScreen() &&
4557 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4558 }
4559
4560 protected void showIntroScreen() {
4561 View introScreen = getIntroScreen();
4562 changeWallpaperVisiblity(false);
4563 if (introScreen != null) {
4564 mDragLayer.showOverlayView(introScreen);
4565 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004566 if (mLauncherOverlayContainer != null) {
4567 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4568 }
Adam Cohen432609a2014-03-13 17:03:22 -07004569 }
4570
4571 public void dismissIntroScreen() {
4572 markIntroScreenDismissed();
4573 if (showFirstRunActivity()) {
4574 // We delay hiding the intro view until the first run activity is showing. This
4575 // avoids a blip.
4576 mWorkspace.postDelayed(new Runnable() {
4577 @Override
4578 public void run() {
4579 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004580 if (mLauncherOverlayContainer != null) {
4581 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4582 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004583 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004584 }
4585 }, ACTIVITY_START_DELAY);
4586 } else {
4587 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004588 if (mLauncherOverlayContainer != null) {
4589 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4590 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004591 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004592 }
4593 changeWallpaperVisiblity(true);
4594 }
4595
4596 private void markIntroScreenDismissed() {
4597 SharedPreferences.Editor editor = mSharedPrefs.edit();
4598 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4599 editor.apply();
4600 }
4601
Adam Cohen091440a2015-03-18 14:16:05 -07004602 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004603 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4604 // on the device, then we always show the first run cling experience (or if there is no
4605 // launcher2). Otherwise, we prompt the user upon started for migration
4606 LauncherClings launcherClings = new LauncherClings(this);
4607 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004608 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 if (mModel.canMigrateFromOldLauncherDb(this)) {
4610 launcherClings.showMigrationCling();
4611 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004612 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004613 }
4614 }
4615 }
4616
Winson Chunga6945242014-01-08 14:04:34 -08004617 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004618 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4619 if (mHotseat != null) mHotseat.setAlpha(1f);
4620 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004621 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4622 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004623 }
4624
4625 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004626 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4627 if (mHotseat != null) mHotseat.setAlpha(0f);
4628 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004629 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4630 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004631 }
4632
Winson Chung5ffd51d2015-06-25 11:36:50 -07004633 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004634 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004635 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004636 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004637 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004638 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004639 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4640 if (userHandle != null) {
4641 user = UserHandleCompat.fromUser(userHandle);
4642 }
4643 }
4644 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004645 }
4646
4647 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004648 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004649 if (user == null) {
4650 user = UserHandleCompat.myUserHandle();
4651 }
4652
4653 // Called from search suggestion, add the profile extra to the intent to ensure that we
4654 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004655 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004656 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004657 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004658 return null;
4659 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004660 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004661 }
4662
Winson Chung5ffd51d2015-06-25 11:36:50 -07004663 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4665 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004666 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004667 UserHandleCompat.myUserHandle());
4668 }
4669
Winson Chung5ffd51d2015-06-25 11:36:50 -07004670 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004671 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4672 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004673 mWorkspace.onExternalDragStartedWithItem(dragView);
4674 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004675 }
4676
Winson Chung5ffd51d2015-06-25 11:36:50 -07004677 protected void moveWorkspaceToDefaultScreen() {
4678 mWorkspace.moveToDefaultScreen(false);
4679 }
4680
Anjali Koppalf5d29132014-02-28 13:33:27 -08004681 @Override
4682 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004683 if (mLauncherCallbacks != null) {
4684 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4685 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004686 }
4687
Winson Chung80baf5a2010-08-09 16:03:15 -07004688 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004689 * Returns a FastBitmapDrawable with the icon, accurately sized.
4690 */
4691 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4692 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4693 d.setFilterBitmap(true);
4694 resizeIconDrawable(d);
4695 return d;
4696 }
4697
4698 /**
4699 * Resizes an icon drawable to the correct icon size.
4700 */
Winson5fbe0742015-09-30 15:33:00 -07004701 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004702 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004703 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004704 }
4705
4706 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004707 * Prints out out state for debugging.
4708 */
4709 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004710 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004711 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004712 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4713 Log.d(TAG, "mRestoring=" + mRestoring);
4714 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4715 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004716 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004717 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004718 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004719
Daniel Sandler325dc232013-06-05 22:57:57 -04004720 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004721 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004722
4723 @Override
4724 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4725 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004726 synchronized (sDumpLogs) {
4727 writer.println(" ");
4728 writer.println("Debug logs: ");
4729 for (int i = 0; i < sDumpLogs.size(); i++) {
4730 writer.println(" " + sDumpLogs.get(i));
4731 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004732 }
Adam Cohen9211d422014-10-07 18:14:53 -07004733 if (mLauncherCallbacks != null) {
4734 mLauncherCallbacks.dump(prefix, fd, writer, args);
4735 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004736 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004737
4738 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004739 if (DEBUG_DUMP_LOG) {
4740 synchronized (sDumpLogs) {
4741 Log.d(TAG, "");
4742 Log.d(TAG, "*********************");
4743 Log.d(TAG, "Launcher debug logs: ");
4744 for (int i = 0; i < sDumpLogs.size(); i++) {
4745 Log.d(TAG, " " + sDumpLogs.get(i));
4746 }
4747 Log.d(TAG, "*********************");
4748 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004749 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004750 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004751 }
4752
4753 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004754 addDumpLog(tag, log, null, debugLog);
4755 }
4756
4757 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004758 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004759 if (e != null) {
4760 Log.d(tag, log, e);
4761 } else {
4762 Log.d(tag, log);
4763 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004764 }
Winson Chungede41292013-09-19 16:27:36 -07004765 if (DEBUG_DUMP_LOG) {
4766 sDateStamp.setTime(System.currentTimeMillis());
4767 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004768 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4769 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004770 }
Winson Chungede41292013-09-19 16:27:36 -07004771 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004772 }
4773
Adam Cohen59400422014-03-05 18:07:04 -08004774 public static CustomAppWidget getCustomAppWidget(String name) {
4775 return sCustomAppWidgets.get(name);
4776 }
4777
4778 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4779 return sCustomAppWidgets;
4780 }
4781
Winson Chungede41292013-09-19 16:27:36 -07004782 public void dumpLogsToLocalData() {
4783 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004784 new AsyncTask<Void, Void, Void>() {
4785 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004786 boolean success = false;
4787 sDateStamp.setTime(sRunStart);
4788 String FILENAME = sDateStamp.getMonth() + "-"
4789 + sDateStamp.getDay() + "_"
4790 + sDateStamp.getHours() + "-"
4791 + sDateStamp.getMinutes() + "_"
4792 + sDateStamp.getSeconds() + ".txt";
4793
4794 FileOutputStream fos = null;
4795 File outFile = null;
4796 try {
4797 outFile = new File(getFilesDir(), FILENAME);
4798 outFile.createNewFile();
4799 fos = new FileOutputStream(outFile);
4800 } catch (Exception e) {
4801 e.printStackTrace();
4802 }
4803 if (fos != null) {
4804 PrintWriter writer = new PrintWriter(fos);
4805
4806 writer.println(" ");
4807 writer.println("Debug logs: ");
4808 synchronized (sDumpLogs) {
4809 for (int i = 0; i < sDumpLogs.size(); i++) {
4810 writer.println(" " + sDumpLogs.get(i));
4811 }
4812 }
4813 writer.close();
4814 }
4815 try {
4816 if (fos != null) {
4817 fos.close();
4818 success = true;
4819 }
4820 } catch (IOException e) {
4821 e.printStackTrace();
4822 }
Michael Jurka43467462013-11-14 12:03:58 +01004823 return null;
Winson Chungede41292013-09-19 16:27:36 -07004824 }
Sunny Goyal8ac727b2015-09-23 15:38:09 -07004825 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Adam Cohen4caf2982013-08-20 18:54:31 -07004826 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004828}
Michael Jurka2763be32011-02-24 11:19:57 -08004829
Dan Sandlerd5024042014-01-09 15:01:33 -05004830interface DebugIntents {
4831 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4832 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004833}