blob: 35d08baa4e6932bb39d71ec206ed8d08a4cedd6d [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070038import android.content.ContentValues;
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;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
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;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700109import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700111import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700112import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700114import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700115import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700116
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700136 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700155 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
156 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
157 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
158
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700159 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
160
Mathew Inwood876a8462013-06-14 14:12:41 +0100161 /**
162 * IntentStarter uses request codes starting with this. This must be greater than all activity
163 * request codes used internally.
164 */
165 protected static final int REQUEST_LAST = 100;
166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700179 // Type: Content Values / parcelable
180 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: parcelable
182 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000183 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Adam Cohen432609a2014-03-13 17:03:22 -0700186 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800187 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
188
Adam Cohen3ed316a2014-07-23 18:21:20 -0700189 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
190 static final String ACTION_FIRST_LOAD_COMPLETE =
191 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
192
Adam Cohen39a06042013-07-19 14:30:12 -0700193 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700194 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700195
Sunny Goyal594d76d2014-11-06 10:12:54 -0800196 private static final String QSB_WIDGET_ID = "qsb_widget_id";
197 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
198
Winson Chunga6945242014-01-08 14:04:34 -0800199 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
200
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700202 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
203 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700204
Adam Cohen091440a2015-03-18 14:16:05 -0700205 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700206 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700208 private boolean mIsSafeModeEnabled;
209
Adam Cohenc2d6e892014-10-16 09:49:24 -0700210 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
211 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700212 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
216 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700217 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Winson Chunga2413752012-04-03 14:22:34 -0700219 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700220 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
221 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700222 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700223
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800224 private final BroadcastReceiver mCloseSystemDialogsReceiver
225 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800226
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700228 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800229 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700230 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700232 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
Sunny Goyalffe83f12014-08-14 17:39:34 -0700234 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700235 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700237 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800238 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800239 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700240
Michael Jurka0280c3b2010-09-17 15:00:07 -0700241 private int[] mTmpAddItemCellCoordinates = new int[2];
242
Sunny Goyal316490e2015-06-02 09:38:28 -0700243 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800244 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700245
Michael Jurka838a4ca2011-02-07 13:33:06 -0800246 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700247 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700248
Winson Chungc51db6a2011-10-05 11:44:49 -0700249 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700250 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700251
252 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700253 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700254
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700255 // Main container view and the model for the widget tray screen.
256 @Thunk WidgetsContainerView mWidgetsView;
257 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
Sunny Goyal594d76d2014-11-06 10:12:54 -0800259 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Adam Cohen091440a2015-03-18 14:16:05 -0700269 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private boolean mRestoring;
273 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700274 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800280 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700283 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800284 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Sunny Goyal639e9062015-08-19 19:17:06 -0700286 private LauncherClings mClings;
287
Sunny Goyale2df0622015-04-24 11:27:00 -0700288 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700289
Adam Cohen61f560d2013-09-30 15:58:20 -0700290 private View.OnTouchListener mHapticFeedbackTouchListener;
291
Adam Cohended9f8d2010-11-03 13:25:16 -0700292 // Related to the auto-advancing of widgets
293 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700294 private static final int ADVANCE_INTERVAL = 20000;
295 private static final int ADVANCE_STAGGER = 250;
296
297 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 private long mAutoAdvanceSentTime;
299 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700300 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700301
Winson Chung400438b2011-01-16 17:53:48 -0800302 // Determines how long to wait after a rotation before restoring the screen orientation to
303 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700307
Adam Cohen1462de32012-07-24 22:34:36 -0700308 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700309 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700310
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700311 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700312 static Date sDateStamp = new Date();
313 static DateFormat sDateFormat =
314 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
315 static long sRunStart = System.currentTimeMillis();
316 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700317
Winson Chung46353de2012-02-16 14:05:10 -0800318 // We only want to get the SharedPreferences once since it does an FS stat each time we get
319 // it from the context.
320 private SharedPreferences mSharedPrefs;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700329 private DeviceProfile mDeviceProfile;
330
Winson Chung13eb5272015-05-11 16:30:13 -0700331 // This is set to the view that launched the activity that navigated the user away from
332 // launcher. Since there is no callback for when the activity has finished launching, enable
333 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800334 private BubbleTextView mWaitingForResume;
335
Adam Cohen59400422014-03-05 18:07:04 -0800336 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
337 new HashMap<String, CustomAppWidget>();
338
339 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
340 static {
341 if (ENABLE_CUSTOM_WIDGET_TEST) {
342 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
343 }
344 }
345
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Adam Cohendb364c32014-05-20 14:23:40 -0700354 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355
Adam Cohen091440a2015-03-18 14:16:05 -0700356 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700363 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 }
365
Daniel Sandlerff02d492013-08-05 02:12:05 -0400366 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700367 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700368 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400369
370 @Thunk void setOrientation() {
371 if (mRotationEnabled) {
372 unlockScreenOrientation(true);
373 } else {
374 setRequestedOrientation(
375 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400377 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378
Sunny Goyal7779d622015-06-11 16:18:39 -0700379 private Runnable mUpdateOrientationRunnable = new Runnable() {
380 public void run() {
381 setOrientation();
382 }
383 };
384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 @Override
386 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700387 if (DEBUG_STRICT_MODE) {
388 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
389 .detectDiskReads()
390 .detectDiskWrites()
391 .detectNetwork() // or .detectAll() for all detectable problems
392 .penaltyLog()
393 .build());
394 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
395 .detectLeakedSqlLiteObjects()
396 .detectLeakedClosableObjects()
397 .penaltyLog()
398 .penaltyDeath()
399 .build());
400 }
401
Adam Cohen9211d422014-10-07 18:14:53 -0700402 if (mLauncherCallbacks != null) {
403 mLauncherCallbacks.preOnCreate();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700410
Adam Cohen2e6da152015-05-06 11:42:25 -0700411 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700412 mDeviceProfile = getResources().getConfiguration().orientation
413 == Configuration.ORIENTATION_LANDSCAPE ?
414 app.getInvariantDeviceProfile().landscapeProfile
415 : app.getInvariantDeviceProfile().portraitProfile;
416
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400417 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700418 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700422
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700424 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Daniel Sandlerff02d492013-08-05 02:12:05 -0400426 mStats = new Stats(this);
427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200439 android.os.Debug.startMethodTracing(
440 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700446 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
453 if (PROFILE_STARTUP) {
454 android.os.Debug.stopMethodTracing();
455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700461 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
474 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700476 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
477 // In case we are on a device with locked rotation, we should look at preferences to check
478 // if the user has specifically allowed rotation.
479 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700480 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700481 }
482
483 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
484 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400485 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700486
Adam Cohen9211d422014-10-07 18:14:53 -0700487 if (mLauncherCallbacks != null) {
488 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700489 if (mLauncherCallbacks.hasLauncherOverlay()) {
490 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700491 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
492 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
493 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700494 mWorkspace.setLauncherOverlay(mLauncherOverlay);
495 }
Adam Cohen9211d422014-10-07 18:14:53 -0700496 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700497
498 if (shouldShowIntroScreen()) {
499 showIntroScreen();
500 } else {
501 showFirstRunActivity();
502 showFirstRunClings();
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
505
Sunny Goyal7779d622015-06-11 16:18:39 -0700506 @Override
507 public void onSettingsChanged(String settings, boolean value) {
508 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
509 mRotationEnabled = value;
510 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
511 mUpdateOrientationRunnable.run();
512 }
513 }
514 }
515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
525 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
526 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700527 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700528 private boolean mWorkspaceImportanceStored = false;
529 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700530 private int mWorkspaceImportanceForAccessibility =
531 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
532 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
533
534 @Override
535 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700536 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700537 return;
538 }
539 // The underlying workspace and hotseat are temporarily suppressed by the search
540 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700541 if (mWorkspace != null) {
542 mWorkspaceImportanceForAccessibility =
543 mWorkspace.getImportantForAccessibility();
544 mWorkspace.setImportantForAccessibility(
545 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
546 mWorkspaceImportanceStored = true;
547 }
548 if (mHotseat != null) {
549 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
550 mHotseat.setImportantForAccessibility(
551 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
552 mHotseatImportanceStored = true;
553 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700554 }
555
556 @Override
557 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700558 if (mWorkspaceImportanceStored && mWorkspace != null) {
559 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
560 }
561 if (mHotseatImportanceStored && mHotseat != null) {
562 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
563 }
564 mWorkspaceImportanceStored = false;
565 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700566 }
567 });
Adam Cohen9211d422014-10-07 18:14:53 -0700568 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700569 }
570
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700571 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700572 public void onLauncherProviderChange() {
573 if (mLauncherCallbacks != null) {
574 mLauncherCallbacks.onLauncherProviderChange();
575 }
576 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700577
Winson Chungef7f8742015-06-04 17:18:17 -0700578 /**
579 * Updates the bounds of all the overlays to match the new fixed bounds.
580 */
581 public void updateOverlayBounds(Rect newBounds) {
582 mAppsView.setSearchBarBounds(newBounds);
583 mWidgetsView.setSearchBarBounds(newBounds);
584 }
585
Adam Cohen9211d422014-10-07 18:14:53 -0700586 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700587 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700588 if (mLauncherCallbacks != null) {
589 return mLauncherCallbacks.hasCustomContentToLeft();
590 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 return false;
592 }
593
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500595 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 * {@link #addToCustomContentPage}. This will only be invoked if
597 * {@link #hasCustomContentToLeft()} is {@code true}.
598 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700600 if (mLauncherCallbacks != null) {
601 mLauncherCallbacks.populateCustomContentContainer();
602 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600603 }
604
605 /**
606 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
607 * ensure the custom content page is added or removed if necessary.
608 */
609 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600610 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 // Not bound yet, wait for bindScreens to be called.
612 return;
613 }
614
615 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
616 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500617 mWorkspace.createCustomContentContainer();
618 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
620 mWorkspace.removeCustomContentPage();
621 }
622 }
623
Anton Hanssona2f665f2013-09-26 12:18:32 +0100624 public Stats getStats() {
625 return mStats;
626 }
627
Hyunyoung Song3f471442015-04-08 19:01:34 -0700628 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700629 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
630 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700631 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700632 }
633
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000634 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700635 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
636 // This cast is safe as long as the id < 0x00FFFFFF
637 // Since we jail all the dynamically generated views, there should be no clashes
638 // with any other views.
639 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000640 }
641
642 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700643 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
644 * a configuration step, this allows the proper animations to run after other transitions.
645 */
Adam Cohendb364c32014-05-20 14:23:40 -0700646 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700647 long screenId = args.screenId;
648 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
649 // When the screen id represents an actual screen (as opposed to a rank) we make sure
650 // that the drop page actually exists.
651 screenId = ensurePendingDropLayoutExists(args.screenId);
652 }
Adam Cohendb364c32014-05-20 14:23:40 -0700653
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800654 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800655 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700656 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700657 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700658 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700660 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700661 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700662 case REQUEST_RECONFIGURE_APPWIDGET:
663 completeRestoreAppWidget(args.appWidgetId);
664 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 }
Michael Jurka27614d22012-04-02 06:40:22 -0700666 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
667 // if you turned the screen off and then back while in All Apps, Launcher would not
668 // return to the workspace. Clearing mAddInfo.container here fixes this issue
669 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700670 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 }
672
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800673 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700674 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700675 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700676 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700677 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800678 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700679
Adam Cohenad4e15c2013-10-17 16:21:35 -0700680 Runnable exitSpringLoaded = new Runnable() {
681 @Override
682 public void run() {
683 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
684 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
685 }
686 };
687
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700689 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700690 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
692 if (resultCode == RESULT_CANCELED) {
693 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700694 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700695 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700696 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800697 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700698 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700699
700 // When the user has granted permission to bind widgets, we should check to see if
701 // we can inflate the default search bar widget.
702 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 }
704 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200705 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
706 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700707 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200708 }
709 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700710 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200711
Adam Cohened66b2b2012-01-23 17:28:51 -0800712 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700713 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700714
Adam Cohendb364c32014-05-20 14:23:40 -0700715 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 // We have special handling for widgets
717 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800718 final int appWidgetId;
719 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
720 : -1;
721 if (widgetId < 0) {
722 appWidgetId = pendingAddWidgetId;
723 } else {
724 appWidgetId = widgetId;
725 }
726
Adam Cohenad4e15c2013-10-17 16:21:35 -0700727 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800728 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700729 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
730 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700731 result = RESULT_CANCELED;
732 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700733 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700734 @Override
735 public void run() {
736 exitSpringLoadedDragModeDelayed(false, 0, null);
737 }
738 };
Adam Cohendb364c32014-05-20 14:23:40 -0700739 if (workspaceLocked) {
740 // No need to remove the empty screen if we're mid-binding, as the
741 // the bind will not add the empty screen.
742 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
743 } else {
744 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
745 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
746 }
Winson Chung5aaab772012-04-27 15:24:02 -0700747 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700748 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700749 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
750 // When the screen id represents an actual screen (as opposed to a rank)
751 // we make sure that the drop page actually exists.
752 mPendingAddInfo.screenId =
753 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
754 }
Adam Cohendb364c32014-05-20 14:23:40 -0700755 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
756
757 dropLayout.setDropPending(true);
758 final Runnable onComplete = new Runnable() {
759 @Override
760 public void run() {
761 completeTwoStageWidgetDrop(resultCode, appWidgetId);
762 dropLayout.setDropPending(false);
763 }
764 };
765 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
766 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
767 } else {
768 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
769 mPendingAddInfo);
770 sPendingAddItem = args;
771 }
Winson Chung5aaab772012-04-27 15:24:02 -0700772 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 return;
774 }
775
Sunny Goyalff572272014-07-23 13:58:07 -0700776 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
777 if (resultCode == RESULT_OK) {
778 // Update the widget view.
779 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
780 pendingAddWidgetId, mPendingAddInfo);
781 if (workspaceLocked) {
782 sPendingAddItem = args;
783 } else {
784 completeAdd(args);
785 }
786 }
787 // Leave the widget in the pending state if the user canceled the configure.
788 return;
789 }
790
Sunny Goyalaad90582015-07-09 14:22:50 -0700791 if (requestCode == REQUEST_CREATE_SHORTCUT) {
792 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
793 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
794 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
795 mPendingAddInfo);
796 if (isWorkspaceLocked()) {
797 sPendingAddItem = args;
798 } else {
799 completeAdd(args);
800 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
802 }
803 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
805 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800809
810 }
811
812 @Override
813 protected void onActivityResult(
814 final int requestCode, final int resultCode, final Intent data) {
815 handleActivityResult(requestCode, resultCode, data);
816 if (mLauncherCallbacks != null) {
817 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
818 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 }
820
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600821 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700822 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600823 int[] grantResults) {
824 if (mLauncherCallbacks != null) {
825 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
826 grantResults);
827 }
828 }
829
Adam Cohendb364c32014-05-20 14:23:40 -0700830 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
831 appWidgetId, ItemInfo info) {
832 PendingAddArguments args = new PendingAddArguments();
833 args.requestCode = requestCode;
834 args.intent = data;
835 args.container = info.container;
836 args.screenId = info.screenId;
837 args.cellX = info.cellX;
838 args.cellY = info.cellY;
839 args.appWidgetId = appWidgetId;
840 return args;
841 }
842
843 /**
844 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
845 *
846 * @param screenId the screen id to check
847 * @return the new screen, or screenId if it exists
848 */
849 private long ensurePendingDropLayoutExists(long screenId) {
850 CellLayout dropLayout =
851 (CellLayout) mWorkspace.getScreenWithId(screenId);
852 if (dropLayout == null) {
853 // it's possible that the add screen was removed because it was
854 // empty and a re-bind occurred
855 mWorkspace.addExtraEmptyScreen();
856 return mWorkspace.commitExtraEmptyScreen();
857 } else {
858 return screenId;
859 }
860 }
861
Adam Cohen091440a2015-03-18 14:16:05 -0700862 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700863 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 Runnable onCompleteRunnable = null;
866 int animationType = 0;
867
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700868 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 if (resultCode == RESULT_OK) {
870 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
871 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700872 mPendingAddWidgetInfo);
873 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 onCompleteRunnable = new Runnable() {
875 @Override
876 public void run() {
877 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700878 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700879 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
880 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 }
882 };
883 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800884 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700887 if (mDragLayer.getAnimatedView() != null) {
888 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
889 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
890 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700891 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700892 // The animated view may be null in the case of a rotation during widget configuration
893 onCompleteRunnable.run();
894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700898 protected void onStop() {
899 super.onStop();
900 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700901
902 if (mLauncherCallbacks != null) {
903 mLauncherCallbacks.onStop();
904 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700905 }
906
907 @Override
908 protected void onStart() {
909 super.onStart();
910 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700911
912 if (mLauncherCallbacks != null) {
913 mLauncherCallbacks.onStart();
914 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700915 }
916
917 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200919 long startTime = 0;
920 if (DEBUG_RESUME_TIME) {
921 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400922 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200923 }
Adam Cohen9211d422014-10-07 18:14:53 -0700924
925 if (mLauncherCallbacks != null) {
926 mLauncherCallbacks.preOnResume();
927 }
928
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700930
Winson Chung4a2afa32012-07-19 14:53:05 -0700931 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700932 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700933 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800934 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700935 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700936 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700937 // view after launching an app, as they may be depending on the UI to be static to
938 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700939 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700940 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800941 } else if (mOnResumeState == State.WIDGETS) {
942 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700943 }
944 mOnResumeState = State.NONE;
945
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700946 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700947 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
948 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700949
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800950 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700951 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700952 setWorkspaceLoading(true);
Sunny Goyal22aa3812015-08-19 16:24:27 -0700953
954 // If we're starting binding all over again, clear any bind calls we'd postponed in
955 // the past (see waitUntilResume) -- we don't need them since we're starting binding
956 // from scratch again
957 mBindOnResumeCallbacks.clear();
958
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700959 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400960 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700961 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700963 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200964 // We might have postponed some bind calls until onResume (see waitUntilResume) --
965 // execute them here
966 long startTimeCallbacks = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTimeCallbacks = System.currentTimeMillis();
969 }
970
Michael Jurka1e2f4652013-07-08 18:03:46 -0700971 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
972 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200973 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700974 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200975 if (DEBUG_RESUME_TIME) {
976 Log.d(TAG, "Time spent processing callbacks in onResume: " +
977 (System.currentTimeMillis() - startTimeCallbacks));
978 }
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Michael Jurka54554252013-08-01 12:52:23 +0200980 if (mOnResumeCallbacks.size() > 0) {
981 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
982 mOnResumeCallbacks.get(i).run();
983 }
984 mOnResumeCallbacks.clear();
985 }
Winson Chunge4e50662012-01-23 14:45:13 -0800986
987 // Reset the pressed state of icons that were locked in the press state while activities
988 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800989 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800990 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800991 mWaitingForResume.setStayPressed(false);
992 }
Winson Chung82963d52013-10-09 11:20:57 -0700993
Adam Cohen06dff352012-06-01 17:17:08 -0700994 // It is possible that widgets can receive updates while launcher is not in the foreground.
995 // Consequently, the widgets will be inflated in the orientation of the foreground activity
996 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
997 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700998 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -0700999 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001000
Michael Jurka447bf842013-05-15 14:52:15 +02001001 if (DEBUG_RESUME_TIME) {
1002 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1003 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001004
1005 if (mWorkspace.getCustomContentCallbacks() != null) {
1006 // If we are resuming and the custom content is the current page, we call onShow().
1007 // It is also poassible that onShow will instead be called slightly after first layout
1008 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1009 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001010 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001011 }
1012 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001013 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001014 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001015
Sunny Goyal756adbc2015-04-16 15:20:51 -07001016 if (!isWorkspaceLoading()) {
1017 // Process any items that were added while Launcher was away.
1018 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1019 }
Adam Cohen9211d422014-10-07 18:14:53 -07001020
1021 if (mLauncherCallbacks != null) {
1022 mLauncherCallbacks.onResume();
1023 }
Adam Cohen06dff352012-06-01 17:17:08 -07001024 }
1025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001027 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001028 // Ensure that items added to Launcher are queued until Launcher returns
1029 InstallShortcutReceiver.enableInstallQueue();
1030
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001032 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001033 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001034 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001035
1036 // We call onHide() aggressively. The custom content callbacks should be able to
1037 // debounce excess onHide calls.
1038 if (mWorkspace.getCustomContentCallbacks() != null) {
1039 mWorkspace.getCustomContentCallbacks().onHide();
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
Adam Cohen9211d422014-10-07 18:14:53 -07001042 if (mLauncherCallbacks != null) {
1043 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001044 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001045 }
1046
1047 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001048 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1049 // by a onResume or by scrolling otherwise.
1050 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001051
1052 // Custom content is completely hidden
1053 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001054
1055 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1056 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001057
1058 // Indicates whether the user is allowed to scroll away from the custom content.
1059 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001060 }
1061
Adam Cohenc2d6e892014-10-16 09:49:24 -07001062 public interface LauncherOverlay {
1063
1064 /**
1065 * Touch interaction leading to overscroll has begun
1066 */
1067 public void onScrollInteractionBegin();
1068
1069 /**
1070 * Touch interaction related to overscroll has ended
1071 */
1072 public void onScrollInteractionEnd();
1073
1074 /**
1075 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1076 * screen (or in the case of RTL, the rightmost screen).
1077 */
1078 public void onScrollChange(int progress, boolean rtl);
1079
1080 /**
1081 * Screen has stopped scrolling
1082 */
1083 public void onScrollSettled();
1084
1085 /**
1086 * This method can be called by the Launcher in order to force the LauncherOverlay
1087 * to exit fully immersive mode.
1088 */
1089 public void forceExitFullImmersion();
1090 }
1091
Jun Mukai8af0cd82015-05-12 12:32:12 -07001092 public interface LauncherSearchCallbacks {
1093 /**
1094 * Called when the search overlay is shown.
1095 */
1096 public void onSearchOverlayOpened();
1097
1098 /**
1099 * Called when the search overlay is dismissed.
1100 */
1101 public void onSearchOverlayClosed();
1102 }
1103
Adam Cohenc2d6e892014-10-16 09:49:24 -07001104 public interface LauncherOverlayCallbacks {
1105 /**
1106 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1107 * however it doesn't modify any state within the launcher.
1108 */
1109 public boolean canEnterFullImmersion();
1110
1111 /**
1112 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1113 * eg. by occupying the full screen and handling all touch events.
1114 *
1115 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1116 * case, Launcher will modify any necessary state and assumes the overlay is
1117 * handling all interaction. If false, the LauncherOverlay should cancel any
1118 *
1119 */
1120 public boolean enterFullImmersion();
1121
1122 /**
1123 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1124 * full control over UI and state.
1125 */
1126 public void exitFullImmersion();
1127 }
1128
1129 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1130
1131 @Override
1132 public boolean canEnterFullImmersion() {
1133 return mState == State.WORKSPACE;
1134 }
1135
1136 @Override
1137 public boolean enterFullImmersion() {
1138 if (mState == State.WORKSPACE) {
1139 // When fully immersed, disregard any touches which fall through.
1140 mDragLayer.setBlockTouch(true);
1141 return true;
1142 }
1143 return false;
1144 }
1145
1146 @Override
1147 public void exitFullImmersion() {
1148 mDragLayer.setBlockTouch(false);
1149 }
1150 }
1151
Jorim Jaggid017f882014-01-14 17:08:48 -08001152 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001153 if (mLauncherCallbacks != null) {
1154 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001155 } else {
1156 // On devices with a locked orientation, we will at least have the allow rotation
1157 // setting.
1158 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001159 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001160 }
1161
Adam Cohen9211d422014-10-07 18:14:53 -07001162 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001163 CustomContentCallbacks callbacks, String description) {
1164 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001165 }
1166
Adam Cohenedb40762013-07-18 16:45:45 -07001167 // The custom content needs to offset its content to account for the QSB
1168 public int getTopOffsetForCustomContent() {
1169 return mWorkspace.getPaddingTop();
1170 }
1171
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001172 @Override
1173 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001175 if (mModel.isCurrentCallbacks(this)) {
1176 mModel.stopLoader();
1177 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001178 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1179
Romain Guy13c2e7b2010-03-10 19:45:00 -08001180 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001181 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001183 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001184 @Override
1185 public void onWindowFocusChanged(boolean hasFocus) {
1186 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001187 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001188
1189 if (mLauncherCallbacks != null) {
1190 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1191 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001192 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 private boolean acceptFilter() {
1195 final InputMethodManager inputManager = (InputMethodManager)
1196 getSystemService(Context.INPUT_METHOD_SERVICE);
1197 return !inputManager.isFullscreenMode();
1198 }
1199
1200 @Override
1201 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001202 final int uniChar = event.getUnicodeChar();
1203 final boolean handled = super.onKeyDown(keyCode, event);
1204 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1205 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1207 keyCode, event);
1208 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001209 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001210 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001211 // showSearchDialog()
1212 // If there are multiple keystrokes before the search dialog takes focus,
1213 // onSearchRequested() will be called for every keystroke,
1214 // but it is idempotent, so it's fine.
1215 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217 }
1218
Joe Onorato8a9625e2010-01-28 15:55:35 -08001219 // Eat the long press event so the keyboard doesn't come up.
1220 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1221 return true;
1222 }
1223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 return handled;
1225 }
1226
Karl Rosaen138a0412009-04-23 19:00:21 -07001227 private String getTypedText() {
1228 return mDefaultKeySsb.toString();
1229 }
1230
1231 private void clearTypedText() {
1232 mDefaultKeySsb.clear();
1233 mDefaultKeySsb.clearSpans();
1234 Selection.setSelection(mDefaultKeySsb, 0);
1235 }
1236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001238 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1239 * State
1240 */
1241 private static State intToState(int stateOrdinal) {
1242 State state = State.WORKSPACE;
1243 final State[] stateValues = State.values();
1244 for (int i = 0; i < stateValues.length; i++) {
1245 if (stateValues[i].ordinal() == stateOrdinal) {
1246 state = stateValues[i];
1247 break;
1248 }
1249 }
1250 return state;
1251 }
1252
1253 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 * Restores the previous state, if it exists.
1255 *
1256 * @param savedState The previous state.
1257 */
1258 private void restoreState(Bundle savedState) {
1259 if (savedState == null) {
1260 return;
1261 }
1262
Michael Jurka883f55b2010-10-21 15:47:14 -07001263 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001264 if (state == State.APPS || state == State.WIDGETS) {
1265 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001266 }
1267
Adam Cohen21cd0022013-10-09 18:57:02 -07001268 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1269 PagedView.INVALID_RESTORE_PAGE);
1270 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001271 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
Sunny Goyal756cd262015-08-20 12:33:21 -07001274 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1275 if (itemValues != null) {
1276 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001277 AppWidgetProviderInfo info = savedState.getParcelable(
1278 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001279 mPendingAddWidgetInfo = info == null ?
1280 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1281
Adam Cohen4637b5a2013-11-04 18:21:24 -08001282 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001283 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 mRestoring = true;
1285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
1288 /**
1289 * Finds all the views we need and configure them properly.
1290 */
1291 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001292 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001293
Craig Mautner360310b2012-10-26 15:13:08 -07001294 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001295 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001296 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1297 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001298 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001299 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001300
John Spurlock77e1f472013-09-11 10:09:51 -04001301 mLauncherView.setSystemUiVisibility(
1302 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001303 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
Winson Chung4c98d922011-05-31 16:50:48 -07001305 // Setup the drag layer
1306 mDragLayer.setup(this, dragController);
1307
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 // Setup the hotseat
1309 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1310 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001311 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 }
1313
Jorim Jaggid017f882014-01-14 17:08:48 -08001314 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Tony Wickhamc75917f2015-09-02 16:11:09 -07001315 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1316 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1317 @Override
1318 public boolean onLongClick(View v) {
1319 return v.performClick();
1320 }
1321 };
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001322 mWidgetsButton = findViewById(R.id.widget_button);
1323 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001324 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001325 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001326 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001327 onClickAddWidgetButton(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001328 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001329 }
1330 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001331 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001332 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001333
1334 View wallpaperButton = findViewById(R.id.wallpaper_button);
1335 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001336 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001337 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001338 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001339 onClickWallpaperPicker(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001340 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001341 }
1342 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001343 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
Adam Cohen61f560d2013-09-30 15:58:20 -07001344 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1345
1346 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001347 if (hasSettings()) {
1348 settingsButton.setOnClickListener(new OnClickListener() {
1349 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001350 public void onClick(View view) {
Jorim Jaggid017f882014-01-14 17:08:48 -08001351 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001352 onClickSettingsButton(view);
Jorim Jaggid017f882014-01-14 17:08:48 -08001353 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001354 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001355 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001356 settingsButton.setOnLongClickListener(performClickOnLongClick);
Jorim Jaggid017f882014-01-14 17:08:48 -08001357 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1358 } else {
1359 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001360 }
1361
Adam Cohendbdff6b2013-09-18 19:09:15 -07001362 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363
Winson Chung4c98d922011-05-31 16:50:48 -07001364 // Setup the workspace
1365 mWorkspace.setHapticFeedbackEnabled(false);
1366 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001367 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001368 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001369
Tony Wickham34d2c912015-09-11 09:27:58 -07001370 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001371 mSearchDropTargetBar = (SearchDropTargetBar)
1372 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001373 // Get the app info bar
1374 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1375 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001376
Winson Chungef7f8742015-06-04 17:18:17 -07001377 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001378 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001379 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001380 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1381 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1382 } else {
1383 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1384 }
Craig Mautner360310b2012-10-26 15:13:08 -07001385
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001387 dragController.setDragScoller(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001388 dragController.setMoveTarget(mWorkspace);
1389 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001390 if (mSearchDropTargetBar != null) {
1391 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001392 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001393 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001394 if (mAppInfoDropTargetBar != null) {
1395 mAppInfoDropTargetBar.setup(this, dragController);
1396 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001397
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001398 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001399 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001400 mWeightWatcher = new WeightWatcher(this);
1401 mWeightWatcher.setAlpha(0.5f);
1402 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001403 new FrameLayout.LayoutParams(
1404 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001405 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001406 Gravity.BOTTOM)
1407 );
Adam Cohen39a06042013-07-19 14:30:12 -07001408
1409 boolean show = shouldShowWeightWatcher();
1410 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413
1414 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001415 * Sets the all apps button. This method is called from {@link Hotseat}.
1416 */
1417 public void setAllAppsButton(View allAppsButton) {
1418 mAllAppsButton = allAppsButton;
1419 }
1420
1421 public View getAllAppsButton() {
1422 return mAllAppsButton;
1423 }
1424
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001425 public View getWidgetsButton() {
1426 return mWidgetsButton;
1427 }
1428
Anjali Koppal5ad44842014-03-10 20:34:39 -07001429 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 * Creates a view representing a shortcut.
1431 *
1432 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001434 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001435 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437
1438 /**
1439 * Creates a view representing a shortcut inflated from the specified resource.
1440 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 * @param parent The group the shortcut belongs to.
1442 * @param info The data structure describing the shortcut.
1443 *
1444 * @return A View inflated from layoutResId.
1445 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001446 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001447 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001448 parent, false);
1449 favorite.applyFromShortcutInfo(info, mIconCache);
1450 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001452 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 return favorite;
1454 }
1455
1456 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 * Add a shortcut to the workspace.
1458 *
1459 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001461 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001462 int cellY) {
1463 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001464 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Sunny Goyal5c97f512015-05-19 16:03:28 -07001466 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001467 if (info == null) {
1468 return;
1469 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001470 final View view = createShortcut(info);
1471
Sunny Goyal5c97f512015-05-19 16:03:28 -07001472 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001473 // First we check if we already know the exact location where we want to add this item.
1474 if (cellX >= 0 && cellY >= 0) {
1475 cellXY[0] = cellX;
1476 cellXY[1] = cellY;
1477 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001478
1479 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001480 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001481 true, null,null)) {
1482 return;
1483 }
1484 DragObject dragObject = new DragObject();
1485 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001486 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1487 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001488 return;
1489 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001490 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001491 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001492 }
1493
1494 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001495 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001496 return;
1497 }
1498
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001499 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
1501 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505 }
1506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001508 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001510 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohen091440a2015-03-18 14:16:05 -07001512 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001513 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1514
1515 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001516 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001517 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1518 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Adam Cohen59400422014-03-05 18:07:04 -08001521 if (appWidgetInfo.isCustomWidget) {
1522 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523 }
1524
Adam Cohen59400422014-03-05 18:07:04 -08001525 LauncherAppWidgetInfo launcherInfo;
1526 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1527 launcherInfo.spanX = info.spanX;
1528 launcherInfo.spanY = info.spanY;
1529 launcherInfo.minSpanX = info.minSpanX;
1530 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001531 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001534 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001537 if (hostView == null) {
1538 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001539 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1540 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 } else {
1542 // The AppWidgetHostView has already been inflated and instantiated
1543 launcherInfo.hostView = hostView;
1544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001546 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001547 launcherInfo.notifyWidgetSizeChanged(this);
1548
Adam Cohen59400422014-03-05 18:07:04 -08001549 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1550 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001551
1552 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001554 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
Romain Guycbb89e42009-06-08 15:52:54 -07001556
Adam Cohended9f8d2010-11-03 13:25:16 -07001557 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1558 @Override
1559 public void onReceive(Context context, Intent intent) {
1560 final String action = intent.getAction();
1561 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1562 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001563 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001564 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001565
Winson Chungc100e8e2011-08-09 16:02:43 -07001566 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001567 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001568 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001569 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001570 if (!showWorkspace(false)) {
1571 // If we are already on the workspace, then manually reset all apps
1572 mAppsView.reset();
1573 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001574 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1576 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001577 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001578 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1579 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001580 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001581 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1582 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1583 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001584 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001585 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1586 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 }
1588 }
1589 };
1590
1591 @Override
1592 public void onAttachedToWindow() {
1593 super.onAttachedToWindow();
1594
1595 // Listen for broadcasts related to user-presence
1596 final IntentFilter filter = new IntentFilter();
1597 filter.addAction(Intent.ACTION_SCREEN_OFF);
1598 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001599 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001600 if (ENABLE_DEBUG_INTENTS) {
1601 filter.addAction(DebugIntents.DELETE_DATABASE);
1602 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1603 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001604 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001605 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001606 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001607 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 mVisible = true;
1609 }
1610
Adam Cohen0b395352014-06-09 22:54:36 +00001611 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001612 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001613 * This method is a no-op for other platform versions.
1614 */
Sunny Goyald0091012015-01-20 15:55:34 -08001615 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001616 private void setupTransparentSystemBarsForLollipop() {
1617 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001618 Window window = getWindow();
1619 window.getAttributes().systemUiVisibility |=
1620 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1621 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1622 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1623 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1624 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1625 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1626 window.setStatusBarColor(Color.TRANSPARENT);
1627 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001628 }
1629 }
1630
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 @Override
1632 public void onDetachedFromWindow() {
1633 super.onDetachedFromWindow();
1634 mVisible = false;
1635
Adam Cohend113e0c2010-11-11 10:48:05 -08001636 if (mAttached) {
1637 unregisterReceiver(mReceiver);
1638 mAttached = false;
1639 }
Winson Chungb745afb2015-03-02 11:51:23 -08001640 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 }
1642
1643 public void onWindowVisibilityChanged(int visibility) {
1644 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001645 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 // The following code used to be in onResume, but it turns out onResume is called when
1647 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1648 // is a more appropriate event to handle
1649 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001650 if (!mWorkspaceLoading) {
1651 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001652 // We want to let Launcher draw itself at least once before we force it to build
1653 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001654 // apps is nice and speedy.
1655 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001656 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001657 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001658 if (mStarted) return;
1659 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001660 // We delay the layer building a bit in order to give
1661 // other message processing a time to run. In particular
1662 // this avoids a delay in hiding the IME if it was
1663 // currently shown, because doing that may involve
1664 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001665 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001666 final ViewTreeObserver.OnDrawListener listener = this;
1667 mWorkspace.post(new Runnable() {
1668 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001669 if (mWorkspace != null &&
1670 mWorkspace.getViewTreeObserver() != null) {
1671 mWorkspace.getViewTreeObserver().
1672 removeOnDrawListener(listener);
1673 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 }
1675 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001676 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001677 }
1678 });
1679 }
1680 clearTypedText();
1681 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 }
1683
Adam Cohen091440a2015-03-18 14:16:05 -07001684 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 mHandler.removeMessages(ADVANCE_MSG);
1686 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1687 mHandler.sendMessageDelayed(msg, delay);
1688 mAutoAdvanceSentTime = System.currentTimeMillis();
1689 }
1690
Adam Cohen091440a2015-03-18 14:16:05 -07001691 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1693 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1694 mAutoAdvanceRunning = autoAdvanceRunning;
1695 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001696 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 sendAdvanceMessage(delay);
1698 } else {
1699 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001700 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1702 }
1703 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001704 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 }
1706 }
1707 }
1708
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001709 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1710
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001712 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 if (msg.what == ADVANCE_MSG) {
1714 int i = 0;
1715 for (View key: mWidgetsToAdvance.keySet()) {
1716 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001717 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001719 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 public void run() {
1721 ((Advanceable) v).advance();
1722 }
1723 }, delay);
1724 }
1725 i++;
1726 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001727 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001729 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001731 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001732
Winsonc0b52fe2015-09-09 16:38:15 -07001733 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001734 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1736 if (v instanceof Advanceable) {
1737 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001738 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741 }
1742
Winsonc0b52fe2015-09-09 16:38:15 -07001743 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 if (mWidgetsToAdvance.containsKey(hostView)) {
1745 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001746 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001748 }
1749
Hyunyoung Song3f471442015-04-08 19:01:34 -07001750 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001751 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1752 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754
Winson Chunga6945242014-01-08 14:04:34 -08001755 public DragLayer getDragLayer() {
1756 return mDragLayer;
1757 }
1758
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001759 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001760 return mAppsView;
1761 }
1762
Hyunyoung Song3f471442015-04-08 19:01:34 -07001763 public WidgetsContainerView getWidgetsView() {
1764 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001765 }
1766
Winson Chunga6945242014-01-08 14:04:34 -08001767 public Workspace getWorkspace() {
1768 return mWorkspace;
1769 }
1770
1771 public Hotseat getHotseat() {
1772 return mHotseat;
1773 }
1774
Jorim Jaggid017f882014-01-14 17:08:48 -08001775 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001776 return mOverviewPanel;
1777 }
1778
Winson Chung006ee262015-08-03 14:40:11 -07001779 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001780 return mSearchDropTargetBar;
1781 }
1782
Tony Wickham34d2c912015-09-11 09:27:58 -07001783 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1784 return mAppInfoDropTargetBar;
1785 }
1786
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001787 public LauncherAppWidgetHost getAppWidgetHost() {
1788 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Winson Chunga9abd0e2010-10-27 17:18:37 -07001791 public LauncherModel getModel() {
1792 return mModel;
1793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 protected SharedPreferences getSharedPrefs() {
1796 return mSharedPrefs;
1797 }
1798
Adam Cohen2e6da152015-05-06 11:42:25 -07001799 public DeviceProfile getDeviceProfile() {
1800 return mDeviceProfile;
1801 }
1802
Bjorn Bringertc459e522013-06-07 19:36:01 +01001803 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001804 getWindow().closeAllPanels();
1805
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001806 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001807 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001812 long startTime = 0;
1813 if (DEBUG_RESUME_TIME) {
1814 startTime = System.currentTimeMillis();
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 super.onNewIntent(intent);
1817
1818 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001819 Folder openFolder = mWorkspace.getOpenFolder();
1820 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1821 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1822 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1823 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1824 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001825 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001826 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001827
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 if (mWorkspace == null) {
1829 // Can be cases where mWorkspace is null, this prevents a NPE
1830 return;
1831 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 // In all these cases, only animate if we're already on home
1833 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001834
Adam Cohen6fecd412013-10-02 17:41:50 -07001835 closeFolder();
1836 exitSpringLoadedDragMode();
1837
1838 // If we are already on home, then just animate back to the workspace,
1839 // otherwise, just wait until onResume to set the state back to Workspace
1840 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001841 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001842 } else {
1843 mOnResumeState = State.WORKSPACE;
1844 }
1845
1846 final View v = getWindow().peekDecorView();
1847 if (v != null && v.getWindowToken() != null) {
1848 InputMethodManager imm = (InputMethodManager)getSystemService(
1849 INPUT_METHOD_SERVICE);
1850 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1851 }
1852
Winson Chungb745afb2015-03-02 11:51:23 -08001853 // Reset the apps view
1854 if (!alreadyOnHome && mAppsView != null) {
1855 mAppsView.scrollToTop();
1856 }
1857
Hyunyoung Song3f471442015-04-08 19:01:34 -07001858 // Reset the widgets view
1859 if (!alreadyOnHome && mWidgetsView != null) {
1860 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001861 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001862
Adam Cohen9211d422014-10-07 18:14:53 -07001863 if (mLauncherCallbacks != null) {
1864 mLauncherCallbacks.onHomeIntent();
1865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Adam Cohened307df2013-10-02 09:37:31 -07001867
Adam Cohen9211d422014-10-07 18:14:53 -07001868 if (mLauncherCallbacks != null) {
1869 mLauncherCallbacks.onNewIntent(intent);
1870 }
Winson15f8b172015-08-19 11:02:39 -07001871
1872 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1873 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1874 // animation.
1875 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001876 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1877 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001878 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1879 openFolder == null && moveToDefaultScreen) {
1880 mWorkspace.post(new Runnable() {
1881 @Override
1882 public void run() {
1883 mWorkspace.moveToDefaultScreen(true);
1884 }
1885 });
1886 }
1887 }
1888
1889 if (DEBUG_RESUME_TIME) {
1890 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1891 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001892 }
1893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001895 public void onRestoreInstanceState(Bundle state) {
1896 super.onRestoreInstanceState(state);
1897 for (int page: mSynchronouslyBoundPages) {
1898 mWorkspace.restoreInstanceStateForChild(page);
1899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901
1902 @Override
1903 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001904 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001905 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1906 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001907 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001908 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909
Michael Jurka883f55b2010-10-21 15:47:14 -07001910 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001911 // We close any open folder since it will not be re-opened, and we need to make sure
1912 // this state is reflected.
1913 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001917 ContentValues itemValues = new ContentValues();
1918 mPendingAddInfo.writeToValues(itemValues);
1919 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001920 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001921 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Hyunyoung Song3f471442015-04-08 19:01:34 -07001924 // Save the current widgets tray?
1925 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001926
1927 if (mLauncherCallbacks != null) {
1928 mLauncherCallbacks.onSaveInstanceState(outState);
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931
1932 @Override
1933 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001935
Winson Chunge7a03942011-08-05 15:05:12 -07001936 // Remove all pending runnables
1937 mHandler.removeMessages(ADVANCE_MSG);
1938 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001939 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001940
Winson Chungcd2b0142011-06-08 16:02:26 -07001941 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001942 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001943
1944 // It's possible to receive onDestroy after a new Launcher activity has
1945 // been created. In this case, don't interfere with the new Launcher.
1946 if (mModel.isCurrentCallbacks(this)) {
1947 mModel.stopLoader();
1948 app.setLauncher(null);
1949 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001950
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001952 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001954 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001956 mAppWidgetHost = null;
1957
1958 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959
1960 TextKeyListener.getInstance().release();
1961
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001962 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001963
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001964 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001965 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001966 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001967 mWorkspace = null;
1968 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001969
Michael Jurka2ecf9952012-06-18 12:52:28 -07001970 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001971
1972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onDestroy();
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
Adam Cohena9cf38f2011-05-02 15:36:58 -07001977 public DragController getDragController() {
1978 return mDragController;
1979 }
1980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 @Override
1982 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001983 onStartForResult(requestCode);
1984 super.startActivityForResult(intent, requestCode);
1985 }
1986
1987 @Override
1988 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1989 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1990 onStartForResult(requestCode);
1991 try {
1992 super.startIntentSenderForResult(intent, requestCode,
1993 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1994 } catch (IntentSender.SendIntentException e) {
1995 throw new ActivityNotFoundException();
1996 }
1997 }
1998
1999 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002000 if (requestCode >= 0) {
2001 setWaitingForResult(true);
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004
Winson Chung70d72102011-08-12 11:24:35 -07002005 /**
2006 * Indicates that we want global search for this activity by setting the globalSearch
2007 * argument for {@link #startSearch} to true.
2008 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002010 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002012
Karl Rosaen138a0412009-04-23 19:00:21 -07002013 if (initialQuery == null) {
2014 // Use any text typed in the launcher as the initial query
2015 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 if (appSearchData == null) {
2018 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002019 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
Winson Chungadf0c182012-08-23 14:59:07 -07002021 Rect sourceBounds = new Rect();
2022 if (mSearchDropTargetBar != null) {
2023 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2024 }
Romain Guycbb89e42009-06-08 15:52:54 -07002025
Ian Parkinson047036e2014-09-01 15:40:53 +01002026 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002027 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002028 if (clearTextImmediately) {
2029 clearTypedText();
2030 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002031
2032 // We need to show the workspace after starting the search
2033 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002034 }
2035
Ian Parkinson047036e2014-09-01 15:40:53 +01002036 /**
2037 * Start a text search.
2038 *
2039 * @return {@code true} if the search will start immediately, so any further keypresses
2040 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2041 * to buffer keypresses.
2042 */
2043 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002044 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002045 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2046 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2047 sourceBounds);
2048 }
2049
Michael Jurkaa33411c2012-06-14 16:18:21 -07002050 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002051 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002052 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002053 }
2054
2055 /**
2056 * Starts the global search activity. This code is a copied from SearchManager
2057 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002059 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002060 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002061 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2062 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2063 if (globalSearchActivity == null) {
2064 Log.w(TAG, "No global search activity found.");
2065 return;
2066 }
2067 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2068 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2069 intent.setComponent(globalSearchActivity);
2070 // Make sure that we have a Bundle to put source in
2071 if (appSearchData == null) {
2072 appSearchData = new Bundle();
2073 } else {
2074 appSearchData = new Bundle(appSearchData);
2075 }
Adam Cohen9211d422014-10-07 18:14:53 -07002076 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002077 if (!appSearchData.containsKey("source")) {
2078 appSearchData.putString("source", getPackageName());
2079 }
2080 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2081 if (!TextUtils.isEmpty(initialQuery)) {
2082 intent.putExtra(SearchManager.QUERY, initialQuery);
2083 }
2084 if (selectInitialQuery) {
2085 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2086 }
2087 intent.setSourceBounds(sourceBounds);
2088 try {
2089 startActivity(intent);
2090 } catch (ActivityNotFoundException ex) {
2091 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094
Winson Chungbedf9232015-07-10 12:38:30 -07002095 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2096 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2097 return;
2098 }
2099
2100 // If not handled, then just start the provided search intent
2101 startActivitySafely(v, searchIntent, null);
2102 }
2103
Yura4f93ec62014-02-04 14:15:21 +00002104 public boolean isOnCustomContent() {
2105 return mWorkspace.isOnOrMovingToCustomContent();
2106 }
2107
Winson Chung70d72102011-08-12 11:24:35 -07002108 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002109 public boolean onPrepareOptionsMenu(Menu menu) {
2110 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002111 if (!isOnCustomContent()) {
2112 // Close any open folders
2113 closeFolder();
2114 // Stop resizing any widgets
2115 mWorkspace.exitWidgetResizeMode();
2116 if (!mWorkspace.isInOverviewMode()) {
2117 // Show the overview mode
2118 showOverviewMode(true);
2119 } else {
2120 showWorkspace(true);
2121 }
Winson Chunge0298742014-01-17 12:03:00 -08002122 }
Adam Cohen9211d422014-10-07 18:14:53 -07002123 if (mLauncherCallbacks != null) {
2124 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2125 }
2126
Michael Jurkab94f3f82013-09-11 18:08:54 +02002127 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002130 @Override
2131 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002132 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002133 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 }
2136
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public boolean isWorkspaceLocked() {
2138 return mWorkspaceLoading || mWaitingForResult;
2139 }
2140
Adam Cohen517a7f52014-03-01 12:12:59 -08002141 public boolean isWorkspaceLoading() {
2142 return mWorkspaceLoading;
2143 }
2144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002145 private void setWorkspaceLoading(boolean value) {
2146 boolean isLocked = isWorkspaceLocked();
2147 mWorkspaceLoading = value;
2148 if (isLocked != isWorkspaceLocked()) {
2149 onWorkspaceLockedChanged();
2150 }
2151 }
2152
2153 private void setWaitingForResult(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWaitingForResult = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
Adam Cohen9211d422014-10-07 18:14:53 -07002161 protected void onWorkspaceLockedChanged() {
2162 if (mLauncherCallbacks != null) {
2163 mLauncherCallbacks.onWorkspaceLockedChanged();
2164 }
2165 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002166
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2171 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002172 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002173 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002174
Sunny Goyale6b63a32015-01-16 16:14:29 -08002175 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002176 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002177 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2178 }
2179
Sunny Goyale6b63a32015-01-16 16:14:29 -08002180 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002181 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2182 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002183 if (appWidgetInfo.configure != null) {
2184 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002185 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002186
Bjorn Bringert7984c942009-12-09 15:38:25 +00002187 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002188 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2189 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002192 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002193 Runnable onComplete = new Runnable() {
2194 @Override
2195 public void run() {
2196 // Exit spring loaded mode if necessary after adding the widget
2197 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2198 null);
2199 }
2200 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002202 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002203 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 }
2205 }
Romain Guycbb89e42009-06-08 15:52:54 -07002206
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002207 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002208 // Close any folders that may be open.
2209 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002210 mWorkspace.moveToCustomContentScreen(animate);
2211 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002212
2213 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2214 int[] cell, int spanX, int spanY) {
2215 switch (info.itemType) {
2216 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2217 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2218 int span[] = new int[2];
2219 span[0] = spanX;
2220 span[1] = spanY;
2221 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2222 container, screenId, cell, span);
2223 break;
2224 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2225 processShortcutFromDrop(info.componentName, container, screenId, cell);
2226 break;
2227 default:
2228 throw new IllegalStateException("Unknown item type: " + info.itemType);
2229 }
2230 }
2231
Adam Cohenfbba09b2011-07-18 21:43:05 -07002232 /**
2233 * Process a shortcut drop.
2234 *
2235 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002239 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2240 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244
2245 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.cellX = cell[0];
2247 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002248 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249
2250 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2251 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002252 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 }
2254
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 /**
2256 * Process a widget drop.
2257 *
2258 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002262 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2263 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 mPendingAddInfo.minSpanX = info.minSpanX;
2268 mPendingAddInfo.minSpanY = info.minSpanY;
2269
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.cellX = cell[0];
2272 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 if (span != null) {
2275 mPendingAddInfo.spanX = span[0];
2276 mPendingAddInfo.spanY = span[1];
2277 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278
Adam Cohened66b2b2012-01-23 17:28:51 -08002279 AppWidgetHostView hostView = info.boundWidget;
2280 int appWidgetId;
2281 if (hostView != null) {
2282 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002283 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002284
2285 // Clear the boundWidget so that it doesn't get destroyed.
2286 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002287 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002288 // In this case, we either need to start an activity to get permission to bind
2289 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002290 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002291 Bundle options = info.bindOptions;
2292
Sunny Goyalffe83f12014-08-14 17:39:34 -07002293 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2294 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002295 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002296 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002297 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002298 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002299 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2300 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2301 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002302 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2303 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002304 // TODO: we need to make sure that this accounts for the options bundle.
2305 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2307 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002308 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 }
2310
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002313 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 folderInfo.title = getText(R.string.folder_name);
2315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002317 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2318 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002319 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320
2321 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 FolderIcon newFolder =
2323 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002326 // Force measure the new folder icon
2327 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2328 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002329 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 }
Romain Guycbb89e42009-06-08 15:52:54 -07002331
Winsonc0b52fe2015-09-09 16:38:15 -07002332 /**
2333 * Unbinds the view for the specified item, and removes the item and all its children items
2334 * from the database. For folders, this incl udes the folder contents. AppWidgets will also
2335 * have their widget ids deleted.
2336 */
2337 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
2338 if (itemInfo instanceof ShortcutInfo) {
2339 mWorkspace.removeWorkspaceItem(v);
2340 if (deleteFromDb) {
2341 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2342 }
2343 } else if (itemInfo instanceof FolderInfo) {
2344 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2345 unbindFolder(folderInfo);
2346 mWorkspace.removeWorkspaceItem(v);
2347 if (deleteFromDb) {
2348 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2349 }
2350 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2351 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
2352 unbindAppWidget(widgetInfo, deleteFromDb);
2353 if (deleteFromDb) {
2354 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
2355 }
2356 } else {
2357 return false;
2358 }
2359 return true;
2360 }
2361
2362 /**
2363 * Unbinds any launcher references to the folder.
2364 */
2365 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002366 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002367 }
2368
Winsonc0b52fe2015-09-09 16:38:15 -07002369 /**
2370 * Unbinds any launcher references to the widget and deletes the app widget id.
2371 */
2372 private void unbindAppWidget(final LauncherAppWidgetInfo widgetInfo, boolean deleteAppWidgetId) {
2373 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
2374 if (deleteAppWidgetId && appWidgetHost != null &&
2375 !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
2376 // Deleting an app widget ID is a void call but writes to disk before returning
2377 // to the caller...
2378 new AsyncTask<Void, Void, Void>() {
2379 public Void doInBackground(Void ... args) {
2380 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2381 return null;
2382 }
2383 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2384 }
2385 removeWidgetToAutoAdvance(widgetInfo.hostView);
2386 widgetInfo.hostView = null;
2387 }
2388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 @Override
2390 public boolean dispatchKeyEvent(KeyEvent event) {
2391 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2392 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002394 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002395 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002396 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002397 dumpState();
2398 return true;
2399 }
2400 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002401 }
2402 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2403 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002404 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 return true;
2406 }
2407 }
2408
2409 return super.dispatchKeyEvent(event);
2410 }
2411
Joe Onorato88ec0992009-11-19 13:16:06 -08002412 @Override
2413 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002414 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2415 return;
2416 }
2417
Sunny Goyal45478022015-06-08 16:52:41 -07002418 if (mDragController.isDragging()) {
2419 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002420 return;
2421 }
2422
Winson Chungb745afb2015-03-02 11:51:23 -08002423 if (isAppsViewVisible()) {
2424 showWorkspace(true);
2425 } else if (isWidgetsViewVisible()) {
2426 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002427 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002428 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002429 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002430 Folder openFolder = mWorkspace.getOpenFolder();
2431 if (openFolder.isEditingName()) {
2432 openFolder.dismissEditingName();
2433 } else {
2434 closeFolder();
2435 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002436 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002437 mWorkspace.exitWidgetResizeMode();
2438
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002439 // Back button is a no-op here, but give at least some feedback for the button press
2440 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002441 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002442 }
2443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002445 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002446 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002447 @Override
2448 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002449 if (mAppWidgetHost != null) {
2450 mAppWidgetHost.startListening();
2451 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002452 }
2453
2454 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 * Launches the intent referred by the clicked shortcut.
2456 *
2457 * @param v The view representing the clicked shortcut.
2458 */
2459 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002460 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2461 // view has detached (it's possible for this to happen if the view is removed mid touch).
2462 if (v.getWindowToken() == null) {
2463 return;
2464 }
2465
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002466 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002467 return;
2468 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002469
Adam Cohen1697b792013-09-17 19:08:21 -07002470 if (v instanceof Workspace) {
2471 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002472 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002473 }
2474 return;
2475 }
2476
2477 if (v instanceof CellLayout) {
2478 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002479 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2480 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002481 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002482 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002483 }
2484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002486 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002487 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002489 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002490 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002491 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002492 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002493 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002494 } else if (tag instanceof AppInfo) {
2495 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002496 } else if (tag instanceof LauncherAppWidgetInfo) {
2497 if (v instanceof PendingAppWidgetHostView) {
2498 onClickPendingWidget((PendingAppWidgetHostView) v);
2499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 }
2501 }
2502
Sunny Goyal70660032015-05-14 00:07:08 -07002503 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002504 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002505 return false;
2506 }
2507
Michael Jurkaaf442092010-06-10 17:01:57 -07002508 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002509 * Event handler for the app widget view which has not fully restored.
2510 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002511 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002512 if (mIsSafeModeEnabled) {
2513 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2514 return;
2515 }
2516
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002517 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002518 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002519 int widgetId = info.appWidgetId;
2520 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2521 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002522 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2523 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002524 mPendingAddInfo.copyFrom(info);
2525 mPendingAddWidgetId = widgetId;
2526
Sunny Goyalffe83f12014-08-14 17:39:34 -07002527 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2528 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002529 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002530 } else if (info.installProgress < 0) {
2531 // The install has not been queued
2532 final String packageName = info.providerName.getPackageName();
2533 showBrokenAppInstallDialog(packageName,
2534 new DialogInterface.OnClickListener() {
2535 public void onClick(DialogInterface dialog, int id) {
2536 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2537 }
2538 });
2539 } else {
2540 // Download has started.
2541 final String packageName = info.providerName.getPackageName();
2542 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002543 }
2544 }
2545
2546 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002547 * Event handler for the "grid" button that appears on the home screen, which
2548 * enters all apps mode.
2549 *
2550 * @param v The view that was clicked.
2551 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 protected void onClickAllAppsButton(View v) {
2553 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002554 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002555 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002556 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002557
2558 if (mLauncherCallbacks != null) {
2559 mLauncherCallbacks.onClickAllAppsButton(v);
2560 }
Winson Chung76648c52015-07-10 14:33:23 -07002561 }
2562 }
2563
2564 protected void onLongClickAllAppsButton(View v) {
2565 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2566 if (!isAppsViewVisible()) {
2567 showAppsView(true /* animated */, false /* resetListToTop */,
2568 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002569 }
2570 }
2571
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002572 private void showBrokenAppInstallDialog(final String packageName,
2573 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002574 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 .setTitle(R.string.abandoned_promises_title)
2576 .setMessage(R.string.abandoned_promise_explanation)
2577 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2578 .setNeutralButton(R.string.abandoned_clean_this,
2579 new DialogInterface.OnClickListener() {
2580 public void onClick(DialogInterface dialog, int id) {
2581 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2582 mWorkspace.removeAbandonedPromise(packageName, user);
2583 }
2584 })
2585 .create().show();
2586 return;
2587 }
2588
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 /**
2590 * Event handler for an app shortcut click.
2591 *
2592 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2593 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002594 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2596 Object tag = v.getTag();
2597 if (!(tag instanceof ShortcutInfo)) {
2598 throw new IllegalArgumentException("Input must be a Shortcut");
2599 }
2600
2601 // Open shortcut
2602 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002603
2604 if (shortcut.isDisabled != 0) {
2605 int error = R.string.activity_not_available;
2606 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2607 error = R.string.safemode_shortcut_error;
2608 }
2609 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2610 return;
2611 }
2612
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002613 final Intent intent = shortcut.intent;
2614
2615 // Check for special shortcuts
2616 if (intent.getComponent() != null) {
2617 final String shortcutClass = intent.getComponent().getClassName();
2618
2619 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2620 MemoryDumpActivity.startDump(this);
2621 return;
2622 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2623 toggleShowWeightWatcher();
2624 return;
2625 }
2626 }
2627
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002629 if ((v instanceof BubbleTextView)
2630 && shortcut.isPromise()
2631 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002633 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002636 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002643 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002644
2645 if (mLauncherCallbacks != null) {
2646 mLauncherCallbacks.onClickAppShortcut(v);
2647 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 }
2649
Adam Cohen091440a2015-03-18 14:16:05 -07002650 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002652 final ShortcutInfo shortcut;
2653 final Intent intent;
2654 if (tag instanceof ShortcutInfo) {
2655 shortcut = (ShortcutInfo) tag;
2656 intent = shortcut.intent;
2657 int[] pos = new int[2];
2658 v.getLocationOnScreen(pos);
2659 intent.setSourceBounds(new Rect(pos[0], pos[1],
2660 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002661
Sunny Goyal508da152014-08-14 10:53:27 -07002662 } else if (tag instanceof AppInfo) {
2663 shortcut = null;
2664 intent = ((AppInfo) tag).intent;
2665 } else {
2666 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2667 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668
2669 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002670 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671
2672 if (success && v instanceof BubbleTextView) {
2673 mWaitingForResume = (BubbleTextView) v;
2674 mWaitingForResume.setStayPressed(true);
2675 }
2676 }
2677
2678 /**
2679 * Event handler for a folder icon click.
2680 *
2681 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2682 */
2683 protected void onClickFolderIcon(View v) {
2684 if (LOGD) Log.d(TAG, "onClickFolder");
2685 if (!(v instanceof FolderIcon)){
2686 throw new IllegalArgumentException("Input must be a FolderIcon");
2687 }
2688
Sunny Goyal317698b2015-07-29 11:45:41 -07002689 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 FolderIcon folderIcon = (FolderIcon) v;
2691 final FolderInfo info = folderIcon.getFolderInfo();
2692 Folder openFolder = mWorkspace.getFolderForTag(info);
2693
2694 // If the folder info reports that the associated folder is open, then verify that
2695 // it is actually opened. There have been a few instances where this gets out of sync.
2696 if (info.opened && openFolder == null) {
2697 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2698 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2699 info.opened = false;
2700 }
2701
2702 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2703 // Close any open folder
2704 closeFolder();
2705 // Open the requested folder
2706 openFolder(folderIcon);
2707 } else {
2708 // Find the open folder...
2709 int folderScreen;
2710 if (openFolder != null) {
2711 folderScreen = mWorkspace.getPageForView(openFolder);
2712 // .. and close it
2713 closeFolder(openFolder);
2714 if (folderScreen != mWorkspace.getCurrentPage()) {
2715 // Close any folder open on the current screen
2716 closeFolder();
2717 // Pull the folder onto this screen
2718 openFolder(folderIcon);
2719 }
2720 }
2721 }
Adam Cohen9211d422014-10-07 18:14:53 -07002722
2723 if (mLauncherCallbacks != null) {
2724 mLauncherCallbacks.onClickFolderIcon(v);
2725 }
Michael Jurka5130e402011-10-13 04:55:35 -07002726 }
2727
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002728 /**
2729 * Event handler for the (Add) Widgets button that appears after a long press
2730 * on the home screen.
2731 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002732 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002734 if (mIsSafeModeEnabled) {
2735 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2736 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002737 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002738 if (mLauncherCallbacks != null) {
2739 mLauncherCallbacks.onClickAddWidgetButton(view);
2740 }
Adam Cohen9211d422014-10-07 18:14:53 -07002741 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002742 }
2743
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 /**
2745 * Event handler for the wallpaper picker button that appears after a long press
2746 * on the home screen.
2747 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002748 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002749 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002750 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2751 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2752 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2753 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002754 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002755
2756 if (mLauncherCallbacks != null) {
2757 mLauncherCallbacks.onClickWallpaperPicker(v);
2758 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 }
2760
2761 /**
2762 * Event handler for a click on the settings button that appears after a long press
2763 * on the home screen.
2764 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002765 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002767 if (mLauncherCallbacks != null) {
2768 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002769 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002770 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002771 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 }
2773
Adam Cohen61f560d2013-09-30 15:58:20 -07002774 public View.OnTouchListener getHapticFeedbackTouchListener() {
2775 if (mHapticFeedbackTouchListener == null) {
2776 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002777 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002778 @Override
2779 public boolean onTouch(View v, MotionEvent event) {
2780 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2781 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2782 }
2783 return false;
2784 }
2785 };
2786 }
2787 return mHapticFeedbackTouchListener;
2788 }
2789
Adam Cohen9211d422014-10-07 18:14:53 -07002790 public void onDragStarted(View view) {
2791 if (isOnCustomContent()) {
2792 // Custom content screen doesn't participate in drag and drop. If on custom
2793 // content screen, move to default.
2794 moveWorkspaceToDefaultScreen();
2795 }
2796
2797 if (mLauncherCallbacks != null) {
2798 mLauncherCallbacks.onDragStarted(view);
2799 }
2800 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002801
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002802 /**
2803 * Called when the user stops interacting with the launcher.
2804 * This implies that the user is now on the homescreen and is not doing housekeeping.
2805 */
Adam Cohen9211d422014-10-07 18:14:53 -07002806 protected void onInteractionEnd() {
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onInteractionEnd();
2809 }
2810 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002811
2812 /**
2813 * Called when the user starts interacting with the launcher.
2814 * The possible interactions are:
2815 * - open all apps
2816 * - reorder an app shortcut, or a widget
2817 * - open the overview mode.
2818 * This is a good time to stop doing things that only make sense
2819 * when the user is on the homescreen and not doing housekeeping.
2820 */
Adam Cohen9211d422014-10-07 18:14:53 -07002821 protected void onInteractionBegin() {
2822 if (mLauncherCallbacks != null) {
2823 mLauncherCallbacks.onInteractionBegin();
2824 }
2825 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002826
Winson Chungcd99cd32015-04-29 11:03:24 -07002827 /** Updates the interaction state. */
2828 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002829 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002830 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002831 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2832 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002833 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002834 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002835 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002836 onInteractionEnd();
2837 }
2838 }
2839
Kenny Guyf07af7b2014-07-31 11:39:16 +01002840 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 try {
2842 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002843 launcherApps.showAppDetailsForProfile(componentName, user);
2844 } catch (SecurityException e) {
2845 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2846 Log.e(TAG, "Launcher does not have permission to launch settings");
2847 } catch (ActivityNotFoundException e) {
2848 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2849 Log.e(TAG, "Unable to launch settings");
2850 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002851 }
2852
Michael Jurka1e2f4652013-07-08 18:03:46 -07002853 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002854 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2855 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002856 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002857 // System applications cannot be installed. For now, show a toast explaining that.
2858 // We may give them the option of disabling apps this way.
2859 int messageId = R.string.uninstall_system_app_text;
2860 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002861 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002862 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002863 String packageName = componentName.getPackageName();
2864 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002865 Intent intent = new Intent(
2866 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002867 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2868 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002869 if (user != null) {
2870 user.addToIntent(intent, Intent.EXTRA_USER);
2871 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002875 }
2876
Winson Chung8f1eff72015-05-28 17:33:40 -07002877 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002878 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002880 // Only launch using the new animation if the shortcut has not opted out (this is a
2881 // private contract between launcher and may be ignored in the future).
2882 boolean useLaunchAnimation = (v != null) &&
2883 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002884 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2885 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002886
Kenny Guy1317e2d2014-05-08 18:52:50 +01002887 UserHandleCompat user = null;
2888 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2889 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2890 user = userManager.getUserForSerialNumber(serialNumber);
2891 }
2892
2893 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002894 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002895 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002896 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002897 int left = 0, top = 0;
2898 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2899 if (v instanceof TextView) {
2900 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002901 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2902 if (icon != null) {
2903 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002904 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002905 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002906 width = bounds.width();
2907 height = bounds.height();
2908 }
2909 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002910 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2911 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002912 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002913 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002914 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002915 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002916 // On L devices, we use the device default slide-up transition.
2917 // On L MR1 devices, we a custom version of the slide-up transition which
2918 // doesn't have the delay present in the device default.
2919 opts = ActivityOptions.makeCustomAnimation(this,
2920 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002921 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002922 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002923 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002924
2925 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2926 // Could be launching some bookkeeping activity
2927 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002928 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002929 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002930 launcherApps.startActivityForProfile(intent.getComponent(), user,
2931 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002932 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002933 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 } catch (SecurityException e) {
2935 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002936 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002938 "or use the exported attribute for this activity. "
2939 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002941 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002943
Winson Chungbedf9232015-07-10 12:38:30 -07002944 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002945 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002946 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2947 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2948 return false;
2949 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002950 try {
2951 success = startActivity(v, intent, tag);
2952 } catch (ActivityNotFoundException e) {
2953 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2954 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2955 }
2956 return success;
2957 }
2958
Adam Cohen268c4752012-06-06 17:47:33 -07002959 /**
2960 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2961 * in the DragLayer in the exact absolute location of the original FolderIcon.
2962 */
2963 private void copyFolderIconToImage(FolderIcon fi) {
2964 final int width = fi.getMeasuredWidth();
2965 final int height = fi.getMeasuredHeight();
2966
2967 // Lazy load ImageView, Bitmap and Canvas
2968 if (mFolderIconImageView == null) {
2969 mFolderIconImageView = new ImageView(this);
2970 }
2971 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2972 mFolderIconBitmap.getHeight() != height) {
2973 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2974 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2975 }
2976
2977 DragLayer.LayoutParams lp;
2978 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2979 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2980 } else {
2981 lp = new DragLayer.LayoutParams(width, height);
2982 }
2983
Adam Cohen307fe232012-08-16 17:55:58 -07002984 // The layout from which the folder is being opened may be scaled, adjust the starting
2985 // view size by this scale factor.
2986 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002987 lp.customPosition = true;
2988 lp.x = mRectForFolderAnimation.left;
2989 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002990 lp.width = (int) (scale * width);
2991 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002992
2993 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2994 fi.draw(mFolderIconCanvas);
2995 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002996 if (fi.getFolder() != null) {
2997 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2998 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002999 }
Adam Cohen268c4752012-06-06 17:47:33 -07003000 // Just in case this image view is still in the drag layer from a previous animation,
3001 // we remove it and re-add it.
3002 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3003 mDragLayer.removeView(mFolderIconImageView);
3004 }
3005 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003006 if (fi.getFolder() != null) {
3007 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003008 }
Adam Cohen268c4752012-06-06 17:47:33 -07003009 }
3010
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003012 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003013 FolderInfo info = (FolderInfo) fi.getTag();
3014 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3015 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003016 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3017 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003018 }
3019
Adam Cohen268c4752012-06-06 17:47:33 -07003020 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3021 copyFolderIconToImage(fi);
3022 fi.setVisibility(View.INVISIBLE);
3023
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003024 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3025 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003026 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003027 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3028 }
3029 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003030 oa.start();
3031 }
3032
Adam Cohen268c4752012-06-06 17:47:33 -07003033 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003034 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003035 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003036
Adam Cohen268c4752012-06-06 17:47:33 -07003037 // We remove and re-draw the FolderIcon in-case it has changed
3038 mDragLayer.removeView(mFolderIconImageView);
3039 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003040 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003041 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003042 oa.addListener(new AnimatorListenerAdapter() {
3043 @Override
3044 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003045 if (cl != null) {
3046 cl.clearFolderLeaveBehind();
3047 // Remove the ImageView copy of the FolderIcon and make the original visible.
3048 mDragLayer.removeView(mFolderIconImageView);
3049 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003050 }
3051 }
3052 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003053 oa.start();
3054 }
3055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003057 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 * is animated relative to the specified View. If the View is null, no animation
3059 * is played.
3060 *
3061 * @param folderInfo The FolderInfo describing the folder to open.
3062 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003063 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003064 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003065 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3066 if (openFolder != null && openFolder != folder) {
3067 // Close any open folder before opening a folder.
3068 closeFolder();
3069 }
3070
Adam Cohena9cf38f2011-05-02 15:36:58 -07003071 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072
Adam Cohena9cf38f2011-05-02 15:36:58 -07003073 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Sunny Goyalf4066152015-04-15 09:42:19 -07003075 // While the folder is open, the position of the icon cannot change.
3076 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3077
Adam Cohen4554ee12011-08-03 16:13:21 -07003078 // Just verify that the folder hasn't already been added to the DragLayer.
3079 // There was a one-off crash where the folder had a parent already.
3080 if (folder.getParent() == null) {
3081 mDragLayer.addView(folder);
3082 mDragController.addDropTarget((DropTarget) folder);
3083 } else {
3084 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3085 folder.getParent() + ").");
3086 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003087 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003088 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003089
3090 // Notify the accessibility manager that this folder "window" has appeared and occluded
3091 // the workspace items
3092 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3093 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003094 }
3095
3096 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003097 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003098 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003099 if (folder.isEditingName()) {
3100 folder.dismissEditingName();
3101 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 closeFolder(folder);
3103 }
3104 }
3105
Sunny Goyal83a8f042015-05-19 12:52:12 -07003106 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 folder.getInfo().opened = false;
3108
3109 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3110 if (parent != null) {
3111 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3112 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003113 if (fi != null) {
3114 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3115 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003116 }
3117 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003118
3119 // Notify the accessibility manager that this folder "window" has disappeard and no
3120 // longer occludeds the workspace items
3121 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
3123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003125 if (!isDraggingEnabled()) return false;
3126 if (isWorkspaceLocked()) return false;
3127 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128
Winson Chung76648c52015-07-10 14:33:23 -07003129 if (v == mAllAppsButton) {
3130 onLongClickAllAppsButton(v);
3131 return true;
3132 }
3133
Adam Cohen1697b792013-09-17 19:08:21 -07003134 if (v instanceof Workspace) {
3135 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003136 if (!mWorkspace.isTouchActive()) {
3137 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003138 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3139 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3140 return true;
3141 } else {
3142 return false;
3143 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003144 } else {
3145 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003146 }
Adam Cohen1697b792013-09-17 19:08:21 -07003147 }
3148
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003149 CellLayout.CellInfo longClickCellInfo = null;
3150 View itemUnderLongClick = null;
3151 if (v.getTag() instanceof ItemInfo) {
3152 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003153 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003154 itemUnderLongClick = longClickCellInfo.cell;
3155 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 }
3157
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 // The hotseat touch handling does not go through Workspace, and we always allow long press
3159 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003160 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003161 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003162 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003163 // User long pressed on empty space
3164 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3165 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003166 if (mWorkspace.isInOverviewMode()) {
3167 mWorkspace.startReordering(v);
3168 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003169 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003172 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3173 mHotseat.getOrderInHotseat(
3174 longClickCellInfo.cellX,
3175 longClickCellInfo.cellY));
3176 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003178 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 }
3180 }
3181 }
3182 return true;
3183 }
3184
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 boolean isHotseatLayout(View layout) {
3186 return mHotseat != null && layout != null &&
3187 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3188 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003189
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 /**
3191 * Returns the CellLayout of the specified container at the specified screen.
3192 */
Sunny Goyal92820592015-03-02 11:31:35 -08003193 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3195 if (mHotseat != null) {
3196 return mHotseat.getLayout();
3197 } else {
3198 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003199 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003201 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003202 }
3203 }
3204
Winson Chungb745afb2015-03-02 11:51:23 -08003205 /**
3206 * For overridden classes.
3207 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003208 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003209 return isAppsViewVisible();
3210 }
3211
3212 public boolean isAppsViewVisible() {
3213 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3214 }
3215
3216 public boolean isWidgetsViewVisible() {
3217 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003218 }
3219
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003220 private void setWorkspaceBackground(int background) {
3221 switch (background) {
3222 case WORKSPACE_BACKGROUND_TRANSPARENT:
3223 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3224 break;
3225 case WORKSPACE_BACKGROUND_BLACK:
3226 getWindow().setBackgroundDrawable(null);
3227 break;
3228 default:
3229 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3230 }
Craig Mautner360310b2012-10-26 15:13:08 -07003231 }
3232
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003233 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003234 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3235 int curflags = getWindow().getAttributes().flags
3236 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3237 if (wpflags != curflags) {
3238 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3239 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003240 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003241 }
3242
Michael Jurkae326f182011-11-21 14:05:46 -08003243 @Override
3244 public void onTrimMemory(int level) {
3245 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003246 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3247 // The widget preview db can result in holding onto over
3248 // 3MB of memory for caching which isn't necessary.
3249 SQLiteDatabase.releaseMemory();
3250
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003251 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003252 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003253 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003254 if (mLauncherCallbacks != null) {
3255 mLauncherCallbacks.onTrimMemory(level);
3256 }
Michael Jurkae326f182011-11-21 14:05:46 -08003257 }
3258
Winson5c6bdbb2015-09-03 11:36:19 -07003259 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003260 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003261 }
3262
Winson5c6bdbb2015-09-03 11:36:19 -07003263 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003264 boolean changed = mState != State.WORKSPACE ||
3265 mWorkspace.getState() != Workspace.State.NORMAL;
3266 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003267 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003268 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003269 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003270
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 // Set focus to the AppsCustomize button
3272 if (mAllAppsButton != null) {
3273 mAllAppsButton.requestFocus();
3274 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003275 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003276
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003277 // Change the state *after* we've called all the transition code
3278 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003279
Winson Chung5fb63472011-02-02 17:03:37 -08003280 // Resume the auto-advance of widgets
3281 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003282 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003283
Winson Chung0f785722015-04-08 10:27:49 -07003284 if (changed) {
3285 // Send an accessibility event to announce the context change
3286 getWindow().getDecorView()
3287 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003288 }
Winson5c6bdbb2015-09-03 11:36:19 -07003289 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003290 }
3291
Adam Cohened307df2013-10-02 09:37:31 -07003292 void showOverviewMode(boolean animated) {
3293 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003294 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003295 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003296 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003297 }
3298
Winson Chungb745afb2015-03-02 11:51:23 -08003299 /**
3300 * Shows the apps view.
3301 */
Winson Chung76648c52015-07-10 14:33:23 -07003302 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3303 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003304 if (resetListToTop) {
3305 mAppsView.scrollToTop();
3306 }
Winson Chung4ac30062015-05-08 17:34:17 -07003307 if (updatePredictedApps) {
3308 tryAndUpdatePredictedApps();
3309 }
Winson Chung76648c52015-07-10 14:33:23 -07003310 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003311 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003312
Winson Chungb745afb2015-03-02 11:51:23 -08003313 /**
3314 * Shows the widgets view.
3315 */
3316 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003317 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003318 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003319 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003320 }
Winson Chung76648c52015-07-10 14:33:23 -07003321 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003322
3323 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003324 @Override
3325 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003326 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003327 }
3328 });
Winson Chungb745afb2015-03-02 11:51:23 -08003329 }
3330
3331 /**
3332 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003333 *
3334 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003335 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003336 // TODO: calling method should use the return value so that when {@code false} is returned
3337 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003338 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003339 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3340 mState != State.WIDGETS_SPRING_LOADED) {
3341 return false;
3342 }
3343 if (toState != State.APPS && toState != State.WIDGETS) {
3344 return false;
3345 }
Winson Chungb745afb2015-03-02 11:51:23 -08003346
3347 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003348 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3349 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003350 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003351 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003352 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003355 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356
3357 // Pause the auto-advance of widgets until we are out of AllApps
3358 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003359 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360 closeFolder();
3361
3362 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003363 getWindow().getDecorView()
3364 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003365 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003366 }
3367
Winson Chungcd99cd32015-04-29 11:03:24 -07003368 /**
3369 * Updates the workspace and interaction state on state change, and return the animation to this
3370 * new state.
3371 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003372 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003373 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003374 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003375 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 updateInteraction(fromState, toState);
3377 return anim;
3378 }
3379
Jun Mukaif0033da2015-08-04 18:34:30 -07003380 public void onLauncherClingShown() {
3381 // When a launcher cling appears, it should cover the underlying layers, so their focus
3382 // should be blocked.
3383 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3384 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3385 }
3386 }
3387
3388 public void onLauncherClingDismissed() {
3389 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3390 }
3391
Hyunyoung Song3f471442015-04-08 19:01:34 -07003392 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003393 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003394 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003395 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003396 }
Winson Chungb745afb2015-03-02 11:51:23 -08003397
Winson Chung006ee262015-08-03 14:40:11 -07003398 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003399 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003400 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003401
3402 if (isAppsViewVisible()) {
3403 mState = State.APPS_SPRING_LOADED;
3404 } else if (isWidgetsViewVisible()) {
3405 mState = State.WIDGETS_SPRING_LOADED;
3406 } else {
3407 mState = State.WORKSPACE_SPRING_LOADED;
3408 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003409 }
Adam Cohen7777d962011-08-18 18:58:38 -07003410
Hyunyoung Song3f471442015-04-08 19:01:34 -07003411 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003412 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003413 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003414
Winson Chunge7a03942011-08-05 15:05:12 -07003415 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003416 @Override
3417 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003418 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3420 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003421 // Before we show workspace, hide all apps again because
3422 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3423 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003424 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003425 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003426 } else {
3427 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003428 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003429 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003430 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003431 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003432
Tony Wickham94e0d372015-09-11 12:17:48 -07003433 private boolean isStateSpringLoaded() {
3434 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3435 || mState == State.WIDGETS_SPRING_LOADED;
3436 }
3437
Michael Jurkad3ef3062010-11-23 16:23:58 -08003438 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003439 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003440 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003441 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003442 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003443 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003444 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3445 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003446 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003447 }
3448
Winson Chung4ac30062015-05-08 17:34:17 -07003449 /**
3450 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3451 * resumed.
3452 */
3453 private void tryAndUpdatePredictedApps() {
3454 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003455 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003456 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003457 mAppsView.setPredictedApps(apps);
3458 }
3459 }
3460 }
3461
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462 void lockAllApps() {
3463 // TODO
3464 }
3465
3466 void unlockAllApps() {
3467 // TODO
3468 }
3469
Sunny Goyal594d76d2014-11-06 10:12:54 -08003470 protected void disableVoiceButtonProxy(boolean disable) {
3471 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003472 }
3473
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003474 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003475 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3476 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003477 }
3478
Adam Cohen24ce0b32014-01-14 16:18:14 -08003479 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003480 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3481 if (searchProvider == null) {
3482 return null;
3483 }
3484
3485 Bundle opts = new Bundle();
3486 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003487 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003488
3489 SharedPreferences sp = getSharedPreferences(
3490 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3491 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003492 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003493 if (!searchProvider.provider.flattenToString().equals(
3494 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003495 || (widgetInfo == null)
3496 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003497 // A valid widget is not already bound.
3498 if (widgetId > -1) {
3499 mAppWidgetHost.deleteAppWidgetId(widgetId);
3500 widgetId = -1;
3501 }
3502
3503 // Try to bind a new widget
3504 widgetId = mAppWidgetHost.allocateAppWidgetId();
3505
3506 if (!AppWidgetManagerCompat.getInstance(this)
3507 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3508 mAppWidgetHost.deleteAppWidgetId(widgetId);
3509 widgetId = -1;
3510 }
3511
3512 sp.edit()
3513 .putInt(QSB_WIDGET_ID, widgetId)
3514 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3515 .commit();
3516 }
3517
Sunny Goyal8d595092015-07-06 12:22:14 -07003518 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003519 if (widgetId != -1) {
3520 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003521 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003522 mQsb.updateAppWidgetOptions(opts);
3523 mQsb.setPadding(0, 0, 0, 0);
3524 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003525 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003526 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003527 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003528 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003529 }
3530
Sunny Goyal22235bc2015-04-03 09:23:43 -07003531 private void reinflateQSBIfNecessary() {
3532 if (mQsb instanceof LauncherAppWidgetHostView &&
3533 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3534 mSearchDropTargetBar.removeView(mQsb);
3535 mQsb = null;
3536 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3537 }
3538 }
3539
Michael Jurkad7c28052012-04-27 15:43:36 -07003540 @Override
3541 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003542 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003543 final List<CharSequence> text = event.getText();
3544 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003545 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003546 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003547 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003548 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003549 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003550 } else if (mWorkspace != null) {
3551 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003552 } else {
3553 text.add(getString(R.string.all_apps_home_button_label));
3554 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003555 return result;
3556 }
3557
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003558 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003559 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003560 */
Adam Cohen091440a2015-03-18 14:16:05 -07003561 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003562 @Override
3563 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003564 closeSystemDialogs();
3565 }
3566 }
3567
3568 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003569 * If the activity is currently paused, signal that we need to run the passed Runnable
3570 * in onResume.
3571 *
3572 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003573 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3574 * wrong when we're not running, and if the activity comes back to what the configuration was
3575 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003579 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003580 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003581 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003582 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003583 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003584 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003585 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003586 }
3587 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003588 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003589 return true;
3590 } else {
3591 return false;
3592 }
3593 }
3594
Michael Jurkac402cd92013-05-20 15:49:32 +02003595 private boolean waitUntilResume(Runnable run) {
3596 return waitUntilResume(run, false);
3597 }
3598
Michael Jurka1e2f4652013-07-08 18:03:46 -07003599 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003600 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003601 }
3602
Michael Jurka7607c2f2013-04-03 14:33:19 -07003603 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003604 * If the activity is currently paused, signal that we need to re-run the loader
3605 * in onResume.
3606 *
3607 * This needs to be called from incoming places where resources might have been loaded
3608 * while we are paused. That is becaues the Configuration might be wrong
3609 * when we're not running, and if it comes back to what it was when we
3610 * were paused, we are not restarted.
3611 *
3612 * Implementation of the method from LauncherModel.Callbacks.
3613 *
3614 * @return true if we are currently paused. The caller might be able to
3615 * skip some work in that case since we will come back again.
3616 */
3617 public boolean setLoadOnResume() {
3618 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003619 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003620 mOnResumeNeedsLoad = true;
3621 return true;
3622 } else {
3623 return false;
3624 }
3625 }
3626
3627 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003631 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003632 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003633 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003634 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003635 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003637
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 /**
3639 * Refreshes the shortcuts shown on the workspace.
3640 *
3641 * Implementation of the method from LauncherModel.Callbacks.
3642 */
3643 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003644 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003645
Michael Jurka7607c2f2013-04-03 14:33:19 -07003646 // If we're starting binding all over again, clear any bind calls we'd postponed in
3647 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3648 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003649 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003650
Winson Chungd64d1762013-08-20 14:37:16 -07003651 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003652 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003653 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003654
Michael Jurka05bf644e2011-11-30 20:28:53 -08003655 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003656 if (mHotseat != null) {
3657 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003658 }
3659 }
3660
Adam Cohendcd297f2013-06-18 13:13:40 -07003661 @Override
3662 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003663 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003664
Adam Cohen5084cba2013-09-03 12:01:16 -07003665 // If there are no screens, we need to have an empty screen
3666 if (orderedScreenIds.size() == 0) {
3667 mWorkspace.addExtraEmptyScreen();
3668 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003669
3670 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003671 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003672 if (hasCustomContentToLeft()) {
3673 mWorkspace.createCustomContentContainer();
3674 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003675 }
Winson Chung64359a52013-07-08 17:17:08 -07003676 }
3677
3678 @Override
3679 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003680 int count = orderedScreenIds.size();
3681 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003682 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003683 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003684 }
3685
Adam Cohen39a06042013-07-19 14:30:12 -07003686 private boolean shouldShowWeightWatcher() {
3687 String spKey = LauncherAppState.getSharedPreferencesKey();
3688 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003689 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003690
3691 return show;
3692 }
3693
3694 private void toggleShowWeightWatcher() {
3695 String spKey = LauncherAppState.getSharedPreferencesKey();
3696 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3697 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3698
3699 show = !show;
3700
3701 SharedPreferences.Editor editor = sp.edit();
3702 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3703 editor.commit();
3704
3705 if (mWeightWatcher != null) {
3706 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3707 }
3708 }
3709
Winson Chungd64d1762013-08-20 14:37:16 -07003710 public void bindAppsAdded(final ArrayList<Long> newScreens,
3711 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003712 final ArrayList<ItemInfo> addAnimated,
3713 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003714 Runnable r = new Runnable() {
3715 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003716 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003717 }
3718 };
3719 if (waitUntilResume(r)) {
3720 return;
3721 }
3722
Winson Chungd64d1762013-08-20 14:37:16 -07003723 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003724 if (newScreens != null) {
3725 bindAddScreens(newScreens);
3726 }
Winson Chungd64d1762013-08-20 14:37:16 -07003727
3728 // We add the items without animation on non-visible pages, and with
3729 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003730 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003731 bindItems(addNotAnimated, 0,
3732 addNotAnimated.size(), false);
3733 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003734 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003735 bindItems(addAnimated, 0,
3736 addAnimated.size(), true);
3737 }
Winson Chungc58497e2013-09-03 17:48:37 -07003738
Winson Chung87412982013-10-03 18:34:14 -07003739 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003740 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003741
Winson Chungb745afb2015-03-02 11:51:23 -08003742 if (addedApps != null && mAppsView != null) {
3743 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003744 }
Winson Chungd64d1762013-08-20 14:37:16 -07003745 }
3746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 * Bind the items start-end from the list.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003751 */
Winson Chung64359a52013-07-08 17:17:08 -07003752 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3753 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003754 Runnable r = new Runnable() {
3755 public void run() {
3756 bindItems(shortcuts, start, end, forceAnimateIcons);
3757 }
3758 };
3759 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003760 return;
3761 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003762
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003764 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3765 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003766 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003767 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003768 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003771
3772 // Short circuit if we are loading dock items for a configuration which has no dock
3773 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3774 mHotseat == null) {
3775 continue;
3776 }
3777
Sunny Goyal639e9062015-08-19 19:17:06 -07003778 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 switch (item.itemType) {
3780 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3781 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003782 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003783 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003784
Winson Chung64359a52013-07-08 17:17:08 -07003785 /*
3786 * TODO: FIX collision case
3787 */
Winson Chungce376632013-07-11 16:12:41 -07003788 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3789 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3790 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003791 View v = cl.getChildAt(item.cellX, item.cellY);
3792 Object tag = v.getTag();
3793 String desc = "Collision while binding workspace item: " + item
3794 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003795 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003796 throw (new RuntimeException(desc));
3797 } else {
3798 Log.d(TAG, desc);
3799 }
Winson Chungce376632013-07-11 16:12:41 -07003800 }
Winson Chung64359a52013-07-08 17:17:08 -07003801 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003803 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003804 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003805 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003806 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003808 default:
3809 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003811
3812 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3813 item.cellY, 1, 1);
3814 if (animateIcons) {
3815 // Animate all the applications up now
3816 view.setAlpha(0f);
3817 view.setScaleX(0f);
3818 view.setScaleY(0f);
3819 bounceAnims.add(createNewAppBounceAnimation(view, i));
3820 newShortcutsScreenId = item.screenId;
3821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003822 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003823
Winson Chung997a9232013-07-24 15:33:46 -07003824 if (animateIcons) {
3825 // Animate to the correct page
3826 if (newShortcutsScreenId > -1) {
3827 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003828 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003829 final Runnable startBounceAnimRunnable = new Runnable() {
3830 public void run() {
3831 anim.playTogether(bounceAnims);
3832 anim.start();
3833 }
3834 };
Winson Chung997a9232013-07-24 15:33:46 -07003835 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003836 // We post the animation slightly delayed to prevent slowdowns
3837 // when we are loading right after we return to launcher.
3838 mWorkspace.postDelayed(new Runnable() {
3839 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003840 if (mWorkspace != null) {
3841 mWorkspace.snapToPage(newScreenIndex);
3842 mWorkspace.postDelayed(startBounceAnimRunnable,
3843 NEW_APPS_ANIMATION_DELAY);
3844 }
Winson Chung94d67682013-09-25 16:29:40 -07003845 }
3846 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003847 } else {
3848 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003849 }
3850 }
Winson Chung64359a52013-07-08 17:17:08 -07003851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003853 }
3854
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 /**
3856 * Implementation of the method from LauncherModel.Callbacks.
3857 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003858 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003859 Runnable r = new Runnable() {
3860 public void run() {
3861 bindFolders(folders);
3862 }
3863 };
3864 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003865 return;
3866 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003867 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869
3870 /**
3871 * Add the views for a widget to the workspace.
3872 *
3873 * Implementation of the method from LauncherModel.Callbacks.
3874 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003875 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003876 Runnable r = new Runnable() {
3877 public void run() {
3878 bindAppWidget(item);
3879 }
3880 };
3881 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 return;
3883 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003884
Daniel Sandler843e8602010-06-07 14:59:01 -04003885 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3886 if (DEBUG_WIDGETS) {
3887 Log.d(TAG, "bindAppWidget: " + item);
3888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 final Workspace workspace = mWorkspace;
3890
Adam Cohen59400422014-03-05 18:07:04 -08003891 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003892 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003893
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003894 if (!mIsSafeModeEnabled
3895 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003896 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3897
Sunny Goyalff572272014-07-23 13:58:07 -07003898 if (appWidgetInfo == null) {
3899 if (DEBUG_WIDGETS) {
3900 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3901 + " belongs to component " + item.providerName
3902 + ", as the povider is null");
3903 }
3904 LauncherModel.deleteItemFromDatabase(this, item);
3905 return;
3906 }
Sunny Goyalff572272014-07-23 13:58:07 -07003907
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003908 // If we do not have a valid id, try to bind an id.
3909 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3910 // Note: This assumes that the id remap broadcast is received before this step.
3911 // If that is not the case, the id remap will be ignored and user may see the
3912 // click to setup view.
3913 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3914 pendingInfo.spanX = item.spanX;
3915 pendingInfo.spanY = item.spanY;
3916 pendingInfo.minSpanX = item.minSpanX;
3917 pendingInfo.minSpanY = item.minSpanY;
3918 Bundle options = null;
3919 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003920
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003921 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3922 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3923 newWidgetId, appWidgetInfo, options);
3924
3925 // TODO consider showing a permission dialog when the widget is clicked.
3926 if (!success) {
3927 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3928 if (DEBUG_WIDGETS) {
3929 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3930 + " belongs to component " + item.providerName
3931 + ", as the launcher is unable to bing a new widget id");
3932 }
3933 LauncherModel.deleteItemFromDatabase(this, item);
3934 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003935 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003936
3937 item.appWidgetId = newWidgetId;
3938
3939 // If the widget has a configure activity, it is still needs to set it up, otherwise
3940 // the widget is ready to go.
3941 item.restoreStatus = (appWidgetInfo.configure == null)
3942 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3943 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3944
3945 LauncherModel.updateItemInDatabase(this, item);
3946 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3947 && (appWidgetInfo.configure == null)) {
3948 // If the ID is already valid, verify if we need to configure or not.
3949 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3950 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003951 }
Sunny Goyalff572272014-07-23 13:58:07 -07003952 }
3953
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003954 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003955 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003956 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003957 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3958 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003959 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003960
Sunny Goyal651077b2014-06-30 14:15:31 -07003961 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003962 item.minSpanX = appWidgetInfo.minSpanX;
3963 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003964 } else {
3965 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003966 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3967 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003968 view.updateIcon(mIconCache);
3969 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003970 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003971 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003975 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976
Adam Cohendcd297f2013-06-18 13:13:40 -07003977 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003979 if (!item.isCustomWidget()) {
3980 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3981 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003982
Joe Onorato9c1289c2009-08-17 11:03:03 -04003983 workspace.requestLayout();
3984
Daniel Sandler843e8602010-06-07 14:59:01 -04003985 if (DEBUG_WIDGETS) {
3986 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3987 + (SystemClock.uptimeMillis()-start) + "ms");
3988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 }
3990
Sunny Goyalff572272014-07-23 13:58:07 -07003991 /**
3992 * Restores a pending widget.
3993 *
3994 * @param appWidgetId The app widget id
3995 * @param cellInfo The position on screen where to create the widget.
3996 */
3997 private void completeRestoreAppWidget(final int appWidgetId) {
3998 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3999 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4000 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4001 return;
4002 }
4003
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004004 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004005 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4006
4007 mWorkspace.reinflateWidgetsIfNecessary();
4008 LauncherModel.updateItemInDatabase(this, info);
4009 }
4010
Adam Cohen1462de32012-07-24 22:34:36 -07004011 public void onPageBoundSynchronously(int page) {
4012 mSynchronouslyBoundPages.add(page);
4013 }
4014
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 /**
4016 * Callback saying that there aren't any more items to bind.
4017 *
4018 * Implementation of the method from LauncherModel.Callbacks.
4019 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004020 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004021 Runnable r = new Runnable() {
4022 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004023 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004024 }
4025 };
4026 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004027 return;
4028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029 if (mSavedState != null) {
4030 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004031 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004033 mSavedState = null;
4034 }
4035
Adam Cohen1462de32012-07-24 22:34:36 -07004036 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004038 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004039 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004040
4041 // If we received the result of any pending adds while the loader was running (e.g. the
4042 // widget configuration forced an orientation change), process them now.
4043 if (sPendingAddItem != null) {
4044 final long screenId = completeAdd(sPendingAddItem);
4045
4046 // TODO: this moves the user to the page where the pending item was added. Ideally,
4047 // the screen would be guaranteed to exist after bind, and the page would be set through
4048 // the workspace restore process.
4049 mWorkspace.post(new Runnable() {
4050 @Override
4051 public void run() {
4052 mWorkspace.snapToScreenId(screenId);
4053 }
4054 });
4055 sPendingAddItem = null;
4056 }
4057
Sunny Goyal756adbc2015-04-16 15:20:51 -07004058 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004059
4060 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004061 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004062 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004063 }
4064
Adam Cohen3ed316a2014-07-23 18:21:20 -07004065 private void sendLoadingCompleteBroadcastIfNecessary() {
4066 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4067 String permission =
4068 getResources().getString(R.string.receive_first_load_broadcast_permission);
4069 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4070 sendBroadcast(intent, permission);
4071 SharedPreferences.Editor editor = mSharedPrefs.edit();
4072 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4073 editor.apply();
4074 }
4075 }
4076
Winson Chunga0b7e862013-09-05 16:03:15 -07004077 public boolean isAllAppsButtonRank(int rank) {
4078 if (mHotseat != null) {
4079 return mHotseat.isAllAppsButtonRank(rank);
4080 }
4081 return false;
4082 }
4083
Winson Chunga2413752012-04-03 14:22:34 -07004084 private boolean canRunNewAppsAnimation() {
4085 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004086 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4087 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004088 }
4089
Winson Chungc9168342013-06-26 14:54:55 -07004090 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004091 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004092 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4093 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004094 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004095 return bounceAnim;
4096 }
4097
Adam Cohen27772732013-11-11 14:00:56 +00004098 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004099 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004100 }
4101
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004102 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004103 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004104 }
4105
Winson Chung88fa7412015-08-03 14:25:28 -07004106 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004107 if (mSearchDropTargetBar == null) {
4108 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004109 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004110 if (mQsb != null) {
4111 mSearchDropTargetBar.removeView(mQsb);
4112 mQsb = null;
4113 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004114 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004115 }
4116
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004117 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004118 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4119 * multiple calls to bind the same list.)
4120 */
4121 @Thunk ArrayList<AppInfo> mTmpAppsList;
4122 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4123 public void run() {
4124 bindAllApplications(mTmpAppsList);
4125 mTmpAppsList = null;
4126 }
4127 };
4128
4129 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 * Add the icons for all apps.
4131 *
4132 * Implementation of the method from LauncherModel.Callbacks.
4133 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004134 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004135 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4136 mTmpAppsList = apps;
4137 return;
4138 }
4139
Winson Chungb745afb2015-03-02 11:51:23 -08004140 if (mAppsView != null) {
4141 mAppsView.setApps(apps);
4142 }
Adam Cohen9211d422014-10-07 18:14:53 -07004143 if (mLauncherCallbacks != null) {
4144 mLauncherCallbacks.bindAllApplications(apps);
4145 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004146 }
4147
4148 /**
4149 * A package was updated.
4150 *
4151 * Implementation of the method from LauncherModel.Callbacks.
4152 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004153 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004154 Runnable r = new Runnable() {
4155 public void run() {
4156 bindAppsUpdated(apps);
4157 }
4158 };
4159 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004160 return;
4161 }
4162
Winson Chungb745afb2015-03-02 11:51:23 -08004163 if (mAppsView != null) {
4164 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004166 }
4167
Sunny Goyal4390ace2014-10-13 11:33:11 -07004168 @Override
4169 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4170 Runnable r = new Runnable() {
4171 public void run() {
4172 bindWidgetsRestored(widgets);
4173 }
4174 };
4175 if (waitUntilResume(r)) {
4176 return;
4177 }
4178 mWorkspace.widgetsRestored(widgets);
4179 }
4180
Joe Onorato9c1289c2009-08-17 11:03:03 -04004181 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004182 * Some shortcuts were updated in the background.
4183 *
4184 * Implementation of the method from LauncherModel.Callbacks.
4185 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004186 @Override
4187 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4188 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004189 Runnable r = new Runnable() {
4190 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004191 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004192 }
4193 };
4194 if (waitUntilResume(r)) {
4195 return;
4196 }
4197
Sunny Goyal4390ace2014-10-13 11:33:11 -07004198 if (!updated.isEmpty()) {
4199 mWorkspace.updateShortcuts(updated);
4200 }
4201
4202 if (!removed.isEmpty()) {
4203 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4204 for (ShortcutInfo si : removed) {
4205 removedComponents.add(si.getTargetComponent());
4206 }
4207 mWorkspace.removeItemsByComponentName(removedComponents, user);
4208 // Notify the drag controller
4209 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 }
4211 }
4212
4213 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004214 * Update the state of a package, typically related to install state.
4215 *
4216 * Implementation of the method from LauncherModel.Callbacks.
4217 */
Sunny Goyale755d462014-07-22 13:48:29 -07004218 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004219 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4220 Runnable r = new Runnable() {
4221 public void run() {
4222 bindRestoreItemsChange(updates);
4223 }
4224 };
4225 if (waitUntilResume(r)) {
4226 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004227 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004228
Sunny Goyal756adbc2015-04-16 15:20:51 -07004229 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004230 }
4231
4232 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004233 * A package was uninstalled. We take both the super set of packageNames
4234 * in addition to specific applications to remove, the reason being that
4235 * this can be called when a package is updated as well. In that scenario,
4236 * we only remove specific components from the workspace, where as
4237 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004239 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004242 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004243 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004244 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004245 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004246 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004247 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004248 }
Winson Chungd64d1762013-08-20 14:37:16 -07004249 };
4250 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004251 return;
4252 }
4253
Sunny Goyal1a745e82014-10-02 15:58:31 -07004254 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004255 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4256 for (AppInfo info : appInfos) {
4257 removedComponents.add(info.componentName);
4258 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 if (!packageNames.isEmpty()) {
4260 mWorkspace.removeItemsByPackageName(packageNames, user);
4261 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004262 if (!removedComponents.isEmpty()) {
4263 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004264 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004265 // Notify the drag controller
4266 mDragController.onAppsRemoved(packageNames, removedComponents);
4267
Sunny Goyal1a745e82014-10-02 15:58:31 -07004268 } else {
4269 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004270 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004271
Winson Chungdf95eb12013-10-16 14:57:07 -07004272 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004273 if (mAppsView != null) {
4274 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004276 }
Joe Onoratobe386092009-11-17 17:32:16 -08004277
Michael Jurkac402cd92013-05-20 15:49:32 +02004278 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004279 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004280 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004281 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004282 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004283
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004284 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004285 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004286 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004287 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004288 return;
4289 }
4290
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004291 if (mWidgetsView != null && model != null) {
4292 mWidgetsView.addWidgets(model);
4293 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004294 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004295 }
4296
Winson Chung400438b2011-01-16 17:53:48 -08004297 private int mapConfigurationOriActivityInfoOri(int configOri) {
4298 final Display d = getWindowManager().getDefaultDisplay();
4299 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4300 switch (d.getRotation()) {
4301 case Surface.ROTATION_0:
4302 case Surface.ROTATION_180:
4303 // We are currently in the same basic orientation as the natural orientation
4304 naturalOri = configOri;
4305 break;
4306 case Surface.ROTATION_90:
4307 case Surface.ROTATION_270:
4308 // We are currently in the other basic orientation to the natural orientation
4309 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4310 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4311 break;
4312 }
4313
4314 int[] oriMap = {
4315 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4316 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4317 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4318 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4319 };
4320 // Since the map starts at portrait, we need to offset if this device's natural orientation
4321 // is landscape.
4322 int indexOffset = 0;
4323 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4324 indexOffset = 1;
4325 }
4326 return oriMap[(d.getRotation() + indexOffset) % 4];
4327 }
Adam Cohen446e9402011-09-15 18:21:21 -07004328
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004329 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004330 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004331 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004332 if (Utilities.ATLEAST_JB_MR2) {
4333 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4334 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004335 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4336 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004337 }
Winson Chung4b919f82012-05-01 10:44:08 -07004338 }
4339 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004340
Winson Chung4b919f82012-05-01 10:44:08 -07004341 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004342 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004343 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004344 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004345 } else {
4346 mHandler.postDelayed(new Runnable() {
4347 public void run() {
4348 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4349 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004350 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004351 }
Winson Chung4b919f82012-05-01 10:44:08 -07004352 }
Winson Chung400438b2011-01-16 17:53:48 -08004353 }
4354
Winson Chunge43a1e72014-01-15 10:33:02 -08004355 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004356 if (mLauncherCallbacks != null) {
4357 return mLauncherCallbacks.isLauncherPreinstalled();
4358 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004359 PackageManager pm = getPackageManager();
4360 try {
4361 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4362 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4363 return true;
4364 } else {
4365 return false;
4366 }
4367 } catch (NameNotFoundException e) {
4368 e.printStackTrace();
4369 return false;
4370 }
4371 }
4372
Adam Cohenea90f832014-05-21 15:03:34 -07004373 /**
4374 * This method indicates whether or not we should suggest default wallpaper dimensions
4375 * when our wallpaper cropper was not yet used to set a wallpaper.
4376 */
4377 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004378 if (mLauncherCallbacks != null) {
4379 return mLauncherCallbacks.overrideWallpaperDimensions();
4380 }
Adam Cohenea90f832014-05-21 15:03:34 -07004381 return true;
4382 }
4383
Adam Cohen432609a2014-03-13 17:03:22 -07004384 /**
4385 * To be overridden by subclasses to indicate that there is an activity to launch
4386 * before showing the standard launcher experience.
4387 */
4388 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004389 if (mLauncherCallbacks != null) {
4390 return mLauncherCallbacks.hasFirstRunActivity();
4391 }
Adam Cohen432609a2014-03-13 17:03:22 -07004392 return false;
4393 }
4394
4395 /**
4396 * To be overridden by subclasses to launch any first run activity
4397 */
4398 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004399 if (mLauncherCallbacks != null) {
4400 return mLauncherCallbacks.getFirstRunActivity();
4401 }
Adam Cohen432609a2014-03-13 17:03:22 -07004402 return null;
4403 }
4404
Winson Chunga6945242014-01-08 14:04:34 -08004405 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004406 return !ActivityManager.isRunningInTestHarness() &&
4407 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004408 }
4409
Adam Cohen4a9423d2014-03-28 14:22:31 -07004410 protected boolean hasRunFirstRunActivity() {
4411 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4412 }
4413
Adam Cohen432609a2014-03-13 17:03:22 -07004414 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004415 if (shouldRunFirstRunActivity() &&
4416 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004417 Intent firstRunIntent = getFirstRunActivity();
4418 if (firstRunIntent != null) {
4419 startActivity(firstRunIntent);
4420 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004422 }
4423 }
Adam Cohen432609a2014-03-13 17:03:22 -07004424 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004425 }
4426
4427 private void markFirstRunActivityShown() {
4428 SharedPreferences.Editor editor = mSharedPrefs.edit();
4429 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4430 editor.apply();
4431 }
4432
Adam Cohen432609a2014-03-13 17:03:22 -07004433 /**
4434 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4435 * screen that must be displayed and dismissed.
4436 */
4437 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004438 if (mLauncherCallbacks != null) {
4439 return mLauncherCallbacks.hasDismissableIntroScreen();
4440 }
Adam Cohen432609a2014-03-13 17:03:22 -07004441 return false;
4442 }
4443
4444 /**
4445 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4446 */
4447 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004448 if (mLauncherCallbacks != null) {
4449 return mLauncherCallbacks.getIntroScreen();
4450 }
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return null;
4452 }
4453
4454 /**
4455 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4456 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4457 */
4458 private boolean shouldShowIntroScreen() {
4459 return hasDismissableIntroScreen() &&
4460 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4461 }
4462
4463 protected void showIntroScreen() {
4464 View introScreen = getIntroScreen();
4465 changeWallpaperVisiblity(false);
4466 if (introScreen != null) {
4467 mDragLayer.showOverlayView(introScreen);
4468 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004469 if (mLauncherOverlayContainer != null) {
4470 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4471 }
Adam Cohen432609a2014-03-13 17:03:22 -07004472 }
4473
4474 public void dismissIntroScreen() {
4475 markIntroScreenDismissed();
4476 if (showFirstRunActivity()) {
4477 // We delay hiding the intro view until the first run activity is showing. This
4478 // avoids a blip.
4479 mWorkspace.postDelayed(new Runnable() {
4480 @Override
4481 public void run() {
4482 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004483 if (mLauncherOverlayContainer != null) {
4484 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4485 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004486 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004487 }
4488 }, ACTIVITY_START_DELAY);
4489 } else {
4490 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004491 if (mLauncherOverlayContainer != null) {
4492 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4493 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004494 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004495 }
4496 changeWallpaperVisiblity(true);
4497 }
4498
4499 private void markIntroScreenDismissed() {
4500 SharedPreferences.Editor editor = mSharedPrefs.edit();
4501 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4502 editor.apply();
4503 }
4504
Adam Cohen091440a2015-03-18 14:16:05 -07004505 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004506 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4507 // on the device, then we always show the first run cling experience (or if there is no
4508 // launcher2). Otherwise, we prompt the user upon started for migration
4509 LauncherClings launcherClings = new LauncherClings(this);
4510 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004511 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004512 if (mModel.canMigrateFromOldLauncherDb(this)) {
4513 launcherClings.showMigrationCling();
4514 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004515 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004516 }
4517 }
4518 }
4519
Winson Chunga6945242014-01-08 14:04:34 -08004520 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004521 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4522 if (mHotseat != null) mHotseat.setAlpha(1f);
4523 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004524 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4525 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004526 }
4527
4528 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004529 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4530 if (mHotseat != null) mHotseat.setAlpha(0f);
4531 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004532 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4533 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004534 }
4535
Winson Chung5ffd51d2015-06-25 11:36:50 -07004536 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004537 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004538 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004539 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004540 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004541 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004542 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4543 if (userHandle != null) {
4544 user = UserHandleCompat.fromUser(userHandle);
4545 }
4546 }
4547 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004548 }
4549
4550 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004551 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004552 if (user == null) {
4553 user = UserHandleCompat.myUserHandle();
4554 }
4555
4556 // Called from search suggestion, add the profile extra to the intent to ensure that we
4557 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004558 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004559 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004560 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004561 return null;
4562 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004563 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004564 }
4565
Winson Chung5ffd51d2015-06-25 11:36:50 -07004566 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4568 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004569 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004570 UserHandleCompat.myUserHandle());
4571 }
4572
Winson Chung5ffd51d2015-06-25 11:36:50 -07004573 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004574 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4575 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004576 mWorkspace.onExternalDragStartedWithItem(dragView);
4577 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004578 }
4579
Winson Chung5ffd51d2015-06-25 11:36:50 -07004580 protected void moveWorkspaceToDefaultScreen() {
4581 mWorkspace.moveToDefaultScreen(false);
4582 }
4583
Anjali Koppalf5d29132014-02-28 13:33:27 -08004584 @Override
4585 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004586 if (mLauncherCallbacks != null) {
4587 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4588 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004589 }
4590
Winson Chung80baf5a2010-08-09 16:03:15 -07004591 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004592 * Returns a FastBitmapDrawable with the icon, accurately sized.
4593 */
4594 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4595 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4596 d.setFilterBitmap(true);
4597 resizeIconDrawable(d);
4598 return d;
4599 }
4600
4601 /**
4602 * Resizes an icon drawable to the correct icon size.
4603 */
4604 public void resizeIconDrawable(Drawable icon) {
4605 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4606 }
4607
4608 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004609 * Prints out out state for debugging.
4610 */
4611 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004612 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004613 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004614 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4615 Log.d(TAG, "mRestoring=" + mRestoring);
4616 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004617 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004618 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004619 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004620
Daniel Sandler325dc232013-06-05 22:57:57 -04004621 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004622 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004623
4624 @Override
4625 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4626 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004627 synchronized (sDumpLogs) {
4628 writer.println(" ");
4629 writer.println("Debug logs: ");
4630 for (int i = 0; i < sDumpLogs.size(); i++) {
4631 writer.println(" " + sDumpLogs.get(i));
4632 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004633 }
Adam Cohen9211d422014-10-07 18:14:53 -07004634 if (mLauncherCallbacks != null) {
4635 mLauncherCallbacks.dump(prefix, fd, writer, args);
4636 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004637 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004638
4639 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004640 if (DEBUG_DUMP_LOG) {
4641 synchronized (sDumpLogs) {
4642 Log.d(TAG, "");
4643 Log.d(TAG, "*********************");
4644 Log.d(TAG, "Launcher debug logs: ");
4645 for (int i = 0; i < sDumpLogs.size(); i++) {
4646 Log.d(TAG, " " + sDumpLogs.get(i));
4647 }
4648 Log.d(TAG, "*********************");
4649 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004650 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004651 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004652 }
4653
4654 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004655 addDumpLog(tag, log, null, debugLog);
4656 }
4657
4658 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004659 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004660 if (e != null) {
4661 Log.d(tag, log, e);
4662 } else {
4663 Log.d(tag, log);
4664 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004665 }
Winson Chungede41292013-09-19 16:27:36 -07004666 if (DEBUG_DUMP_LOG) {
4667 sDateStamp.setTime(System.currentTimeMillis());
4668 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004669 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4670 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004671 }
Winson Chungede41292013-09-19 16:27:36 -07004672 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004673 }
4674
Adam Cohen59400422014-03-05 18:07:04 -08004675 public static CustomAppWidget getCustomAppWidget(String name) {
4676 return sCustomAppWidgets.get(name);
4677 }
4678
4679 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4680 return sCustomAppWidgets;
4681 }
4682
Winson Chungede41292013-09-19 16:27:36 -07004683 public void dumpLogsToLocalData() {
4684 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004685 new AsyncTask<Void, Void, Void>() {
4686 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004687 boolean success = false;
4688 sDateStamp.setTime(sRunStart);
4689 String FILENAME = sDateStamp.getMonth() + "-"
4690 + sDateStamp.getDay() + "_"
4691 + sDateStamp.getHours() + "-"
4692 + sDateStamp.getMinutes() + "_"
4693 + sDateStamp.getSeconds() + ".txt";
4694
4695 FileOutputStream fos = null;
4696 File outFile = null;
4697 try {
4698 outFile = new File(getFilesDir(), FILENAME);
4699 outFile.createNewFile();
4700 fos = new FileOutputStream(outFile);
4701 } catch (Exception e) {
4702 e.printStackTrace();
4703 }
4704 if (fos != null) {
4705 PrintWriter writer = new PrintWriter(fos);
4706
4707 writer.println(" ");
4708 writer.println("Debug logs: ");
4709 synchronized (sDumpLogs) {
4710 for (int i = 0; i < sDumpLogs.size(); i++) {
4711 writer.println(" " + sDumpLogs.get(i));
4712 }
4713 }
4714 writer.close();
4715 }
4716 try {
4717 if (fos != null) {
4718 fos.close();
4719 success = true;
4720 }
4721 } catch (IOException e) {
4722 e.printStackTrace();
4723 }
Michael Jurka43467462013-11-14 12:03:58 +01004724 return null;
Winson Chungede41292013-09-19 16:27:36 -07004725 }
Michael Jurka43467462013-11-14 12:03:58 +01004726 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004727 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004729}
Michael Jurka2763be32011-02-24 11:19:57 -08004730
Dan Sandlerd5024042014-01-09 15:01:33 -05004731interface DebugIntents {
4732 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4733 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004734}