blob: 17b72b7afe75d6e55b2e2ccde60490f95784cd0b [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;
Sunny Goyal527c7d32015-08-28 15:19:36 -0700113import com.android.launcher3.util.ViewOnDrawExecutor;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700114import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700115import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700116import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700117
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700122import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700137 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700156 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
157 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
158 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
159
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700160 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
161
Mathew Inwood876a8462013-06-14 14:12:41 +0100162 /**
163 * IntentStarter uses request codes starting with this. This must be greater than all activity
164 * request codes used internally.
165 */
166 protected static final int REQUEST_LAST = 100;
167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700180 // Type: Content Values / parcelable
181 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000184 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Adam Cohen432609a2014-03-13 17:03:22 -0700187 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800188 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
189
Adam Cohen3ed316a2014-07-23 18:21:20 -0700190 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
191 static final String ACTION_FIRST_LOAD_COMPLETE =
192 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700195 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700196
Sunny Goyal594d76d2014-11-06 10:12:54 -0800197 private static final String QSB_WIDGET_ID = "qsb_widget_id";
198 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
199
Winson Chunga6945242014-01-08 14:04:34 -0800200 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700203 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
204
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;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700250
251 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700252 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700253
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700254 // Main container view and the model for the widget tray screen.
255 @Thunk WidgetsContainerView mWidgetsView;
256 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700257
Sunny Goyal594d76d2014-11-06 10:12:54 -0800258 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
262 // scroll issues (because the workspace may not have been measured yet) and extra work.
263 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
264 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265
266 private SpannableStringBuilder mDefaultKeySsb = null;
267
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800270 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private boolean mRestoring;
272 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700273 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Michael Jurka1e2f4652013-07-08 18:03:46 -0700275 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700276 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700277 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278
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 Goyal2bba4c32015-05-18 15:42:48 -0700953 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700955 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700957 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200958 // We might have postponed some bind calls until onResume (see waitUntilResume) --
959 // execute them here
960 long startTimeCallbacks = 0;
961 if (DEBUG_RESUME_TIME) {
962 startTimeCallbacks = System.currentTimeMillis();
963 }
964
Michael Jurka1e2f4652013-07-08 18:03:46 -0700965 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
966 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200967 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700968 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200969 if (DEBUG_RESUME_TIME) {
970 Log.d(TAG, "Time spent processing callbacks in onResume: " +
971 (System.currentTimeMillis() - startTimeCallbacks));
972 }
Michael Jurka447bf842013-05-15 14:52:15 +0200973 }
Michael Jurka54554252013-08-01 12:52:23 +0200974 if (mOnResumeCallbacks.size() > 0) {
975 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
976 mOnResumeCallbacks.get(i).run();
977 }
978 mOnResumeCallbacks.clear();
979 }
Winson Chunge4e50662012-01-23 14:45:13 -0800980
981 // Reset the pressed state of icons that were locked in the press state while activities
982 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800983 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800984 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800985 mWaitingForResume.setStayPressed(false);
986 }
Winson Chung82963d52013-10-09 11:20:57 -0700987
Adam Cohen06dff352012-06-01 17:17:08 -0700988 // It is possible that widgets can receive updates while launcher is not in the foreground.
989 // Consequently, the widgets will be inflated in the orientation of the foreground activity
990 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
991 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700992 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -0700993 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700994
Michael Jurka447bf842013-05-15 14:52:15 +0200995 if (DEBUG_RESUME_TIME) {
996 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
997 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700998
999 if (mWorkspace.getCustomContentCallbacks() != null) {
1000 // If we are resuming and the custom content is the current page, we call onShow().
1001 // It is also poassible that onShow will instead be called slightly after first layout
1002 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1003 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001004 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001005 }
1006 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001007 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001008 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001009
Sunny Goyal756adbc2015-04-16 15:20:51 -07001010 if (!isWorkspaceLoading()) {
1011 // Process any items that were added while Launcher was away.
1012 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1013 }
Adam Cohen9211d422014-10-07 18:14:53 -07001014
1015 if (mLauncherCallbacks != null) {
1016 mLauncherCallbacks.onResume();
1017 }
Adam Cohen06dff352012-06-01 17:17:08 -07001018 }
1019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001021 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001022 // Ensure that items added to Launcher are queued until Launcher returns
1023 InstallShortcutReceiver.enableInstallQueue();
1024
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001025 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001026 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001027 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001028 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001029
1030 // We call onHide() aggressively. The custom content callbacks should be able to
1031 // debounce excess onHide calls.
1032 if (mWorkspace.getCustomContentCallbacks() != null) {
1033 mWorkspace.getCustomContentCallbacks().onHide();
1034 }
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Adam Cohen9211d422014-10-07 18:14:53 -07001036 if (mLauncherCallbacks != null) {
1037 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001038 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001039 }
1040
1041 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001042 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1043 // by a onResume or by scrolling otherwise.
1044 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001045
1046 // Custom content is completely hidden
1047 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001048
1049 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1050 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001051
1052 // Indicates whether the user is allowed to scroll away from the custom content.
1053 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001054 }
1055
Adam Cohenc2d6e892014-10-16 09:49:24 -07001056 public interface LauncherOverlay {
1057
1058 /**
1059 * Touch interaction leading to overscroll has begun
1060 */
1061 public void onScrollInteractionBegin();
1062
1063 /**
1064 * Touch interaction related to overscroll has ended
1065 */
1066 public void onScrollInteractionEnd();
1067
1068 /**
1069 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1070 * screen (or in the case of RTL, the rightmost screen).
1071 */
1072 public void onScrollChange(int progress, boolean rtl);
1073
1074 /**
1075 * Screen has stopped scrolling
1076 */
1077 public void onScrollSettled();
1078
1079 /**
1080 * This method can be called by the Launcher in order to force the LauncherOverlay
1081 * to exit fully immersive mode.
1082 */
1083 public void forceExitFullImmersion();
1084 }
1085
Jun Mukai8af0cd82015-05-12 12:32:12 -07001086 public interface LauncherSearchCallbacks {
1087 /**
1088 * Called when the search overlay is shown.
1089 */
1090 public void onSearchOverlayOpened();
1091
1092 /**
1093 * Called when the search overlay is dismissed.
1094 */
1095 public void onSearchOverlayClosed();
1096 }
1097
Adam Cohenc2d6e892014-10-16 09:49:24 -07001098 public interface LauncherOverlayCallbacks {
1099 /**
1100 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1101 * however it doesn't modify any state within the launcher.
1102 */
1103 public boolean canEnterFullImmersion();
1104
1105 /**
1106 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1107 * eg. by occupying the full screen and handling all touch events.
1108 *
1109 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1110 * case, Launcher will modify any necessary state and assumes the overlay is
1111 * handling all interaction. If false, the LauncherOverlay should cancel any
1112 *
1113 */
1114 public boolean enterFullImmersion();
1115
1116 /**
1117 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1118 * full control over UI and state.
1119 */
1120 public void exitFullImmersion();
1121 }
1122
1123 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1124
1125 @Override
1126 public boolean canEnterFullImmersion() {
1127 return mState == State.WORKSPACE;
1128 }
1129
1130 @Override
1131 public boolean enterFullImmersion() {
1132 if (mState == State.WORKSPACE) {
1133 // When fully immersed, disregard any touches which fall through.
1134 mDragLayer.setBlockTouch(true);
1135 return true;
1136 }
1137 return false;
1138 }
1139
1140 @Override
1141 public void exitFullImmersion() {
1142 mDragLayer.setBlockTouch(false);
1143 }
1144 }
1145
Jorim Jaggid017f882014-01-14 17:08:48 -08001146 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001147 if (mLauncherCallbacks != null) {
1148 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001149 } else {
1150 // On devices with a locked orientation, we will at least have the allow rotation
1151 // setting.
1152 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001153 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001154 }
1155
Adam Cohen9211d422014-10-07 18:14:53 -07001156 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001157 CustomContentCallbacks callbacks, String description) {
1158 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001159 }
1160
Adam Cohenedb40762013-07-18 16:45:45 -07001161 // The custom content needs to offset its content to account for the QSB
1162 public int getTopOffsetForCustomContent() {
1163 return mWorkspace.getPaddingTop();
1164 }
1165
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001166 @Override
1167 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001169 if (mModel.isCurrentCallbacks(this)) {
1170 mModel.stopLoader();
1171 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001172 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1173
Romain Guy13c2e7b2010-03-10 19:45:00 -08001174 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001175 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001177 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001178 @Override
1179 public void onWindowFocusChanged(boolean hasFocus) {
1180 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001181 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001182
1183 if (mLauncherCallbacks != null) {
1184 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1185 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001186 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 private boolean acceptFilter() {
1189 final InputMethodManager inputManager = (InputMethodManager)
1190 getSystemService(Context.INPUT_METHOD_SERVICE);
1191 return !inputManager.isFullscreenMode();
1192 }
1193
1194 @Override
1195 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001196 final int uniChar = event.getUnicodeChar();
1197 final boolean handled = super.onKeyDown(keyCode, event);
1198 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1199 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1201 keyCode, event);
1202 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001204 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001205 // showSearchDialog()
1206 // If there are multiple keystrokes before the search dialog takes focus,
1207 // onSearchRequested() will be called for every keystroke,
1208 // but it is idempotent, so it's fine.
1209 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211 }
1212
Joe Onorato8a9625e2010-01-28 15:55:35 -08001213 // Eat the long press event so the keyboard doesn't come up.
1214 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1215 return true;
1216 }
1217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 return handled;
1219 }
1220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 private String getTypedText() {
1222 return mDefaultKeySsb.toString();
1223 }
1224
1225 private void clearTypedText() {
1226 mDefaultKeySsb.clear();
1227 mDefaultKeySsb.clearSpans();
1228 Selection.setSelection(mDefaultKeySsb, 0);
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001232 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1233 * State
1234 */
1235 private static State intToState(int stateOrdinal) {
1236 State state = State.WORKSPACE;
1237 final State[] stateValues = State.values();
1238 for (int i = 0; i < stateValues.length; i++) {
1239 if (stateValues[i].ordinal() == stateOrdinal) {
1240 state = stateValues[i];
1241 break;
1242 }
1243 }
1244 return state;
1245 }
1246
1247 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * Restores the previous state, if it exists.
1249 *
1250 * @param savedState The previous state.
1251 */
1252 private void restoreState(Bundle savedState) {
1253 if (savedState == null) {
1254 return;
1255 }
1256
Michael Jurka883f55b2010-10-21 15:47:14 -07001257 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001258 if (state == State.APPS || state == State.WIDGETS) {
1259 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001260 }
1261
Adam Cohen21cd0022013-10-09 18:57:02 -07001262 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1263 PagedView.INVALID_RESTORE_PAGE);
1264 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001265 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267
Sunny Goyal756cd262015-08-20 12:33:21 -07001268 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1269 if (itemValues != null) {
1270 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001271 AppWidgetProviderInfo info = savedState.getParcelable(
1272 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001273 mPendingAddWidgetInfo = info == null ?
1274 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1275
Adam Cohen4637b5a2013-11-04 18:21:24 -08001276 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001277 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 mRestoring = true;
1279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281
1282 /**
1283 * Finds all the views we need and configure them properly.
1284 */
1285 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001286 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001287
Craig Mautner360310b2012-10-26 15:13:08 -07001288 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001289 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001290 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1291 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001292 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001293 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001294
John Spurlock77e1f472013-09-11 10:09:51 -04001295 mLauncherView.setSystemUiVisibility(
1296 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001297 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298
Winson Chung4c98d922011-05-31 16:50:48 -07001299 // Setup the drag layer
1300 mDragLayer.setup(this, dragController);
1301
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 // Setup the hotseat
1303 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1304 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001305 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 }
1307
Jorim Jaggid017f882014-01-14 17:08:48 -08001308 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Tony Wickhamc75917f2015-09-02 16:11:09 -07001309 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1310 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1311 @Override
1312 public boolean onLongClick(View v) {
1313 return v.performClick();
1314 }
1315 };
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001316 mWidgetsButton = findViewById(R.id.widget_button);
1317 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001318 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001319 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001320 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001321 onClickAddWidgetButton(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001322 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001323 }
1324 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001325 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001326 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001327
1328 View wallpaperButton = findViewById(R.id.wallpaper_button);
1329 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001331 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001332 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001333 onClickWallpaperPicker(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001334 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001335 }
1336 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001337 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
Adam Cohen61f560d2013-09-30 15:58:20 -07001338 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1339
1340 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001341 if (hasSettings()) {
1342 settingsButton.setOnClickListener(new OnClickListener() {
1343 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001344 public void onClick(View view) {
Jorim Jaggid017f882014-01-14 17:08:48 -08001345 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001346 onClickSettingsButton(view);
Jorim Jaggid017f882014-01-14 17:08:48 -08001347 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001348 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001349 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001350 settingsButton.setOnLongClickListener(performClickOnLongClick);
Jorim Jaggid017f882014-01-14 17:08:48 -08001351 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1352 } else {
1353 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001354 }
1355
Adam Cohendbdff6b2013-09-18 19:09:15 -07001356 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001357
Winson Chung4c98d922011-05-31 16:50:48 -07001358 // Setup the workspace
1359 mWorkspace.setHapticFeedbackEnabled(false);
1360 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001361 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001362 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001363
Winson Chungf0ea4d32011-06-06 14:27:16 -07001364 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001365 mSearchDropTargetBar = (SearchDropTargetBar)
1366 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001367
Winson Chungef7f8742015-06-04 17:18:17 -07001368 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001369 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001370 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001371 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1372 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1373 } else {
1374 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1375 }
Craig Mautner360310b2012-10-26 15:13:08 -07001376
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001378 dragController.setDragScoller(mWorkspace);
1379 dragController.setScrollView(mDragLayer);
1380 dragController.setMoveTarget(mWorkspace);
1381 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001382 if (mSearchDropTargetBar != null) {
1383 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001384 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001385 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001386
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001387 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001388 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001389 mWeightWatcher = new WeightWatcher(this);
1390 mWeightWatcher.setAlpha(0.5f);
1391 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001392 new FrameLayout.LayoutParams(
1393 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001394 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001395 Gravity.BOTTOM)
1396 );
Adam Cohen39a06042013-07-19 14:30:12 -07001397
1398 boolean show = shouldShowWeightWatcher();
1399 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402
1403 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001404 * Sets the all apps button. This method is called from {@link Hotseat}.
1405 */
1406 public void setAllAppsButton(View allAppsButton) {
1407 mAllAppsButton = allAppsButton;
1408 }
1409
1410 public View getAllAppsButton() {
1411 return mAllAppsButton;
1412 }
1413
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001414 public View getWidgetsButton() {
1415 return mWidgetsButton;
1416 }
1417
Anjali Koppal5ad44842014-03-10 20:34:39 -07001418 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 * Creates a view representing a shortcut.
1420 *
1421 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001423 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001424 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 /**
1428 * Creates a view representing a shortcut inflated from the specified resource.
1429 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 * @param parent The group the shortcut belongs to.
1431 * @param info The data structure describing the shortcut.
1432 *
1433 * @return A View inflated from layoutResId.
1434 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001435 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001436 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001437 parent, false);
1438 favorite.applyFromShortcutInfo(info, mIconCache);
1439 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001441 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 return favorite;
1443 }
1444
1445 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 * Add a shortcut to the workspace.
1447 *
1448 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001451 int cellY) {
1452 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001453 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001454
Sunny Goyal5c97f512015-05-19 16:03:28 -07001455 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001456 if (info == null) {
1457 return;
1458 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001459 final View view = createShortcut(info);
1460
Sunny Goyal5c97f512015-05-19 16:03:28 -07001461 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 // First we check if we already know the exact location where we want to add this item.
1463 if (cellX >= 0 && cellY >= 0) {
1464 cellXY[0] = cellX;
1465 cellXY[1] = cellY;
1466 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001467
1468 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001469 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001470 true, null,null)) {
1471 return;
1472 }
1473 DragObject dragObject = new DragObject();
1474 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001475 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1476 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001477 return;
1478 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001479 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001481 }
1482
1483 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001484 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001485 return;
1486 }
1487
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001488 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
1490 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001491 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001492 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494 }
1495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001497 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001499 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 */
Adam Cohen091440a2015-03-18 14:16:05 -07001501 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001502 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1503
1504 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001505 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001506 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1507 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001508 }
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Adam Cohen59400422014-03-05 18:07:04 -08001510 if (appWidgetInfo.isCustomWidget) {
1511 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001512 }
1513
Adam Cohen59400422014-03-05 18:07:04 -08001514 LauncherAppWidgetInfo launcherInfo;
1515 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1516 launcherInfo.spanX = info.spanX;
1517 launcherInfo.spanY = info.spanY;
1518 launcherInfo.minSpanX = info.minSpanX;
1519 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001520 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001523 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524
1525 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001526 if (hostView == null) {
1527 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001528 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1529 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 } else {
1531 // The AppWidgetHostView has already been inflated and instantiated
1532 launcherInfo.hostView = hostView;
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001535 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001536 launcherInfo.notifyWidgetSizeChanged(this);
1537
Adam Cohen59400422014-03-05 18:07:04 -08001538 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1539 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001540
1541 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001543 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Adam Cohended9f8d2010-11-03 13:25:16 -07001546 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1547 @Override
1548 public void onReceive(Context context, Intent intent) {
1549 final String action = intent.getAction();
1550 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1551 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001552 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001553 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001554
Winson Chungc100e8e2011-08-09 16:02:43 -07001555 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001556 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001557 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001558 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001559 if (!showWorkspace(false)) {
1560 // If we are already on the workspace, then manually reset all apps
1561 mAppsView.reset();
1562 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001563 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1565 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001566 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001567 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1568 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001569 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001570 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1571 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1572 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001573 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001574 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1575 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001576 }
1577 }
1578 };
1579
1580 @Override
1581 public void onAttachedToWindow() {
1582 super.onAttachedToWindow();
1583
1584 // Listen for broadcasts related to user-presence
1585 final IntentFilter filter = new IntentFilter();
1586 filter.addAction(Intent.ACTION_SCREEN_OFF);
1587 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001588 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 if (ENABLE_DEBUG_INTENTS) {
1590 filter.addAction(DebugIntents.DELETE_DATABASE);
1591 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1592 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001594 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001595 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001596 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 mVisible = true;
1598 }
1599
Adam Cohen0b395352014-06-09 22:54:36 +00001600 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001601 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001602 * This method is a no-op for other platform versions.
1603 */
Sunny Goyald0091012015-01-20 15:55:34 -08001604 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001605 private void setupTransparentSystemBarsForLollipop() {
1606 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001607 Window window = getWindow();
1608 window.getAttributes().systemUiVisibility |=
1609 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1610 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1611 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1612 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1613 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1614 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1615 window.setStatusBarColor(Color.TRANSPARENT);
1616 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001617 }
1618 }
1619
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 @Override
1621 public void onDetachedFromWindow() {
1622 super.onDetachedFromWindow();
1623 mVisible = false;
1624
Adam Cohend113e0c2010-11-11 10:48:05 -08001625 if (mAttached) {
1626 unregisterReceiver(mReceiver);
1627 mAttached = false;
1628 }
Winson Chungb745afb2015-03-02 11:51:23 -08001629 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 }
1631
1632 public void onWindowVisibilityChanged(int visibility) {
1633 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001634 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001635 // The following code used to be in onResume, but it turns out onResume is called when
1636 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1637 // is a more appropriate event to handle
1638 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001639 if (!mWorkspaceLoading) {
1640 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001641 // We want to let Launcher draw itself at least once before we force it to build
1642 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 // apps is nice and speedy.
1644 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001645 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001646 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001647 if (mStarted) return;
1648 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001649 // We delay the layer building a bit in order to give
1650 // other message processing a time to run. In particular
1651 // this avoids a delay in hiding the IME if it was
1652 // currently shown, because doing that may involve
1653 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001654 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001655 final ViewTreeObserver.OnDrawListener listener = this;
1656 mWorkspace.post(new Runnable() {
1657 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001658 if (mWorkspace != null &&
1659 mWorkspace.getViewTreeObserver() != null) {
1660 mWorkspace.getViewTreeObserver().
1661 removeOnDrawListener(listener);
1662 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001663 }
1664 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001665 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 }
1667 });
1668 }
1669 clearTypedText();
1670 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672
Adam Cohen091440a2015-03-18 14:16:05 -07001673 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 mHandler.removeMessages(ADVANCE_MSG);
1675 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1676 mHandler.sendMessageDelayed(msg, delay);
1677 mAutoAdvanceSentTime = System.currentTimeMillis();
1678 }
1679
Adam Cohen091440a2015-03-18 14:16:05 -07001680 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1682 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1683 mAutoAdvanceRunning = autoAdvanceRunning;
1684 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001685 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 sendAdvanceMessage(delay);
1687 } else {
1688 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001689 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1691 }
1692 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001693 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 }
1695 }
1696 }
1697
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001698 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1699
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001701 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 if (msg.what == ADVANCE_MSG) {
1703 int i = 0;
1704 for (View key: mWidgetsToAdvance.keySet()) {
1705 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001706 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001708 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 public void run() {
1710 ((Advanceable) v).advance();
1711 }
1712 }, delay);
1713 }
1714 i++;
1715 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001716 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001718 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001720 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001721
Winsonc0b52fe2015-09-09 16:38:15 -07001722 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001723 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1725 if (v instanceof Advanceable) {
1726 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001727 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001728 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730 }
1731
Winsonc0b52fe2015-09-09 16:38:15 -07001732 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 if (mWidgetsToAdvance.containsKey(hostView)) {
1734 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001735 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 }
1738
Hyunyoung Song3f471442015-04-08 19:01:34 -07001739 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001740 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1741 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 }
1743
Winson Chunga6945242014-01-08 14:04:34 -08001744 public DragLayer getDragLayer() {
1745 return mDragLayer;
1746 }
1747
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001748 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001749 return mAppsView;
1750 }
1751
Hyunyoung Song3f471442015-04-08 19:01:34 -07001752 public WidgetsContainerView getWidgetsView() {
1753 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001754 }
1755
Winson Chunga6945242014-01-08 14:04:34 -08001756 public Workspace getWorkspace() {
1757 return mWorkspace;
1758 }
1759
1760 public Hotseat getHotseat() {
1761 return mHotseat;
1762 }
1763
Jorim Jaggid017f882014-01-14 17:08:48 -08001764 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001765 return mOverviewPanel;
1766 }
1767
Winson Chung006ee262015-08-03 14:40:11 -07001768 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001769 return mSearchDropTargetBar;
1770 }
1771
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001772 public LauncherAppWidgetHost getAppWidgetHost() {
1773 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
Romain Guycbb89e42009-06-08 15:52:54 -07001775
Winson Chunga9abd0e2010-10-27 17:18:37 -07001776 public LauncherModel getModel() {
1777 return mModel;
1778 }
1779
Winson Chunga6945242014-01-08 14:04:34 -08001780 protected SharedPreferences getSharedPrefs() {
1781 return mSharedPrefs;
1782 }
1783
Adam Cohen2e6da152015-05-06 11:42:25 -07001784 public DeviceProfile getDeviceProfile() {
1785 return mDeviceProfile;
1786 }
1787
Bjorn Bringertc459e522013-06-07 19:36:01 +01001788 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001789 getWindow().closeAllPanels();
1790
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001791 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001792 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001793 }
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 @Override
1796 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001797 long startTime = 0;
1798 if (DEBUG_RESUME_TIME) {
1799 startTime = System.currentTimeMillis();
1800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 super.onNewIntent(intent);
1802
1803 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001804 Folder openFolder = mWorkspace.getOpenFolder();
1805 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1806 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1807 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1808 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1809 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001810 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001811 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001812
Adam Cohen6fecd412013-10-02 17:41:50 -07001813 if (mWorkspace == null) {
1814 // Can be cases where mWorkspace is null, this prevents a NPE
1815 return;
1816 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001817 // In all these cases, only animate if we're already on home
1818 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001819
Adam Cohen6fecd412013-10-02 17:41:50 -07001820 closeFolder();
1821 exitSpringLoadedDragMode();
1822
1823 // If we are already on home, then just animate back to the workspace,
1824 // otherwise, just wait until onResume to set the state back to Workspace
1825 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001826 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001827 } else {
1828 mOnResumeState = State.WORKSPACE;
1829 }
1830
1831 final View v = getWindow().peekDecorView();
1832 if (v != null && v.getWindowToken() != null) {
1833 InputMethodManager imm = (InputMethodManager)getSystemService(
1834 INPUT_METHOD_SERVICE);
1835 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1836 }
1837
Winson Chungb745afb2015-03-02 11:51:23 -08001838 // Reset the apps view
1839 if (!alreadyOnHome && mAppsView != null) {
1840 mAppsView.scrollToTop();
1841 }
1842
Hyunyoung Song3f471442015-04-08 19:01:34 -07001843 // Reset the widgets view
1844 if (!alreadyOnHome && mWidgetsView != null) {
1845 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001847
Adam Cohen9211d422014-10-07 18:14:53 -07001848 if (mLauncherCallbacks != null) {
1849 mLauncherCallbacks.onHomeIntent();
1850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
Adam Cohened307df2013-10-02 09:37:31 -07001852
Adam Cohen9211d422014-10-07 18:14:53 -07001853 if (mLauncherCallbacks != null) {
1854 mLauncherCallbacks.onNewIntent(intent);
1855 }
Winson15f8b172015-08-19 11:02:39 -07001856
1857 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1858 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1859 // animation.
1860 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001861 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1862 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001863 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1864 openFolder == null && moveToDefaultScreen) {
1865 mWorkspace.post(new Runnable() {
1866 @Override
1867 public void run() {
1868 mWorkspace.moveToDefaultScreen(true);
1869 }
1870 });
1871 }
1872 }
1873
1874 if (DEBUG_RESUME_TIME) {
1875 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1876 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001877 }
1878
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001880 public void onRestoreInstanceState(Bundle state) {
1881 super.onRestoreInstanceState(state);
1882 for (int page: mSynchronouslyBoundPages) {
1883 mWorkspace.restoreInstanceStateForChild(page);
1884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
1887 @Override
1888 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001889 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001890 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1891 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001892 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001893 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894
Michael Jurka883f55b2010-10-21 15:47:14 -07001895 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001896 // We close any open folder since it will not be re-opened, and we need to make sure
1897 // this state is reflected.
1898 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001901 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001902 ContentValues itemValues = new ContentValues();
1903 mPendingAddInfo.writeToValues(itemValues);
1904 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001905 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001906 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908
Hyunyoung Song3f471442015-04-08 19:01:34 -07001909 // Save the current widgets tray?
1910 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001911
1912 if (mLauncherCallbacks != null) {
1913 mLauncherCallbacks.onSaveInstanceState(outState);
1914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916
1917 @Override
1918 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Winson Chunge7a03942011-08-05 15:05:12 -07001921 // Remove all pending runnables
1922 mHandler.removeMessages(ADVANCE_MSG);
1923 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001924 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001925
Winson Chungcd2b0142011-06-08 16:02:26 -07001926 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001927 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001928
1929 // It's possible to receive onDestroy after a new Launcher activity has
1930 // been created. In this case, don't interfere with the new Launcher.
1931 if (mModel.isCurrentCallbacks(this)) {
1932 mModel.stopLoader();
1933 app.setLauncher(null);
1934 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001937 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001939 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001941 mAppWidgetHost = null;
1942
1943 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
1945 TextKeyListener.getInstance().release();
1946
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001947 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001948
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001949 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001950 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001951 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001952 mWorkspace = null;
1953 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001954
Michael Jurka2ecf9952012-06-18 12:52:28 -07001955 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001956
1957 if (mLauncherCallbacks != null) {
1958 mLauncherCallbacks.onDestroy();
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 public DragController getDragController() {
1963 return mDragController;
1964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
1967 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001968 onStartForResult(requestCode);
1969 super.startActivityForResult(intent, requestCode);
1970 }
1971
1972 @Override
1973 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1974 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1975 onStartForResult(requestCode);
1976 try {
1977 super.startIntentSenderForResult(intent, requestCode,
1978 fillInIntent, flagsMask, flagsValues, extraFlags, options);
1979 } catch (IntentSender.SendIntentException e) {
1980 throw new ActivityNotFoundException();
1981 }
1982 }
1983
1984 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001985 if (requestCode >= 0) {
1986 setWaitingForResult(true);
1987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
Winson Chung70d72102011-08-12 11:24:35 -07001990 /**
1991 * Indicates that we want global search for this activity by setting the globalSearch
1992 * argument for {@link #startSearch} to true.
1993 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001995 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001997
Karl Rosaen138a0412009-04-23 19:00:21 -07001998 if (initialQuery == null) {
1999 // Use any text typed in the launcher as the initial query
2000 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 if (appSearchData == null) {
2003 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002004 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
Winson Chungadf0c182012-08-23 14:59:07 -07002006 Rect sourceBounds = new Rect();
2007 if (mSearchDropTargetBar != null) {
2008 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2009 }
Romain Guycbb89e42009-06-08 15:52:54 -07002010
Ian Parkinson047036e2014-09-01 15:40:53 +01002011 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002012 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002013 if (clearTextImmediately) {
2014 clearTypedText();
2015 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002016
2017 // We need to show the workspace after starting the search
2018 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002019 }
2020
Ian Parkinson047036e2014-09-01 15:40:53 +01002021 /**
2022 * Start a text search.
2023 *
2024 * @return {@code true} if the search will start immediately, so any further keypresses
2025 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2026 * to buffer keypresses.
2027 */
2028 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002029 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002030 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2031 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2032 sourceBounds);
2033 }
2034
Michael Jurkaa33411c2012-06-14 16:18:21 -07002035 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002036 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002037 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002038 }
2039
2040 /**
2041 * Starts the global search activity. This code is a copied from SearchManager
2042 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002043 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002044 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002045 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002046 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2047 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2048 if (globalSearchActivity == null) {
2049 Log.w(TAG, "No global search activity found.");
2050 return;
2051 }
2052 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2053 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2054 intent.setComponent(globalSearchActivity);
2055 // Make sure that we have a Bundle to put source in
2056 if (appSearchData == null) {
2057 appSearchData = new Bundle();
2058 } else {
2059 appSearchData = new Bundle(appSearchData);
2060 }
Adam Cohen9211d422014-10-07 18:14:53 -07002061 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002062 if (!appSearchData.containsKey("source")) {
2063 appSearchData.putString("source", getPackageName());
2064 }
2065 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2066 if (!TextUtils.isEmpty(initialQuery)) {
2067 intent.putExtra(SearchManager.QUERY, initialQuery);
2068 }
2069 if (selectInitialQuery) {
2070 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2071 }
2072 intent.setSourceBounds(sourceBounds);
2073 try {
2074 startActivity(intent);
2075 } catch (ActivityNotFoundException ex) {
2076 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
2079
Winson Chungbedf9232015-07-10 12:38:30 -07002080 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2081 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2082 return;
2083 }
2084
2085 // If not handled, then just start the provided search intent
2086 startActivitySafely(v, searchIntent, null);
2087 }
2088
Yura4f93ec62014-02-04 14:15:21 +00002089 public boolean isOnCustomContent() {
2090 return mWorkspace.isOnOrMovingToCustomContent();
2091 }
2092
Winson Chung70d72102011-08-12 11:24:35 -07002093 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002094 public boolean onPrepareOptionsMenu(Menu menu) {
2095 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002096 if (!isOnCustomContent()) {
2097 // Close any open folders
2098 closeFolder();
2099 // Stop resizing any widgets
2100 mWorkspace.exitWidgetResizeMode();
2101 if (!mWorkspace.isInOverviewMode()) {
2102 // Show the overview mode
2103 showOverviewMode(true);
2104 } else {
2105 showWorkspace(true);
2106 }
Winson Chunge0298742014-01-17 12:03:00 -08002107 }
Adam Cohen9211d422014-10-07 18:14:53 -07002108 if (mLauncherCallbacks != null) {
2109 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2110 }
2111
Michael Jurkab94f3f82013-09-11 18:08:54 +02002112 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002115 @Override
2116 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002117 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002118 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002119 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002120 }
2121
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 public boolean isWorkspaceLocked() {
2123 return mWorkspaceLoading || mWaitingForResult;
2124 }
2125
Adam Cohen517a7f52014-03-01 12:12:59 -08002126 public boolean isWorkspaceLoading() {
2127 return mWorkspaceLoading;
2128 }
2129
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002130 private void setWorkspaceLoading(boolean value) {
2131 boolean isLocked = isWorkspaceLocked();
2132 mWorkspaceLoading = value;
2133 if (isLocked != isWorkspaceLocked()) {
2134 onWorkspaceLockedChanged();
2135 }
2136 }
2137
2138 private void setWaitingForResult(boolean value) {
2139 boolean isLocked = isWorkspaceLocked();
2140 mWaitingForResult = value;
2141 if (isLocked != isWorkspaceLocked()) {
2142 onWorkspaceLockedChanged();
2143 }
2144 }
2145
Adam Cohen9211d422014-10-07 18:14:53 -07002146 protected void onWorkspaceLockedChanged() {
2147 if (mLauncherCallbacks != null) {
2148 mLauncherCallbacks.onWorkspaceLockedChanged();
2149 }
2150 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002151
Michael Jurka0280c3b2010-09-17 15:00:07 -07002152 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002153 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002154 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002155 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2156 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002157 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002158 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002159
Sunny Goyale6b63a32015-01-16 16:14:29 -08002160 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002161 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002162 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2163 }
2164
Sunny Goyale6b63a32015-01-16 16:14:29 -08002165 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002166 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2167 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002168 if (appWidgetInfo.configure != null) {
2169 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002170 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002171
Bjorn Bringert7984c942009-12-09 15:38:25 +00002172 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002173 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2174 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002177 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002178 Runnable onComplete = new Runnable() {
2179 @Override
2180 public void run() {
2181 // Exit spring loaded mode if necessary after adding the widget
2182 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2183 null);
2184 }
2185 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002186 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002187 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002188 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
2190 }
Romain Guycbb89e42009-06-08 15:52:54 -07002191
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002192 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002193 // Close any folders that may be open.
2194 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002195 mWorkspace.moveToCustomContentScreen(animate);
2196 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002197
2198 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2199 int[] cell, int spanX, int spanY) {
2200 switch (info.itemType) {
2201 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2202 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2203 int span[] = new int[2];
2204 span[0] = spanX;
2205 span[1] = spanY;
2206 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2207 container, screenId, cell, span);
2208 break;
2209 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2210 processShortcutFromDrop(info.componentName, container, screenId, cell);
2211 break;
2212 default:
2213 throw new IllegalStateException("Unknown item type: " + info.itemType);
2214 }
2215 }
2216
Adam Cohenfbba09b2011-07-18 21:43:05 -07002217 /**
2218 * Process a shortcut drop.
2219 *
2220 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002222 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002223 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002224 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2225 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002226 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229
2230 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 mPendingAddInfo.cellX = cell[0];
2232 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234
2235 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2236 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002237 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 }
2239
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 /**
2241 * Process a widget drop.
2242 *
2243 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002247 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2248 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002252 mPendingAddInfo.minSpanX = info.minSpanX;
2253 mPendingAddInfo.minSpanY = info.minSpanY;
2254
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.cellX = cell[0];
2257 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002259 if (span != null) {
2260 mPendingAddInfo.spanX = span[0];
2261 mPendingAddInfo.spanY = span[1];
2262 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263
Adam Cohened66b2b2012-01-23 17:28:51 -08002264 AppWidgetHostView hostView = info.boundWidget;
2265 int appWidgetId;
2266 if (hostView != null) {
2267 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002268 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002269
2270 // Clear the boundWidget so that it doesn't get destroyed.
2271 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002272 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002273 // In this case, we either need to start an activity to get permission to bind
2274 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002275 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002276 Bundle options = info.bindOptions;
2277
Sunny Goyalffe83f12014-08-14 17:39:34 -07002278 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2279 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002280 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002281 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002282 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002283 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002284 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2285 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2286 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002287 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2288 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002289 // TODO: we need to make sure that this accounts for the options bundle.
2290 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002291 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2292 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294 }
2295
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002298 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 folderInfo.title = getText(R.string.folder_name);
2300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002302 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2303 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002304 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305
2306 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 FolderIcon newFolder =
2308 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002311 // Force measure the new folder icon
2312 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2313 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002314 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 }
Romain Guycbb89e42009-06-08 15:52:54 -07002316
Winsonc0b52fe2015-09-09 16:38:15 -07002317 /**
2318 * Unbinds the view for the specified item, and removes the item and all its children items
2319 * from the database. For folders, this incl udes the folder contents. AppWidgets will also
2320 * have their widget ids deleted.
2321 */
2322 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
2323 if (itemInfo instanceof ShortcutInfo) {
2324 mWorkspace.removeWorkspaceItem(v);
2325 if (deleteFromDb) {
2326 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2327 }
2328 } else if (itemInfo instanceof FolderInfo) {
2329 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2330 unbindFolder(folderInfo);
2331 mWorkspace.removeWorkspaceItem(v);
2332 if (deleteFromDb) {
2333 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2334 }
2335 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2336 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
2337 unbindAppWidget(widgetInfo, deleteFromDb);
2338 if (deleteFromDb) {
2339 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
2340 }
2341 } else {
2342 return false;
2343 }
2344 return true;
2345 }
2346
2347 /**
2348 * Unbinds any launcher references to the folder.
2349 */
2350 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002351 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002352 }
2353
Winsonc0b52fe2015-09-09 16:38:15 -07002354 /**
2355 * Unbinds any launcher references to the widget and deletes the app widget id.
2356 */
2357 private void unbindAppWidget(final LauncherAppWidgetInfo widgetInfo, boolean deleteAppWidgetId) {
2358 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
2359 if (deleteAppWidgetId && appWidgetHost != null &&
2360 !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
2361 // Deleting an app widget ID is a void call but writes to disk before returning
2362 // to the caller...
2363 new AsyncTask<Void, Void, Void>() {
2364 public Void doInBackground(Void ... args) {
2365 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2366 return null;
2367 }
2368 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2369 }
2370 removeWidgetToAutoAdvance(widgetInfo.hostView);
2371 widgetInfo.hostView = null;
2372 }
2373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 @Override
2375 public boolean dispatchKeyEvent(KeyEvent event) {
2376 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2377 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002379 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002380 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002381 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002382 dumpState();
2383 return true;
2384 }
2385 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002386 }
2387 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2388 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002389 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 return true;
2391 }
2392 }
2393
2394 return super.dispatchKeyEvent(event);
2395 }
2396
Joe Onorato88ec0992009-11-19 13:16:06 -08002397 @Override
2398 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002399 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2400 return;
2401 }
2402
Sunny Goyal45478022015-06-08 16:52:41 -07002403 if (mDragController.isDragging()) {
2404 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002405 return;
2406 }
2407
Winson Chungb745afb2015-03-02 11:51:23 -08002408 if (isAppsViewVisible()) {
2409 showWorkspace(true);
2410 } else if (isWidgetsViewVisible()) {
2411 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002412 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002413 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002414 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002415 Folder openFolder = mWorkspace.getOpenFolder();
2416 if (openFolder.isEditingName()) {
2417 openFolder.dismissEditingName();
2418 } else {
2419 closeFolder();
2420 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002421 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002422 mWorkspace.exitWidgetResizeMode();
2423
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002424 // Back button is a no-op here, but give at least some feedback for the button press
2425 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002426 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002427 }
2428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002430 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002431 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002432 @Override
2433 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002434 if (mAppWidgetHost != null) {
2435 mAppWidgetHost.startListening();
2436 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002437 }
2438
2439 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 * Launches the intent referred by the clicked shortcut.
2441 *
2442 * @param v The view representing the clicked shortcut.
2443 */
2444 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002445 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2446 // view has detached (it's possible for this to happen if the view is removed mid touch).
2447 if (v.getWindowToken() == null) {
2448 return;
2449 }
2450
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002451 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002452 return;
2453 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002454
Adam Cohen1697b792013-09-17 19:08:21 -07002455 if (v instanceof Workspace) {
2456 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002457 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002458 }
2459 return;
2460 }
2461
2462 if (v instanceof CellLayout) {
2463 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002464 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2465 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002466 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002467 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002468 }
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002471 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002472 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002474 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002475 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002476 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002477 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002478 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002479 } else if (tag instanceof AppInfo) {
2480 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002481 } else if (tag instanceof LauncherAppWidgetInfo) {
2482 if (v instanceof PendingAppWidgetHostView) {
2483 onClickPendingWidget((PendingAppWidgetHostView) v);
2484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 }
2486 }
2487
Sunny Goyal70660032015-05-14 00:07:08 -07002488 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002489 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002490 return false;
2491 }
2492
Michael Jurkaaf442092010-06-10 17:01:57 -07002493 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002494 * Event handler for the app widget view which has not fully restored.
2495 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002496 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002497 if (mIsSafeModeEnabled) {
2498 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2499 return;
2500 }
2501
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002502 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002503 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002504 int widgetId = info.appWidgetId;
2505 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2506 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002507 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2508 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002509 mPendingAddInfo.copyFrom(info);
2510 mPendingAddWidgetId = widgetId;
2511
Sunny Goyalffe83f12014-08-14 17:39:34 -07002512 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2513 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002514 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002515 } else if (info.installProgress < 0) {
2516 // The install has not been queued
2517 final String packageName = info.providerName.getPackageName();
2518 showBrokenAppInstallDialog(packageName,
2519 new DialogInterface.OnClickListener() {
2520 public void onClick(DialogInterface dialog, int id) {
2521 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2522 }
2523 });
2524 } else {
2525 // Download has started.
2526 final String packageName = info.providerName.getPackageName();
2527 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002528 }
2529 }
2530
2531 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002532 * Event handler for the "grid" button that appears on the home screen, which
2533 * enters all apps mode.
2534 *
2535 * @param v The view that was clicked.
2536 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 protected void onClickAllAppsButton(View v) {
2538 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002539 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002540 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002541 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002542
2543 if (mLauncherCallbacks != null) {
2544 mLauncherCallbacks.onClickAllAppsButton(v);
2545 }
Winson Chung76648c52015-07-10 14:33:23 -07002546 }
2547 }
2548
2549 protected void onLongClickAllAppsButton(View v) {
2550 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2551 if (!isAppsViewVisible()) {
2552 showAppsView(true /* animated */, false /* resetListToTop */,
2553 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002554 }
2555 }
2556
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002557 private void showBrokenAppInstallDialog(final String packageName,
2558 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002559 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 .setTitle(R.string.abandoned_promises_title)
2561 .setMessage(R.string.abandoned_promise_explanation)
2562 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2563 .setNeutralButton(R.string.abandoned_clean_this,
2564 new DialogInterface.OnClickListener() {
2565 public void onClick(DialogInterface dialog, int id) {
2566 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2567 mWorkspace.removeAbandonedPromise(packageName, user);
2568 }
2569 })
2570 .create().show();
2571 return;
2572 }
2573
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002574 /**
2575 * Event handler for an app shortcut click.
2576 *
2577 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2578 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002579 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2581 Object tag = v.getTag();
2582 if (!(tag instanceof ShortcutInfo)) {
2583 throw new IllegalArgumentException("Input must be a Shortcut");
2584 }
2585
2586 // Open shortcut
2587 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002588
2589 if (shortcut.isDisabled != 0) {
2590 int error = R.string.activity_not_available;
2591 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2592 error = R.string.safemode_shortcut_error;
2593 }
2594 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2595 return;
2596 }
2597
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 final Intent intent = shortcut.intent;
2599
2600 // Check for special shortcuts
2601 if (intent.getComponent() != null) {
2602 final String shortcutClass = intent.getComponent().getClassName();
2603
2604 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2605 MemoryDumpActivity.startDump(this);
2606 return;
2607 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2608 toggleShowWeightWatcher();
2609 return;
2610 }
2611 }
2612
Chris Wren40c5ed32014-06-24 18:24:23 -04002613 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002614 if ((v instanceof BubbleTextView)
2615 && shortcut.isPromise()
2616 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002617 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002618 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002619 new DialogInterface.OnClickListener() {
2620 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002621 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002622 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 return;
2625 }
2626
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002628 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002629
2630 if (mLauncherCallbacks != null) {
2631 mLauncherCallbacks.onClickAppShortcut(v);
2632 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 }
2634
Adam Cohen091440a2015-03-18 14:16:05 -07002635 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002637 final ShortcutInfo shortcut;
2638 final Intent intent;
2639 if (tag instanceof ShortcutInfo) {
2640 shortcut = (ShortcutInfo) tag;
2641 intent = shortcut.intent;
2642 int[] pos = new int[2];
2643 v.getLocationOnScreen(pos);
2644 intent.setSourceBounds(new Rect(pos[0], pos[1],
2645 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002646
Sunny Goyal508da152014-08-14 10:53:27 -07002647 } else if (tag instanceof AppInfo) {
2648 shortcut = null;
2649 intent = ((AppInfo) tag).intent;
2650 } else {
2651 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2652 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653
2654 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002655 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656
2657 if (success && v instanceof BubbleTextView) {
2658 mWaitingForResume = (BubbleTextView) v;
2659 mWaitingForResume.setStayPressed(true);
2660 }
2661 }
2662
2663 /**
2664 * Event handler for a folder icon click.
2665 *
2666 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2667 */
2668 protected void onClickFolderIcon(View v) {
2669 if (LOGD) Log.d(TAG, "onClickFolder");
2670 if (!(v instanceof FolderIcon)){
2671 throw new IllegalArgumentException("Input must be a FolderIcon");
2672 }
2673
Sunny Goyal317698b2015-07-29 11:45:41 -07002674 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675 FolderIcon folderIcon = (FolderIcon) v;
2676 final FolderInfo info = folderIcon.getFolderInfo();
2677 Folder openFolder = mWorkspace.getFolderForTag(info);
2678
2679 // If the folder info reports that the associated folder is open, then verify that
2680 // it is actually opened. There have been a few instances where this gets out of sync.
2681 if (info.opened && openFolder == null) {
2682 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2683 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2684 info.opened = false;
2685 }
2686
2687 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2688 // Close any open folder
2689 closeFolder();
2690 // Open the requested folder
2691 openFolder(folderIcon);
2692 } else {
2693 // Find the open folder...
2694 int folderScreen;
2695 if (openFolder != null) {
2696 folderScreen = mWorkspace.getPageForView(openFolder);
2697 // .. and close it
2698 closeFolder(openFolder);
2699 if (folderScreen != mWorkspace.getCurrentPage()) {
2700 // Close any folder open on the current screen
2701 closeFolder();
2702 // Pull the folder onto this screen
2703 openFolder(folderIcon);
2704 }
2705 }
2706 }
Adam Cohen9211d422014-10-07 18:14:53 -07002707
2708 if (mLauncherCallbacks != null) {
2709 mLauncherCallbacks.onClickFolderIcon(v);
2710 }
Michael Jurka5130e402011-10-13 04:55:35 -07002711 }
2712
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002713 /**
2714 * Event handler for the (Add) Widgets button that appears after a long press
2715 * on the home screen.
2716 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002717 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002719 if (mIsSafeModeEnabled) {
2720 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2721 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002722 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002723 if (mLauncherCallbacks != null) {
2724 mLauncherCallbacks.onClickAddWidgetButton(view);
2725 }
Adam Cohen9211d422014-10-07 18:14:53 -07002726 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002727 }
2728
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002729 /**
2730 * Event handler for the wallpaper picker button that appears after a long press
2731 * on the home screen.
2732 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002733 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002735 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2736 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2737 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2738 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002739 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002740
2741 if (mLauncherCallbacks != null) {
2742 mLauncherCallbacks.onClickWallpaperPicker(v);
2743 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 }
2745
2746 /**
2747 * Event handler for a click on the settings button that appears after a long press
2748 * on the home screen.
2749 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002750 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002751 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002752 if (mLauncherCallbacks != null) {
2753 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002754 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002755 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002756 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 }
2758
Adam Cohen61f560d2013-09-30 15:58:20 -07002759 public View.OnTouchListener getHapticFeedbackTouchListener() {
2760 if (mHapticFeedbackTouchListener == null) {
2761 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002762 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002763 @Override
2764 public boolean onTouch(View v, MotionEvent event) {
2765 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2766 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2767 }
2768 return false;
2769 }
2770 };
2771 }
2772 return mHapticFeedbackTouchListener;
2773 }
2774
Adam Cohen9211d422014-10-07 18:14:53 -07002775 public void onDragStarted(View view) {
2776 if (isOnCustomContent()) {
2777 // Custom content screen doesn't participate in drag and drop. If on custom
2778 // content screen, move to default.
2779 moveWorkspaceToDefaultScreen();
2780 }
2781
2782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onDragStarted(view);
2784 }
2785 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002786
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002787 /**
2788 * Called when the user stops interacting with the launcher.
2789 * This implies that the user is now on the homescreen and is not doing housekeeping.
2790 */
Adam Cohen9211d422014-10-07 18:14:53 -07002791 protected void onInteractionEnd() {
2792 if (mLauncherCallbacks != null) {
2793 mLauncherCallbacks.onInteractionEnd();
2794 }
2795 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002796
2797 /**
2798 * Called when the user starts interacting with the launcher.
2799 * The possible interactions are:
2800 * - open all apps
2801 * - reorder an app shortcut, or a widget
2802 * - open the overview mode.
2803 * This is a good time to stop doing things that only make sense
2804 * when the user is on the homescreen and not doing housekeeping.
2805 */
Adam Cohen9211d422014-10-07 18:14:53 -07002806 protected void onInteractionBegin() {
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onInteractionBegin();
2809 }
2810 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002811
Winson Chungcd99cd32015-04-29 11:03:24 -07002812 /** Updates the interaction state. */
2813 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002814 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002815 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002816 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2817 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002818 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002819 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002820 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002821 onInteractionEnd();
2822 }
2823 }
2824
Kenny Guyf07af7b2014-07-31 11:39:16 +01002825 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002826 try {
2827 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002828 launcherApps.showAppDetailsForProfile(componentName, user);
2829 } catch (SecurityException e) {
2830 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2831 Log.e(TAG, "Launcher does not have permission to launch settings");
2832 } catch (ActivityNotFoundException e) {
2833 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2834 Log.e(TAG, "Unable to launch settings");
2835 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002836 }
2837
Michael Jurka1e2f4652013-07-08 18:03:46 -07002838 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002839 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2840 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002841 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002842 // System applications cannot be installed. For now, show a toast explaining that.
2843 // We may give them the option of disabling apps this way.
2844 int messageId = R.string.uninstall_system_app_text;
2845 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002846 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002847 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002848 String packageName = componentName.getPackageName();
2849 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002850 Intent intent = new Intent(
2851 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2853 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002854 if (user != null) {
2855 user.addToIntent(intent, Intent.EXTRA_USER);
2856 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002857 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002858 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002859 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002860 }
2861
Winson Chung8f1eff72015-05-28 17:33:40 -07002862 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002863 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002865 // Only launch using the new animation if the shortcut has not opted out (this is a
2866 // private contract between launcher and may be ignored in the future).
2867 boolean useLaunchAnimation = (v != null) &&
2868 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002869 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2870 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002871
Kenny Guy1317e2d2014-05-08 18:52:50 +01002872 UserHandleCompat user = null;
2873 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2874 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2875 user = userManager.getUserForSerialNumber(serialNumber);
2876 }
2877
2878 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002879 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002880 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002881 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002882 int left = 0, top = 0;
2883 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2884 if (v instanceof TextView) {
2885 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002886 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2887 if (icon != null) {
2888 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002889 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002890 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002891 width = bounds.width();
2892 height = bounds.height();
2893 }
2894 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002895 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2896 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002897 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002898 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002899 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002900 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002901 // On L devices, we use the device default slide-up transition.
2902 // On L MR1 devices, we a custom version of the slide-up transition which
2903 // doesn't have the delay present in the device default.
2904 opts = ActivityOptions.makeCustomAnimation(this,
2905 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002906 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002907 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002908 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002909
2910 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2911 // Could be launching some bookkeeping activity
2912 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002913 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002914 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002915 launcherApps.startActivityForProfile(intent.getComponent(), user,
2916 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002918 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 } catch (SecurityException e) {
2920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002921 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002923 "or use the exported attribute for this activity. "
2924 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002926 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002928
Winson Chungbedf9232015-07-10 12:38:30 -07002929 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002930 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002931 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2932 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2933 return false;
2934 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002935 try {
2936 success = startActivity(v, intent, tag);
2937 } catch (ActivityNotFoundException e) {
2938 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2939 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2940 }
2941 return success;
2942 }
2943
Adam Cohen268c4752012-06-06 17:47:33 -07002944 /**
2945 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2946 * in the DragLayer in the exact absolute location of the original FolderIcon.
2947 */
2948 private void copyFolderIconToImage(FolderIcon fi) {
2949 final int width = fi.getMeasuredWidth();
2950 final int height = fi.getMeasuredHeight();
2951
2952 // Lazy load ImageView, Bitmap and Canvas
2953 if (mFolderIconImageView == null) {
2954 mFolderIconImageView = new ImageView(this);
2955 }
2956 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2957 mFolderIconBitmap.getHeight() != height) {
2958 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2959 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2960 }
2961
2962 DragLayer.LayoutParams lp;
2963 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2964 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2965 } else {
2966 lp = new DragLayer.LayoutParams(width, height);
2967 }
2968
Adam Cohen307fe232012-08-16 17:55:58 -07002969 // The layout from which the folder is being opened may be scaled, adjust the starting
2970 // view size by this scale factor.
2971 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002972 lp.customPosition = true;
2973 lp.x = mRectForFolderAnimation.left;
2974 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002975 lp.width = (int) (scale * width);
2976 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002977
2978 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2979 fi.draw(mFolderIconCanvas);
2980 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002981 if (fi.getFolder() != null) {
2982 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2983 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002984 }
Adam Cohen268c4752012-06-06 17:47:33 -07002985 // Just in case this image view is still in the drag layer from a previous animation,
2986 // we remove it and re-add it.
2987 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2988 mDragLayer.removeView(mFolderIconImageView);
2989 }
2990 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002991 if (fi.getFolder() != null) {
2992 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002993 }
Adam Cohen268c4752012-06-06 17:47:33 -07002994 }
2995
Adam Cohen2801caf2011-05-13 20:57:39 -07002996 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002997 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002998 FolderInfo info = (FolderInfo) fi.getTag();
2999 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3000 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003001 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3002 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003003 }
3004
Adam Cohen268c4752012-06-06 17:47:33 -07003005 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3006 copyFolderIconToImage(fi);
3007 fi.setVisibility(View.INVISIBLE);
3008
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003009 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3010 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003011 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003012 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3013 }
3014 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003015 oa.start();
3016 }
3017
Adam Cohen268c4752012-06-06 17:47:33 -07003018 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003019 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003020 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003021
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // We remove and re-draw the FolderIcon in-case it has changed
3023 mDragLayer.removeView(mFolderIconImageView);
3024 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003025 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003026 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003027 oa.addListener(new AnimatorListenerAdapter() {
3028 @Override
3029 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003030 if (cl != null) {
3031 cl.clearFolderLeaveBehind();
3032 // Remove the ImageView copy of the FolderIcon and make the original visible.
3033 mDragLayer.removeView(mFolderIconImageView);
3034 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003035 }
3036 }
3037 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003038 oa.start();
3039 }
3040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003042 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 * is animated relative to the specified View. If the View is null, no animation
3044 * is played.
3045 *
3046 * @param folderInfo The FolderInfo describing the folder to open.
3047 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003048 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003049 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003050 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3051 if (openFolder != null && openFolder != folder) {
3052 // Close any open folder before opening a folder.
3053 closeFolder();
3054 }
3055
Adam Cohena9cf38f2011-05-02 15:36:58 -07003056 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057
Adam Cohena9cf38f2011-05-02 15:36:58 -07003058 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059
Sunny Goyalf4066152015-04-15 09:42:19 -07003060 // While the folder is open, the position of the icon cannot change.
3061 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3062
Adam Cohen4554ee12011-08-03 16:13:21 -07003063 // Just verify that the folder hasn't already been added to the DragLayer.
3064 // There was a one-off crash where the folder had a parent already.
3065 if (folder.getParent() == null) {
3066 mDragLayer.addView(folder);
3067 mDragController.addDropTarget((DropTarget) folder);
3068 } else {
3069 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3070 folder.getParent() + ").");
3071 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003072 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003073 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003074
3075 // Notify the accessibility manager that this folder "window" has appeared and occluded
3076 // the workspace items
3077 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3078 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 }
3080
3081 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003082 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003084 if (folder.isEditingName()) {
3085 folder.dismissEditingName();
3086 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003087 closeFolder(folder);
3088 }
3089 }
3090
Sunny Goyal83a8f042015-05-19 12:52:12 -07003091 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 folder.getInfo().opened = false;
3093
3094 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3095 if (parent != null) {
3096 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3097 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003098 if (fi != null) {
3099 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3100 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003101 }
3102 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003103
3104 // Notify the accessibility manager that this folder "window" has disappeard and no
3105 // longer occludeds the workspace items
3106 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 }
3108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003110 if (!isDraggingEnabled()) return false;
3111 if (isWorkspaceLocked()) return false;
3112 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113
Winson Chung76648c52015-07-10 14:33:23 -07003114 if (v == mAllAppsButton) {
3115 onLongClickAllAppsButton(v);
3116 return true;
3117 }
3118
Adam Cohen1697b792013-09-17 19:08:21 -07003119 if (v instanceof Workspace) {
3120 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003121 if (!mWorkspace.isTouchActive()) {
3122 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003123 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3124 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3125 return true;
3126 } else {
3127 return false;
3128 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003129 } else {
3130 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003131 }
Adam Cohen1697b792013-09-17 19:08:21 -07003132 }
3133
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003134 CellLayout.CellInfo longClickCellInfo = null;
3135 View itemUnderLongClick = null;
3136 if (v.getTag() instanceof ItemInfo) {
3137 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003138 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003139 itemUnderLongClick = longClickCellInfo.cell;
3140 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 }
3142
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 // The hotseat touch handling does not go through Workspace, and we always allow long press
3144 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003145 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003146 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003147 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003148 // User long pressed on empty space
3149 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3150 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003151 if (mWorkspace.isInOverviewMode()) {
3152 mWorkspace.startReordering(v);
3153 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003154 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003157 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3158 mHotseat.getOrderInHotseat(
3159 longClickCellInfo.cellX,
3160 longClickCellInfo.cellY));
3161 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003163 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165 }
3166 }
3167 return true;
3168 }
3169
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 boolean isHotseatLayout(View layout) {
3171 return mHotseat != null && layout != null &&
3172 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3173 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 /**
3176 * Returns the CellLayout of the specified container at the specified screen.
3177 */
Sunny Goyal92820592015-03-02 11:31:35 -08003178 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3180 if (mHotseat != null) {
3181 return mHotseat.getLayout();
3182 } else {
3183 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003184 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003186 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003187 }
3188 }
3189
Winson Chungb745afb2015-03-02 11:51:23 -08003190 /**
3191 * For overridden classes.
3192 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003193 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003194 return isAppsViewVisible();
3195 }
3196
3197 public boolean isAppsViewVisible() {
3198 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3199 }
3200
3201 public boolean isWidgetsViewVisible() {
3202 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003203 }
3204
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003205 private void setWorkspaceBackground(int background) {
3206 switch (background) {
3207 case WORKSPACE_BACKGROUND_TRANSPARENT:
3208 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3209 break;
3210 case WORKSPACE_BACKGROUND_BLACK:
3211 getWindow().setBackgroundDrawable(null);
3212 break;
3213 default:
3214 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3215 }
Craig Mautner360310b2012-10-26 15:13:08 -07003216 }
3217
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003218 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003219 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3220 int curflags = getWindow().getAttributes().flags
3221 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3222 if (wpflags != curflags) {
3223 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3224 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003225 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003226 }
3227
Michael Jurkae326f182011-11-21 14:05:46 -08003228 @Override
3229 public void onTrimMemory(int level) {
3230 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003231 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3232 // The widget preview db can result in holding onto over
3233 // 3MB of memory for caching which isn't necessary.
3234 SQLiteDatabase.releaseMemory();
3235
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003236 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003237 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003238 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003239 if (mLauncherCallbacks != null) {
3240 mLauncherCallbacks.onTrimMemory(level);
3241 }
Michael Jurkae326f182011-11-21 14:05:46 -08003242 }
3243
Winson5c6bdbb2015-09-03 11:36:19 -07003244 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003245 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003246 }
3247
Winson5c6bdbb2015-09-03 11:36:19 -07003248 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003249 boolean changed = mState != State.WORKSPACE ||
3250 mWorkspace.getState() != Workspace.State.NORMAL;
3251 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003252 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003253 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003254 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003255
Michael Jurkab3e22d92011-10-31 15:58:33 -07003256 // Set focus to the AppsCustomize button
3257 if (mAllAppsButton != null) {
3258 mAllAppsButton.requestFocus();
3259 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003260 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003261
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003262 // Change the state *after* we've called all the transition code
3263 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003264
Winson Chung5fb63472011-02-02 17:03:37 -08003265 // Resume the auto-advance of widgets
3266 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003267 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003268
Winson Chung0f785722015-04-08 10:27:49 -07003269 if (changed) {
3270 // Send an accessibility event to announce the context change
3271 getWindow().getDecorView()
3272 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003273 }
Winson5c6bdbb2015-09-03 11:36:19 -07003274 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003275 }
3276
Adam Cohened307df2013-10-02 09:37:31 -07003277 void showOverviewMode(boolean animated) {
3278 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003279 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003280 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003281 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003282 }
3283
Winson Chungb745afb2015-03-02 11:51:23 -08003284 /**
3285 * Shows the apps view.
3286 */
Winson Chung76648c52015-07-10 14:33:23 -07003287 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3288 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003289 if (resetListToTop) {
3290 mAppsView.scrollToTop();
3291 }
Winson Chung4ac30062015-05-08 17:34:17 -07003292 if (updatePredictedApps) {
3293 tryAndUpdatePredictedApps();
3294 }
Winson Chung76648c52015-07-10 14:33:23 -07003295 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003296 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297
Winson Chungb745afb2015-03-02 11:51:23 -08003298 /**
3299 * Shows the widgets view.
3300 */
3301 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003302 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003303 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003304 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003305 }
Winson Chung76648c52015-07-10 14:33:23 -07003306 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003307
3308 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003309 @Override
3310 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003312 }
3313 });
Winson Chungb745afb2015-03-02 11:51:23 -08003314 }
3315
3316 /**
3317 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003318 *
3319 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003320 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003321 // TODO: calling method should use the return value so that when {@code false} is returned
3322 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003323 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003324 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3325 mState != State.WIDGETS_SPRING_LOADED) {
3326 return false;
3327 }
3328 if (toState != State.APPS && toState != State.WIDGETS) {
3329 return false;
3330 }
Winson Chungb745afb2015-03-02 11:51:23 -08003331
3332 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003333 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3334 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003336 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003337 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003338
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003340 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341
3342 // Pause the auto-advance of widgets until we are out of AllApps
3343 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003344 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003345 closeFolder();
3346
3347 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003348 getWindow().getDecorView()
3349 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003350 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351 }
3352
Winson Chungcd99cd32015-04-29 11:03:24 -07003353 /**
3354 * Updates the workspace and interaction state on state change, and return the animation to this
3355 * new state.
3356 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003357 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003358 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003359 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003360 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003361 updateInteraction(fromState, toState);
3362 return anim;
3363 }
3364
Jun Mukaif0033da2015-08-04 18:34:30 -07003365 public void onLauncherClingShown() {
3366 // When a launcher cling appears, it should cover the underlying layers, so their focus
3367 // should be blocked.
3368 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3369 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3370 }
3371 }
3372
3373 public void onLauncherClingDismissed() {
3374 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3375 }
3376
Hyunyoung Song3f471442015-04-08 19:01:34 -07003377 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003378 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003379 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3380 mState == State.WIDGETS_SPRING_LOADED) {
3381 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003382 }
Winson Chungb745afb2015-03-02 11:51:23 -08003383
Winson Chung006ee262015-08-03 14:40:11 -07003384 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003385 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003386 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003387 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003388 }
Adam Cohen7777d962011-08-18 18:58:38 -07003389
Hyunyoung Song3f471442015-04-08 19:01:34 -07003390 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003391 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003392 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003393
Winson Chunge7a03942011-08-05 15:05:12 -07003394 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003395 @Override
3396 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003397 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003398 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3399 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003400 // Before we show workspace, hide all apps again because
3401 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3402 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003403 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003404 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003405 } else {
3406 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003407 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003408 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003409 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003410 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411
Michael Jurkad3ef3062010-11-23 16:23:58 -08003412 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003413 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003414 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003415 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003416 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003417 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003418 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003419 }
3420
Winson Chung4ac30062015-05-08 17:34:17 -07003421 /**
3422 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3423 * resumed.
3424 */
3425 private void tryAndUpdatePredictedApps() {
3426 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003427 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003428 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003429 mAppsView.setPredictedApps(apps);
3430 }
3431 }
3432 }
3433
Michael Jurkab3e22d92011-10-31 15:58:33 -07003434 void lockAllApps() {
3435 // TODO
3436 }
3437
3438 void unlockAllApps() {
3439 // TODO
3440 }
3441
Sunny Goyal594d76d2014-11-06 10:12:54 -08003442 protected void disableVoiceButtonProxy(boolean disable) {
3443 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003444 }
3445
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003446 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003447 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3448 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003449 }
3450
Adam Cohen24ce0b32014-01-14 16:18:14 -08003451 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003452 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3453 if (searchProvider == null) {
3454 return null;
3455 }
3456
3457 Bundle opts = new Bundle();
3458 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003459 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003460
3461 SharedPreferences sp = getSharedPreferences(
3462 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3463 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003464 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003465 if (!searchProvider.provider.flattenToString().equals(
3466 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003467 || (widgetInfo == null)
3468 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003469 // A valid widget is not already bound.
3470 if (widgetId > -1) {
3471 mAppWidgetHost.deleteAppWidgetId(widgetId);
3472 widgetId = -1;
3473 }
3474
3475 // Try to bind a new widget
3476 widgetId = mAppWidgetHost.allocateAppWidgetId();
3477
3478 if (!AppWidgetManagerCompat.getInstance(this)
3479 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3480 mAppWidgetHost.deleteAppWidgetId(widgetId);
3481 widgetId = -1;
3482 }
3483
3484 sp.edit()
3485 .putInt(QSB_WIDGET_ID, widgetId)
3486 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3487 .commit();
3488 }
3489
Sunny Goyal8d595092015-07-06 12:22:14 -07003490 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003491 if (widgetId != -1) {
3492 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003493 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003494 mQsb.updateAppWidgetOptions(opts);
3495 mQsb.setPadding(0, 0, 0, 0);
3496 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003497 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003498 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003499 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003500 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003501 }
3502
Sunny Goyal22235bc2015-04-03 09:23:43 -07003503 private void reinflateQSBIfNecessary() {
3504 if (mQsb instanceof LauncherAppWidgetHostView &&
3505 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3506 mSearchDropTargetBar.removeView(mQsb);
3507 mQsb = null;
3508 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3509 }
3510 }
3511
Michael Jurkad7c28052012-04-27 15:43:36 -07003512 @Override
3513 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003514 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003515 final List<CharSequence> text = event.getText();
3516 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003517 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003518 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003519 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003520 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003521 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003522 } else if (mWorkspace != null) {
3523 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003524 } else {
3525 text.add(getString(R.string.all_apps_home_button_label));
3526 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003527 return result;
3528 }
3529
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003530 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003531 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003532 */
Adam Cohen091440a2015-03-18 14:16:05 -07003533 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003534 @Override
3535 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003536 closeSystemDialogs();
3537 }
3538 }
3539
3540 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003541 * If the activity is currently paused, signal that we need to run the passed Runnable
3542 * in onResume.
3543 *
3544 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003545 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3546 * wrong when we're not running, and if the activity comes back to what the configuration was
3547 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003551 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003552 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003553 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003555 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003556 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003557 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003558 }
3559 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003560 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003561 return true;
3562 } else {
3563 return false;
3564 }
3565 }
3566
Michael Jurkac402cd92013-05-20 15:49:32 +02003567 private boolean waitUntilResume(Runnable run) {
3568 return waitUntilResume(run, false);
3569 }
3570
Michael Jurka1e2f4652013-07-08 18:03:46 -07003571 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003572 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003573 }
3574
Michael Jurka7607c2f2013-04-03 14:33:19 -07003575 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003576 * If the activity is currently paused, signal that we need to re-run the loader
3577 * in onResume.
3578 *
3579 * This needs to be called from incoming places where resources might have been loaded
3580 * while we are paused. That is becaues the Configuration might be wrong
3581 * when we're not running, and if it comes back to what it was when we
3582 * were paused, we are not restarted.
3583 *
3584 * Implementation of the method from LauncherModel.Callbacks.
3585 *
3586 * @return true if we are currently paused. The caller might be able to
3587 * skip some work in that case since we will come back again.
3588 */
3589 public boolean setLoadOnResume() {
3590 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003591 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003592 mOnResumeNeedsLoad = true;
3593 return true;
3594 } else {
3595 return false;
3596 }
3597 }
3598
3599 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003601 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003603 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003604 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003605 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003606 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003607 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003609
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003611 * Clear any pending bind callbacks. This is called when is loader is planning to
3612 * perform a full rebind from scratch.
3613 */
3614 @Override
3615 public void clearPendingBinds() {
3616 mBindOnResumeCallbacks.clear();
3617 if (mPendingExecutor != null) {
3618 mPendingExecutor.markCompleted();
3619 mPendingExecutor = null;
3620 }
3621 }
3622
3623 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 * Refreshes the shortcuts shown on the workspace.
3625 *
3626 * Implementation of the method from LauncherModel.Callbacks.
3627 */
3628 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003629 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003630
Winson Chungd64d1762013-08-20 14:37:16 -07003631 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003632 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003633 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003634
Michael Jurka05bf644e2011-11-30 20:28:53 -08003635 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003636 if (mHotseat != null) {
3637 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
3639 }
3640
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 @Override
3642 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003643 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003644
Adam Cohen5084cba2013-09-03 12:01:16 -07003645 // If there are no screens, we need to have an empty screen
3646 if (orderedScreenIds.size() == 0) {
3647 mWorkspace.addExtraEmptyScreen();
3648 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003649
3650 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003651 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003652 if (hasCustomContentToLeft()) {
3653 mWorkspace.createCustomContentContainer();
3654 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003655 }
Winson Chung64359a52013-07-08 17:17:08 -07003656 }
3657
3658 @Override
3659 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003660 int count = orderedScreenIds.size();
3661 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003662 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003663 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003664 }
3665
Adam Cohen39a06042013-07-19 14:30:12 -07003666 private boolean shouldShowWeightWatcher() {
3667 String spKey = LauncherAppState.getSharedPreferencesKey();
3668 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003669 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003670
3671 return show;
3672 }
3673
3674 private void toggleShowWeightWatcher() {
3675 String spKey = LauncherAppState.getSharedPreferencesKey();
3676 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3677 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3678
3679 show = !show;
3680
3681 SharedPreferences.Editor editor = sp.edit();
3682 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3683 editor.commit();
3684
3685 if (mWeightWatcher != null) {
3686 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3687 }
3688 }
3689
Winson Chungd64d1762013-08-20 14:37:16 -07003690 public void bindAppsAdded(final ArrayList<Long> newScreens,
3691 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003692 final ArrayList<ItemInfo> addAnimated,
3693 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003696 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003697 }
3698 };
3699 if (waitUntilResume(r)) {
3700 return;
3701 }
3702
Winson Chungd64d1762013-08-20 14:37:16 -07003703 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003704 if (newScreens != null) {
3705 bindAddScreens(newScreens);
3706 }
Winson Chungd64d1762013-08-20 14:37:16 -07003707
3708 // We add the items without animation on non-visible pages, and with
3709 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003710 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 bindItems(addNotAnimated, 0,
3712 addNotAnimated.size(), false);
3713 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003714 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003715 bindItems(addAnimated, 0,
3716 addAnimated.size(), true);
3717 }
Winson Chungc58497e2013-09-03 17:48:37 -07003718
Winson Chung87412982013-10-03 18:34:14 -07003719 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003720 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003721
Winson Chungb745afb2015-03-02 11:51:23 -08003722 if (addedApps != null && mAppsView != null) {
3723 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725 }
3726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 * Bind the items start-end from the list.
3729 *
3730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 */
Winson Chung64359a52013-07-08 17:17:08 -07003732 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3733 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
3736 bindItems(shortcuts, start, end, forceAnimateIcons);
3737 }
3738 };
3739 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 return;
3741 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003742
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003744 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3745 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003746 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003748 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003751
3752 // Short circuit if we are loading dock items for a configuration which has no dock
3753 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3754 mHotseat == null) {
3755 continue;
3756 }
3757
Sunny Goyal639e9062015-08-19 19:17:06 -07003758 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 switch (item.itemType) {
3760 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3761 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003763 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003764
Winson Chung64359a52013-07-08 17:17:08 -07003765 /*
3766 * TODO: FIX collision case
3767 */
Winson Chungce376632013-07-11 16:12:41 -07003768 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3769 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3770 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003771 View v = cl.getChildAt(item.cellX, item.cellY);
3772 Object tag = v.getTag();
3773 String desc = "Collision while binding workspace item: " + item
3774 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003775 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003776 throw (new RuntimeException(desc));
3777 } else {
3778 Log.d(TAG, desc);
3779 }
Winson Chungce376632013-07-11 16:12:41 -07003780 }
Winson Chung64359a52013-07-08 17:17:08 -07003781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003783 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003784 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003785 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003786 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003788 default:
3789 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003791
3792 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3793 item.cellY, 1, 1);
3794 if (animateIcons) {
3795 // Animate all the applications up now
3796 view.setAlpha(0f);
3797 view.setScaleX(0f);
3798 view.setScaleY(0f);
3799 bounceAnims.add(createNewAppBounceAnimation(view, i));
3800 newShortcutsScreenId = item.screenId;
3801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003802 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003803
Winson Chung997a9232013-07-24 15:33:46 -07003804 if (animateIcons) {
3805 // Animate to the correct page
3806 if (newShortcutsScreenId > -1) {
3807 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003808 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003809 final Runnable startBounceAnimRunnable = new Runnable() {
3810 public void run() {
3811 anim.playTogether(bounceAnims);
3812 anim.start();
3813 }
3814 };
Winson Chung997a9232013-07-24 15:33:46 -07003815 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003816 // We post the animation slightly delayed to prevent slowdowns
3817 // when we are loading right after we return to launcher.
3818 mWorkspace.postDelayed(new Runnable() {
3819 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003820 if (mWorkspace != null) {
3821 mWorkspace.snapToPage(newScreenIndex);
3822 mWorkspace.postDelayed(startBounceAnimRunnable,
3823 NEW_APPS_ANIMATION_DELAY);
3824 }
Winson Chung94d67682013-09-25 16:29:40 -07003825 }
3826 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003827 } else {
3828 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003829 }
3830 }
Winson Chung64359a52013-07-08 17:17:08 -07003831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003833 }
3834
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 /**
3836 * Implementation of the method from LauncherModel.Callbacks.
3837 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003838 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003839 Runnable r = new Runnable() {
3840 public void run() {
3841 bindFolders(folders);
3842 }
3843 };
3844 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003845 return;
3846 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003847 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003848 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849
3850 /**
3851 * Add the views for a widget to the workspace.
3852 *
3853 * Implementation of the method from LauncherModel.Callbacks.
3854 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003855 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003856 Runnable r = new Runnable() {
3857 public void run() {
3858 bindAppWidget(item);
3859 }
3860 };
3861 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003862 return;
3863 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003864
Daniel Sandler843e8602010-06-07 14:59:01 -04003865 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3866 if (DEBUG_WIDGETS) {
3867 Log.d(TAG, "bindAppWidget: " + item);
3868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 final Workspace workspace = mWorkspace;
3870
Adam Cohen59400422014-03-05 18:07:04 -08003871 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003872 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003873
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003874 if (!mIsSafeModeEnabled
3875 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003876 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3877
Sunny Goyalff572272014-07-23 13:58:07 -07003878 if (appWidgetInfo == null) {
3879 if (DEBUG_WIDGETS) {
3880 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3881 + " belongs to component " + item.providerName
3882 + ", as the povider is null");
3883 }
3884 LauncherModel.deleteItemFromDatabase(this, item);
3885 return;
3886 }
Sunny Goyalff572272014-07-23 13:58:07 -07003887
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003888 // If we do not have a valid id, try to bind an id.
3889 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3890 // Note: This assumes that the id remap broadcast is received before this step.
3891 // If that is not the case, the id remap will be ignored and user may see the
3892 // click to setup view.
3893 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3894 pendingInfo.spanX = item.spanX;
3895 pendingInfo.spanY = item.spanY;
3896 pendingInfo.minSpanX = item.minSpanX;
3897 pendingInfo.minSpanY = item.minSpanY;
3898 Bundle options = null;
3899 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003900
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003901 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3902 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3903 newWidgetId, appWidgetInfo, options);
3904
3905 // TODO consider showing a permission dialog when the widget is clicked.
3906 if (!success) {
3907 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3910 + " belongs to component " + item.providerName
3911 + ", as the launcher is unable to bing a new widget id");
3912 }
3913 LauncherModel.deleteItemFromDatabase(this, item);
3914 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003915 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003916
3917 item.appWidgetId = newWidgetId;
3918
3919 // If the widget has a configure activity, it is still needs to set it up, otherwise
3920 // the widget is ready to go.
3921 item.restoreStatus = (appWidgetInfo.configure == null)
3922 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3923 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3924
3925 LauncherModel.updateItemInDatabase(this, item);
3926 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3927 && (appWidgetInfo.configure == null)) {
3928 // If the ID is already valid, verify if we need to configure or not.
3929 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3930 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003931 }
Sunny Goyalff572272014-07-23 13:58:07 -07003932 }
3933
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003934 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003935 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003936 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003937 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3938 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003939 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003940
Sunny Goyal651077b2014-06-30 14:15:31 -07003941 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003942 item.minSpanX = appWidgetInfo.minSpanX;
3943 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003944 } else {
3945 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003946 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3947 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003948 view.updateIcon(mIconCache);
3949 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003950 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003951 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003955 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956
Adam Cohendcd297f2013-06-18 13:13:40 -07003957 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003959 if (!item.isCustomWidget()) {
3960 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3961 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003962
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 workspace.requestLayout();
3964
Daniel Sandler843e8602010-06-07 14:59:01 -04003965 if (DEBUG_WIDGETS) {
3966 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3967 + (SystemClock.uptimeMillis()-start) + "ms");
3968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 }
3970
Sunny Goyalff572272014-07-23 13:58:07 -07003971 /**
3972 * Restores a pending widget.
3973 *
3974 * @param appWidgetId The app widget id
3975 * @param cellInfo The position on screen where to create the widget.
3976 */
3977 private void completeRestoreAppWidget(final int appWidgetId) {
3978 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3979 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3980 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3981 return;
3982 }
3983
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003984 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003985 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3986
3987 mWorkspace.reinflateWidgetsIfNecessary();
3988 LauncherModel.updateItemInDatabase(this, info);
3989 }
3990
Adam Cohen1462de32012-07-24 22:34:36 -07003991 public void onPageBoundSynchronously(int page) {
3992 mSynchronouslyBoundPages.add(page);
3993 }
3994
Sunny Goyal527c7d32015-08-28 15:19:36 -07003995 @Override
3996 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
3997 if (mPendingExecutor != null) {
3998 mPendingExecutor.markCompleted();
3999 }
4000 mPendingExecutor = executor;
4001 executor.attachTo(this);
4002 }
4003
4004 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4005 if (mPendingExecutor == executor) {
4006 mPendingExecutor = null;
4007 }
4008 }
4009
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 /**
4011 * Callback saying that there aren't any more items to bind.
4012 *
4013 * Implementation of the method from LauncherModel.Callbacks.
4014 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004015 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004016 Runnable r = new Runnable() {
4017 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004018 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004019 }
4020 };
4021 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004022 return;
4023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 if (mSavedState != null) {
4025 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004026 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 mSavedState = null;
4029 }
4030
Adam Cohen1462de32012-07-24 22:34:36 -07004031 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004033 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004034 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004035
4036 // If we received the result of any pending adds while the loader was running (e.g. the
4037 // widget configuration forced an orientation change), process them now.
4038 if (sPendingAddItem != null) {
4039 final long screenId = completeAdd(sPendingAddItem);
4040
4041 // TODO: this moves the user to the page where the pending item was added. Ideally,
4042 // the screen would be guaranteed to exist after bind, and the page would be set through
4043 // the workspace restore process.
4044 mWorkspace.post(new Runnable() {
4045 @Override
4046 public void run() {
4047 mWorkspace.snapToScreenId(screenId);
4048 }
4049 });
4050 sPendingAddItem = null;
4051 }
4052
Sunny Goyal756adbc2015-04-16 15:20:51 -07004053 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004054
4055 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004056 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004057 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004058 }
4059
Adam Cohen3ed316a2014-07-23 18:21:20 -07004060 private void sendLoadingCompleteBroadcastIfNecessary() {
4061 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4062 String permission =
4063 getResources().getString(R.string.receive_first_load_broadcast_permission);
4064 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4065 sendBroadcast(intent, permission);
4066 SharedPreferences.Editor editor = mSharedPrefs.edit();
4067 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4068 editor.apply();
4069 }
4070 }
4071
Winson Chunga0b7e862013-09-05 16:03:15 -07004072 public boolean isAllAppsButtonRank(int rank) {
4073 if (mHotseat != null) {
4074 return mHotseat.isAllAppsButtonRank(rank);
4075 }
4076 return false;
4077 }
4078
Winson Chunga2413752012-04-03 14:22:34 -07004079 private boolean canRunNewAppsAnimation() {
4080 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004081 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4082 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004083 }
4084
Winson Chungc9168342013-06-26 14:54:55 -07004085 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004086 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004087 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4088 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004089 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004090 return bounceAnim;
4091 }
4092
Adam Cohen27772732013-11-11 14:00:56 +00004093 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004094 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004095 }
4096
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004097 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004098 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004099 }
4100
Winson Chung88fa7412015-08-03 14:25:28 -07004101 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004102 if (mSearchDropTargetBar == null) {
4103 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004104 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004105 if (mQsb != null) {
4106 mSearchDropTargetBar.removeView(mQsb);
4107 mQsb = null;
4108 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004109 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004110 }
4111
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004112 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004113 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4114 * multiple calls to bind the same list.)
4115 */
4116 @Thunk ArrayList<AppInfo> mTmpAppsList;
4117 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4118 public void run() {
4119 bindAllApplications(mTmpAppsList);
4120 mTmpAppsList = null;
4121 }
4122 };
4123
4124 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 * Add the icons for all apps.
4126 *
4127 * Implementation of the method from LauncherModel.Callbacks.
4128 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004129 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004130 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4131 mTmpAppsList = apps;
4132 return;
4133 }
4134
Winson Chungb745afb2015-03-02 11:51:23 -08004135 if (mAppsView != null) {
4136 mAppsView.setApps(apps);
4137 }
Adam Cohen9211d422014-10-07 18:14:53 -07004138 if (mLauncherCallbacks != null) {
4139 mLauncherCallbacks.bindAllApplications(apps);
4140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 }
4142
4143 /**
4144 * A package was updated.
4145 *
4146 * Implementation of the method from LauncherModel.Callbacks.
4147 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004148 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004149 Runnable r = new Runnable() {
4150 public void run() {
4151 bindAppsUpdated(apps);
4152 }
4153 };
4154 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004155 return;
4156 }
4157
Winson Chungb745afb2015-03-02 11:51:23 -08004158 if (mAppsView != null) {
4159 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161 }
4162
Sunny Goyal4390ace2014-10-13 11:33:11 -07004163 @Override
4164 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4165 Runnable r = new Runnable() {
4166 public void run() {
4167 bindWidgetsRestored(widgets);
4168 }
4169 };
4170 if (waitUntilResume(r)) {
4171 return;
4172 }
4173 mWorkspace.widgetsRestored(widgets);
4174 }
4175
Joe Onorato9c1289c2009-08-17 11:03:03 -04004176 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004177 * Some shortcuts were updated in the background.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004181 @Override
4182 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4183 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004184 Runnable r = new Runnable() {
4185 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004186 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004187 }
4188 };
4189 if (waitUntilResume(r)) {
4190 return;
4191 }
4192
Sunny Goyal4390ace2014-10-13 11:33:11 -07004193 if (!updated.isEmpty()) {
4194 mWorkspace.updateShortcuts(updated);
4195 }
4196
4197 if (!removed.isEmpty()) {
4198 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4199 for (ShortcutInfo si : removed) {
4200 removedComponents.add(si.getTargetComponent());
4201 }
4202 mWorkspace.removeItemsByComponentName(removedComponents, user);
4203 // Notify the drag controller
4204 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004205 }
4206 }
4207
4208 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004209 * Update the state of a package, typically related to install state.
4210 *
4211 * Implementation of the method from LauncherModel.Callbacks.
4212 */
Sunny Goyale755d462014-07-22 13:48:29 -07004213 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004214 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4215 Runnable r = new Runnable() {
4216 public void run() {
4217 bindRestoreItemsChange(updates);
4218 }
4219 };
4220 if (waitUntilResume(r)) {
4221 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004222 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004223
Sunny Goyal756adbc2015-04-16 15:20:51 -07004224 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004225 }
4226
4227 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004228 * A package was uninstalled. We take both the super set of packageNames
4229 * in addition to specific applications to remove, the reason being that
4230 * this can be called when a package is updated as well. In that scenario,
4231 * we only remove specific components from the workspace, where as
4232 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004233 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004234 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004235 * Implementation of the method from LauncherModel.Callbacks.
4236 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004237 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004238 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004239 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004240 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004241 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004242 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004243 }
Winson Chungd64d1762013-08-20 14:37:16 -07004244 };
4245 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004246 return;
4247 }
4248
Sunny Goyal1a745e82014-10-02 15:58:31 -07004249 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004250 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4251 for (AppInfo info : appInfos) {
4252 removedComponents.add(info.componentName);
4253 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004254 if (!packageNames.isEmpty()) {
4255 mWorkspace.removeItemsByPackageName(packageNames, user);
4256 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004257 if (!removedComponents.isEmpty()) {
4258 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004260 // Notify the drag controller
4261 mDragController.onAppsRemoved(packageNames, removedComponents);
4262
Sunny Goyal1a745e82014-10-02 15:58:31 -07004263 } else {
4264 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004265 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004266
Winson Chungdf95eb12013-10-16 14:57:07 -07004267 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004268 if (mAppsView != null) {
4269 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004270 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004271 }
Joe Onoratobe386092009-11-17 17:32:16 -08004272
Michael Jurkac402cd92013-05-20 15:49:32 +02004273 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004274 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004275 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004276 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004277 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004278
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004279 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004280 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004281 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004282 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004283 return;
4284 }
4285
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004286 if (mWidgetsView != null && model != null) {
4287 mWidgetsView.addWidgets(model);
4288 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004289 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004290 }
4291
Winson Chung400438b2011-01-16 17:53:48 -08004292 private int mapConfigurationOriActivityInfoOri(int configOri) {
4293 final Display d = getWindowManager().getDefaultDisplay();
4294 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4295 switch (d.getRotation()) {
4296 case Surface.ROTATION_0:
4297 case Surface.ROTATION_180:
4298 // We are currently in the same basic orientation as the natural orientation
4299 naturalOri = configOri;
4300 break;
4301 case Surface.ROTATION_90:
4302 case Surface.ROTATION_270:
4303 // We are currently in the other basic orientation to the natural orientation
4304 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4305 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4306 break;
4307 }
4308
4309 int[] oriMap = {
4310 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4311 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4312 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4313 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4314 };
4315 // Since the map starts at portrait, we need to offset if this device's natural orientation
4316 // is landscape.
4317 int indexOffset = 0;
4318 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4319 indexOffset = 1;
4320 }
4321 return oriMap[(d.getRotation() + indexOffset) % 4];
4322 }
Adam Cohen446e9402011-09-15 18:21:21 -07004323
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004324 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004325 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004326 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004327 if (Utilities.ATLEAST_JB_MR2) {
4328 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4329 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004330 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4331 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004332 }
Winson Chung4b919f82012-05-01 10:44:08 -07004333 }
4334 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004335
Winson Chung4b919f82012-05-01 10:44:08 -07004336 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004337 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004338 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004339 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004340 } else {
4341 mHandler.postDelayed(new Runnable() {
4342 public void run() {
4343 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4344 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004345 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004346 }
Winson Chung4b919f82012-05-01 10:44:08 -07004347 }
Winson Chung400438b2011-01-16 17:53:48 -08004348 }
4349
Winson Chunge43a1e72014-01-15 10:33:02 -08004350 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004351 if (mLauncherCallbacks != null) {
4352 return mLauncherCallbacks.isLauncherPreinstalled();
4353 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004354 PackageManager pm = getPackageManager();
4355 try {
4356 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4357 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4358 return true;
4359 } else {
4360 return false;
4361 }
4362 } catch (NameNotFoundException e) {
4363 e.printStackTrace();
4364 return false;
4365 }
4366 }
4367
Adam Cohenea90f832014-05-21 15:03:34 -07004368 /**
4369 * This method indicates whether or not we should suggest default wallpaper dimensions
4370 * when our wallpaper cropper was not yet used to set a wallpaper.
4371 */
4372 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004373 if (mLauncherCallbacks != null) {
4374 return mLauncherCallbacks.overrideWallpaperDimensions();
4375 }
Adam Cohenea90f832014-05-21 15:03:34 -07004376 return true;
4377 }
4378
Adam Cohen432609a2014-03-13 17:03:22 -07004379 /**
4380 * To be overridden by subclasses to indicate that there is an activity to launch
4381 * before showing the standard launcher experience.
4382 */
4383 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004384 if (mLauncherCallbacks != null) {
4385 return mLauncherCallbacks.hasFirstRunActivity();
4386 }
Adam Cohen432609a2014-03-13 17:03:22 -07004387 return false;
4388 }
4389
4390 /**
4391 * To be overridden by subclasses to launch any first run activity
4392 */
4393 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004394 if (mLauncherCallbacks != null) {
4395 return mLauncherCallbacks.getFirstRunActivity();
4396 }
Adam Cohen432609a2014-03-13 17:03:22 -07004397 return null;
4398 }
4399
Winson Chunga6945242014-01-08 14:04:34 -08004400 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004401 return !ActivityManager.isRunningInTestHarness() &&
4402 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004403 }
4404
Adam Cohen4a9423d2014-03-28 14:22:31 -07004405 protected boolean hasRunFirstRunActivity() {
4406 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4407 }
4408
Adam Cohen432609a2014-03-13 17:03:22 -07004409 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004410 if (shouldRunFirstRunActivity() &&
4411 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004412 Intent firstRunIntent = getFirstRunActivity();
4413 if (firstRunIntent != null) {
4414 startActivity(firstRunIntent);
4415 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004416 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004417 }
4418 }
Adam Cohen432609a2014-03-13 17:03:22 -07004419 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004420 }
4421
4422 private void markFirstRunActivityShown() {
4423 SharedPreferences.Editor editor = mSharedPrefs.edit();
4424 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4425 editor.apply();
4426 }
4427
Adam Cohen432609a2014-03-13 17:03:22 -07004428 /**
4429 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4430 * screen that must be displayed and dismissed.
4431 */
4432 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004433 if (mLauncherCallbacks != null) {
4434 return mLauncherCallbacks.hasDismissableIntroScreen();
4435 }
Adam Cohen432609a2014-03-13 17:03:22 -07004436 return false;
4437 }
4438
4439 /**
4440 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4441 */
4442 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004443 if (mLauncherCallbacks != null) {
4444 return mLauncherCallbacks.getIntroScreen();
4445 }
Adam Cohen432609a2014-03-13 17:03:22 -07004446 return null;
4447 }
4448
4449 /**
4450 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4451 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4452 */
4453 private boolean shouldShowIntroScreen() {
4454 return hasDismissableIntroScreen() &&
4455 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4456 }
4457
4458 protected void showIntroScreen() {
4459 View introScreen = getIntroScreen();
4460 changeWallpaperVisiblity(false);
4461 if (introScreen != null) {
4462 mDragLayer.showOverlayView(introScreen);
4463 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004464 if (mLauncherOverlayContainer != null) {
4465 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4466 }
Adam Cohen432609a2014-03-13 17:03:22 -07004467 }
4468
4469 public void dismissIntroScreen() {
4470 markIntroScreenDismissed();
4471 if (showFirstRunActivity()) {
4472 // We delay hiding the intro view until the first run activity is showing. This
4473 // avoids a blip.
4474 mWorkspace.postDelayed(new Runnable() {
4475 @Override
4476 public void run() {
4477 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004478 if (mLauncherOverlayContainer != null) {
4479 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4480 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004481 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004482 }
4483 }, ACTIVITY_START_DELAY);
4484 } else {
4485 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004486 if (mLauncherOverlayContainer != null) {
4487 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4488 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004489 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004490 }
4491 changeWallpaperVisiblity(true);
4492 }
4493
4494 private void markIntroScreenDismissed() {
4495 SharedPreferences.Editor editor = mSharedPrefs.edit();
4496 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4497 editor.apply();
4498 }
4499
Adam Cohen091440a2015-03-18 14:16:05 -07004500 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004501 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4502 // on the device, then we always show the first run cling experience (or if there is no
4503 // launcher2). Otherwise, we prompt the user upon started for migration
4504 LauncherClings launcherClings = new LauncherClings(this);
4505 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004506 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004507 if (mModel.canMigrateFromOldLauncherDb(this)) {
4508 launcherClings.showMigrationCling();
4509 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004510 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004511 }
4512 }
4513 }
4514
Winson Chunga6945242014-01-08 14:04:34 -08004515 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004516 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4517 if (mHotseat != null) mHotseat.setAlpha(1f);
4518 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004519 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4520 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004521 }
4522
4523 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004524 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4525 if (mHotseat != null) mHotseat.setAlpha(0f);
4526 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004527 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4528 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004529 }
4530
Winson Chung5ffd51d2015-06-25 11:36:50 -07004531 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004532 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004533 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004534 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004535 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004536 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004537 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4538 if (userHandle != null) {
4539 user = UserHandleCompat.fromUser(userHandle);
4540 }
4541 }
4542 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004543 }
4544
4545 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004546 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004547 if (user == null) {
4548 user = UserHandleCompat.myUserHandle();
4549 }
4550
4551 // Called from search suggestion, add the profile extra to the intent to ensure that we
4552 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004553 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004554 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004555 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004556 return null;
4557 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004558 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004559 }
4560
Winson Chung5ffd51d2015-06-25 11:36:50 -07004561 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004562 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4563 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004564 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004565 UserHandleCompat.myUserHandle());
4566 }
4567
Winson Chung5ffd51d2015-06-25 11:36:50 -07004568 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004569 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4570 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004571 mWorkspace.onExternalDragStartedWithItem(dragView);
4572 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 }
4574
Winson Chung5ffd51d2015-06-25 11:36:50 -07004575 protected void moveWorkspaceToDefaultScreen() {
4576 mWorkspace.moveToDefaultScreen(false);
4577 }
4578
Anjali Koppalf5d29132014-02-28 13:33:27 -08004579 @Override
4580 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004581 if (mLauncherCallbacks != null) {
4582 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4583 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004584 }
4585
Winson Chung80baf5a2010-08-09 16:03:15 -07004586 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004587 * Returns a FastBitmapDrawable with the icon, accurately sized.
4588 */
4589 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4590 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4591 d.setFilterBitmap(true);
4592 resizeIconDrawable(d);
4593 return d;
4594 }
4595
4596 /**
4597 * Resizes an icon drawable to the correct icon size.
4598 */
4599 public void resizeIconDrawable(Drawable icon) {
4600 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4601 }
4602
4603 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004604 * Prints out out state for debugging.
4605 */
4606 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004607 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004608 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004609 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4610 Log.d(TAG, "mRestoring=" + mRestoring);
4611 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004612 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004613 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004614 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004615
Daniel Sandler325dc232013-06-05 22:57:57 -04004616 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004617 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004618
4619 @Override
4620 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4621 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004622 synchronized (sDumpLogs) {
4623 writer.println(" ");
4624 writer.println("Debug logs: ");
4625 for (int i = 0; i < sDumpLogs.size(); i++) {
4626 writer.println(" " + sDumpLogs.get(i));
4627 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004628 }
Adam Cohen9211d422014-10-07 18:14:53 -07004629 if (mLauncherCallbacks != null) {
4630 mLauncherCallbacks.dump(prefix, fd, writer, args);
4631 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004632 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004633
4634 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004635 if (DEBUG_DUMP_LOG) {
4636 synchronized (sDumpLogs) {
4637 Log.d(TAG, "");
4638 Log.d(TAG, "*********************");
4639 Log.d(TAG, "Launcher debug logs: ");
4640 for (int i = 0; i < sDumpLogs.size(); i++) {
4641 Log.d(TAG, " " + sDumpLogs.get(i));
4642 }
4643 Log.d(TAG, "*********************");
4644 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004645 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004646 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004647 }
4648
4649 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004650 addDumpLog(tag, log, null, debugLog);
4651 }
4652
4653 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004654 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004655 if (e != null) {
4656 Log.d(tag, log, e);
4657 } else {
4658 Log.d(tag, log);
4659 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004660 }
Winson Chungede41292013-09-19 16:27:36 -07004661 if (DEBUG_DUMP_LOG) {
4662 sDateStamp.setTime(System.currentTimeMillis());
4663 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004664 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4665 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 }
Winson Chungede41292013-09-19 16:27:36 -07004667 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004668 }
4669
Adam Cohen59400422014-03-05 18:07:04 -08004670 public static CustomAppWidget getCustomAppWidget(String name) {
4671 return sCustomAppWidgets.get(name);
4672 }
4673
4674 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4675 return sCustomAppWidgets;
4676 }
4677
Winson Chungede41292013-09-19 16:27:36 -07004678 public void dumpLogsToLocalData() {
4679 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004680 new AsyncTask<Void, Void, Void>() {
4681 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004682 boolean success = false;
4683 sDateStamp.setTime(sRunStart);
4684 String FILENAME = sDateStamp.getMonth() + "-"
4685 + sDateStamp.getDay() + "_"
4686 + sDateStamp.getHours() + "-"
4687 + sDateStamp.getMinutes() + "_"
4688 + sDateStamp.getSeconds() + ".txt";
4689
4690 FileOutputStream fos = null;
4691 File outFile = null;
4692 try {
4693 outFile = new File(getFilesDir(), FILENAME);
4694 outFile.createNewFile();
4695 fos = new FileOutputStream(outFile);
4696 } catch (Exception e) {
4697 e.printStackTrace();
4698 }
4699 if (fos != null) {
4700 PrintWriter writer = new PrintWriter(fos);
4701
4702 writer.println(" ");
4703 writer.println("Debug logs: ");
4704 synchronized (sDumpLogs) {
4705 for (int i = 0; i < sDumpLogs.size(); i++) {
4706 writer.println(" " + sDumpLogs.get(i));
4707 }
4708 }
4709 writer.close();
4710 }
4711 try {
4712 if (fos != null) {
4713 fos.close();
4714 success = true;
4715 }
4716 } catch (IOException e) {
4717 e.printStackTrace();
4718 }
Michael Jurka43467462013-11-14 12:03:58 +01004719 return null;
Winson Chungede41292013-09-19 16:27:36 -07004720 }
Michael Jurka43467462013-11-14 12:03:58 +01004721 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004722 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004723 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004724}
Michael Jurka2763be32011-02-24 11:19:57 -08004725
Dan Sandlerd5024042014-01-09 15:01:33 -05004726interface DebugIntents {
4727 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4728 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004729}