blob: 750d211bfc3da578e1e0277d5541a86e73743d71 [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;
Winsonc0880492015-08-21 11:16:27 -070049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070098import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700104import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700105import com.android.launcher3.dragndrop.DragController;
106import com.android.launcher3.dragndrop.DragLayer;
107import com.android.launcher3.dragndrop.DragView;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700109import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700110import com.android.launcher3.util.LongArrayMap;
Sunny Goyal322d5562015-06-25 19:35:49 -0700111import com.android.launcher3.util.TestingUtils;
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;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700141 static final boolean DEBUG_WIDGETS = false;
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;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700155 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
156 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
157 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
158
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700159 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
160
Mathew Inwood876a8462013-06-14 14:12:41 +0100161 /**
162 * IntentStarter uses request codes starting with this. This must be greater than all activity
163 * request codes used internally.
164 */
165 protected static final int REQUEST_LAST = 100;
166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700179 // Type: Content Values / parcelable
180 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: parcelable
182 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000183 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Adam Cohen432609a2014-03-13 17:03:22 -0700186 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800187 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
188
Adam Cohen3ed316a2014-07-23 18:21:20 -0700189 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
190 static final String ACTION_FIRST_LOAD_COMPLETE =
191 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
192
Sunny Goyal594d76d2014-11-06 10:12:54 -0800193 private static final String QSB_WIDGET_ID = "qsb_widget_id";
194 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
195
Winson Chunga6945242014-01-08 14:04:34 -0800196 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
197
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198 /** The different states that Launcher can be in. */
Tony Wickham94e0d372015-09-11 12:17:48 -0700199 enum State { NONE, WORKSPACE, WORKSPACE_SPRING_LOADED, APPS, APPS_SPRING_LOADED,
200 WIDGETS, WIDGETS_SPRING_LOADED }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700201
Adam Cohen091440a2015-03-18 14:16:05 -0700202 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700203 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700205 private boolean mIsSafeModeEnabled;
206
Adam Cohenc2d6e892014-10-16 09:49:24 -0700207 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
208 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700209 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700214 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
Winson Chunga2413752012-04-03 14:22:34 -0700216 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700217 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
218 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700220
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800221 private final BroadcastReceiver mCloseSystemDialogsReceiver
222 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800226 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragController mDragController;
Sunny Goyal322d5562015-06-25 19:35:49 -0700229
230 public View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Sunny Goyalffe83f12014-08-14 17:39:34 -0700232 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700235 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800236 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800237 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
Sunny Goyal316490e2015-06-02 09:38:28 -0700241 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800242 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700243
Michael Jurka838a4ca2011-02-07 13:33:06 -0800244 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700245 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246
Winson Chungc51db6a2011-10-05 11:44:49 -0700247 private SearchDropTargetBar mSearchDropTargetBar;
Tony Wickham34d2c912015-09-11 09:27:58 -0700248 private AppInfoDropTargetBar mAppInfoDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700249
250 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700251 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700252
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700253 // Main container view and the model for the widget tray screen.
254 @Thunk WidgetsContainerView mWidgetsView;
255 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700256
Sunny Goyal594d76d2014-11-06 10:12:54 -0800257 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
261 // scroll issues (because the workspace may not have been measured yet) and extra work.
262 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
263 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
265 private SpannableStringBuilder mDefaultKeySsb = null;
266
Adam Cohen091440a2015-03-18 14:16:05 -0700267 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800269 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private boolean mRestoring;
271 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700272 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
Michael Jurka1e2f4652013-07-08 18:03:46 -0700274 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700275 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
Sunny Goyal527c7d32015-08-28 15:19:36 -0700276 private ViewOnDrawExecutor mPendingExecutor;
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700280 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800281 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700282 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
Sunny Goyal639e9062015-08-19 19:17:06 -0700285 private LauncherClings mClings;
286
Sunny Goyale2df0622015-04-24 11:27:00 -0700287 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Adam Cohen61f560d2013-09-30 15:58:20 -0700289 private View.OnTouchListener mHapticFeedbackTouchListener;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700293 private static final int ADVANCE_INTERVAL = 20000;
294 private static final int ADVANCE_STAGGER = 250;
295
296 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 private long mAutoAdvanceSentTime;
298 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700299 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700303 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700308 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700309
Sunny Goyala1365452015-10-01 15:46:24 -0700310 private static final ArrayList<String> sDumpLogs = new ArrayList<String>();
311 private static final Date sDateStamp = new Date();
312 private static final DateFormat sDateFormat =
Adam Cohen4caf2982013-08-20 18:54:31 -0700313 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700314
Winson Chung46353de2012-02-16 14:05:10 -0800315 // We only want to get the SharedPreferences once since it does an FS stat each time we get
316 // it from the context.
317 private SharedPreferences mSharedPrefs;
318
Winson Chungf0c6ae02012-03-21 16:10:31 -0700319 // Holds the page that we need to animate to, and the icon views that we need to animate up
320 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700322 private Bitmap mFolderIconBitmap;
323 private Canvas mFolderIconCanvas;
324 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700326 private DeviceProfile mDeviceProfile;
327
Adam Cohen4b66bf32015-09-18 12:15:19 -0700328 private boolean mMoveToDefaultScreenFromNewIntent;
329
Winson Chung13eb5272015-05-11 16:30:13 -0700330 // This is set to the view that launched the activity that navigated the user away from
331 // launcher. Since there is no callback for when the activity has finished launching, enable
332 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Adam Cohen59400422014-03-05 18:07:04 -0800335 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
336 new HashMap<String, CustomAppWidget>();
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 static {
Sunny Goyal322d5562015-06-25 19:35:49 -0700339 if (TestingUtils.ENABLE_CUSTOM_WIDGET_TEST) {
340 TestingUtils.addDummyWidget(sCustomAppWidgets);
Adam Cohen59400422014-03-05 18:07:04 -0800341 }
342 }
343
Adam Cohen091440a2015-03-18 14:16:05 -0700344 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700345 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700346 if (mWorkspace != null) {
347 mWorkspace.buildPageHardwareLayers();
348 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 }
350 };
351
Adam Cohendb364c32014-05-20 14:23:40 -0700352 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353
Adam Cohen091440a2015-03-18 14:16:05 -0700354 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 int requestCode;
356 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700357 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700358 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int cellX;
360 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700361 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800362 }
363
Daniel Sandlerff02d492013-08-05 02:12:05 -0400364 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700365 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700366 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400367
368 @Thunk void setOrientation() {
369 if (mRotationEnabled) {
370 unlockScreenOrientation(true);
371 } else {
372 setRequestedOrientation(
373 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700374 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400375 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700376
Sunny Goyal7779d622015-06-11 16:18:39 -0700377 private Runnable mUpdateOrientationRunnable = new Runnable() {
378 public void run() {
379 setOrientation();
380 }
381 };
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
399
Adam Cohen9211d422014-10-07 18:14:53 -0700400 if (mLauncherCallbacks != null) {
401 mLauncherCallbacks.preOnCreate();
402 }
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400405
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400407 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700408
Adam Cohen2e6da152015-05-06 11:42:25 -0700409 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700410 mDeviceProfile = getResources().getConfiguration().orientation
411 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700412 app.getInvariantDeviceProfile().landscapeProfile
413 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700414
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400415 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700416 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700417 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700420
Joe Onorato41a12d22009-10-31 18:30:00 -0400421 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700422 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700423
Daniel Sandlerff02d492013-08-05 02:12:05 -0400424 mStats = new Stats(this);
425
Sunny Goyalffe83f12014-08-14 17:39:34 -0700426 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
429 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
432 // this also ensures that any synchronous binding below doesn't re-trigger another
433 // LauncherModel load.
434 mPaused = false;
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200437 android.os.Debug.startMethodTracing(
438 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700444 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
Joe Onorato7c312c12009-08-13 21:36:53 -0700446 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 mSavedState = savedInstanceState;
449 restoreState(mSavedState);
450
451 if (PROFILE_STARTUP) {
452 android.os.Debug.stopMethodTracing();
453 }
454
455 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700456 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 // If the user leaves launcher, then we should just load items asynchronously when
458 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700459 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 } else {
461 // We only load the page synchronously if the user rotates (or triggers a
462 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466
467 // For handling default keys
468 mDefaultKeySsb = new SpannableStringBuilder();
469 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800470
471 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
472 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800473
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700474 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
475 // In case we are on a device with locked rotation, we should look at preferences to check
476 // if the user has specifically allowed rotation.
477 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700478 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700479 }
480
481 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
482 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400483 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 if (mLauncherCallbacks != null) {
486 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700487 if (mLauncherCallbacks.hasLauncherOverlay()) {
488 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700489 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
490 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
491 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 mWorkspace.setLauncherOverlay(mLauncherOverlay);
493 }
Adam Cohen9211d422014-10-07 18:14:53 -0700494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
Sunny Goyal7779d622015-06-11 16:18:39 -0700504 @Override
505 public void onSettingsChanged(String settings, boolean value) {
506 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
507 mRotationEnabled = value;
508 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
509 mUpdateOrientationRunnable.run();
510 }
511 }
512 }
513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
523 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
524 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700525 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700526 private boolean mWorkspaceImportanceStored = false;
527 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700528 private int mWorkspaceImportanceForAccessibility =
529 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
530 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
531
532 @Override
533 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700534 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700535 return;
536 }
537 // The underlying workspace and hotseat are temporarily suppressed by the search
538 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700539 if (mWorkspace != null) {
540 mWorkspaceImportanceForAccessibility =
541 mWorkspace.getImportantForAccessibility();
542 mWorkspace.setImportantForAccessibility(
543 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
544 mWorkspaceImportanceStored = true;
545 }
546 if (mHotseat != null) {
547 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
548 mHotseat.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mHotseatImportanceStored = true;
551 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700552 }
553
554 @Override
555 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700556 if (mWorkspaceImportanceStored && mWorkspace != null) {
557 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
558 }
559 if (mHotseatImportanceStored && mHotseat != null) {
560 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
561 }
562 mWorkspaceImportanceStored = false;
563 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700564 }
565 });
Adam Cohen9211d422014-10-07 18:14:53 -0700566 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700567 }
568
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700569 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700570 public void onLauncherProviderChange() {
571 if (mLauncherCallbacks != null) {
572 mLauncherCallbacks.onLauncherProviderChange();
573 }
574 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575
Winson Chungef7f8742015-06-04 17:18:17 -0700576 /**
577 * Updates the bounds of all the overlays to match the new fixed bounds.
578 */
579 public void updateOverlayBounds(Rect newBounds) {
580 mAppsView.setSearchBarBounds(newBounds);
581 mWidgetsView.setSearchBarBounds(newBounds);
582 }
583
Adam Cohen9211d422014-10-07 18:14:53 -0700584 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700585 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700586 if (mLauncherCallbacks != null) {
587 return mLauncherCallbacks.hasCustomContentToLeft();
588 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 return false;
590 }
591
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500593 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600594 * {@link #addToCustomContentPage}. This will only be invoked if
595 * {@link #hasCustomContentToLeft()} is {@code true}.
596 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700598 if (mLauncherCallbacks != null) {
599 mLauncherCallbacks.populateCustomContentContainer();
600 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600601 }
602
603 /**
604 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
605 * ensure the custom content page is added or removed if necessary.
606 */
607 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600608 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 // Not bound yet, wait for bindScreens to be called.
610 return;
611 }
612
613 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
614 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500615 mWorkspace.createCustomContentContainer();
616 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
618 mWorkspace.removeCustomContentPage();
619 }
620 }
621
Anton Hanssona2f665f2013-09-26 12:18:32 +0100622 public Stats getStats() {
623 return mStats;
624 }
625
Hyunyoung Song3f471442015-04-08 19:01:34 -0700626 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700627 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
628 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700629 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700630 }
631
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000632 public int getViewIdForItem(ItemInfo info) {
Sunny Goyale2fd14b2015-08-27 17:45:46 -0700633 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
634 // This cast is safe as long as the id < 0x00FFFFFF
635 // Since we jail all the dynamically generated views, there should be no clashes
636 // with any other views.
637 return (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000638 }
639
640 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700641 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
642 * a configuration step, this allows the proper animations to run after other transitions.
643 */
Adam Cohendb364c32014-05-20 14:23:40 -0700644 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700645 long screenId = args.screenId;
646 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
647 // When the screen id represents an actual screen (as opposed to a rank) we make sure
648 // that the drop page actually exists.
649 screenId = ensurePendingDropLayoutExists(args.screenId);
650 }
Adam Cohendb364c32014-05-20 14:23:40 -0700651
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800652 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700654 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700656 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700658 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700659 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700660 case REQUEST_RECONFIGURE_APPWIDGET:
661 completeRestoreAppWidget(args.appWidgetId);
662 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 }
Michael Jurka27614d22012-04-02 06:40:22 -0700664 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
665 // if you turned the screen off and then back while in All Apps, Launcher would not
666 // return to the workspace. Clearing mAddInfo.container here fixes this issue
667 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700668 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 }
670
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800671 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700672 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700673 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700674 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700675 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800676 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700677
Adam Cohenad4e15c2013-10-17 16:21:35 -0700678 Runnable exitSpringLoaded = new Runnable() {
679 @Override
680 public void run() {
681 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
682 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
683 }
684 };
685
Michael Jurka8b805b12012-04-18 14:23:14 -0700686 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700687 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700688 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700689 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
690 if (resultCode == RESULT_CANCELED) {
691 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700692 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700693 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700694 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800695 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700696 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700697
698 // When the user has granted permission to bind widgets, we should check to see if
699 // we can inflate the default search bar widget.
700 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700701 }
702 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200703 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
704 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickhame3054412015-09-09 09:05:25 -0700705 // User could have free-scrolled between pages before picking a wallpaper; make sure
706 // we move to the closest one now.
707 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700708 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200709 }
710 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200712
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700714 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700715
Adam Cohendb364c32014-05-20 14:23:40 -0700716 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 // We have special handling for widgets
718 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800719 final int appWidgetId;
720 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
721 : -1;
722 if (widgetId < 0) {
723 appWidgetId = pendingAddWidgetId;
724 } else {
725 appWidgetId = widgetId;
726 }
727
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800729 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700730 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
731 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 result = RESULT_CANCELED;
733 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700734 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700735 @Override
736 public void run() {
737 exitSpringLoadedDragModeDelayed(false, 0, null);
738 }
739 };
Adam Cohendb364c32014-05-20 14:23:40 -0700740 if (workspaceLocked) {
741 // No need to remove the empty screen if we're mid-binding, as the
742 // the bind will not add the empty screen.
743 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
744 } else {
745 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
746 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
747 }
Winson Chung5aaab772012-04-27 15:24:02 -0700748 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700749 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700750 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
751 // When the screen id represents an actual screen (as opposed to a rank)
752 // we make sure that the drop page actually exists.
753 mPendingAddInfo.screenId =
754 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
755 }
Adam Cohendb364c32014-05-20 14:23:40 -0700756 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
757
758 dropLayout.setDropPending(true);
759 final Runnable onComplete = new Runnable() {
760 @Override
761 public void run() {
762 completeTwoStageWidgetDrop(resultCode, appWidgetId);
763 dropLayout.setDropPending(false);
764 }
765 };
766 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
767 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
768 } else {
769 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
770 mPendingAddInfo);
771 sPendingAddItem = args;
772 }
Winson Chung5aaab772012-04-27 15:24:02 -0700773 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 return;
775 }
776
Sunny Goyalff572272014-07-23 13:58:07 -0700777 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
778 if (resultCode == RESULT_OK) {
779 // Update the widget view.
780 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
781 pendingAddWidgetId, mPendingAddInfo);
782 if (workspaceLocked) {
783 sPendingAddItem = args;
784 } else {
785 completeAdd(args);
786 }
787 }
788 // Leave the widget in the pending state if the user canceled the configure.
789 return;
790 }
791
Sunny Goyalaad90582015-07-09 14:22:50 -0700792 if (requestCode == REQUEST_CREATE_SHORTCUT) {
793 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
794 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
795 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
796 mPendingAddInfo);
797 if (isWorkspaceLocked()) {
798 sPendingAddItem = args;
799 } else {
800 completeAdd(args);
801 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
802 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
803 }
804 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700805 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
806 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800810
811 }
812
813 @Override
814 protected void onActivityResult(
815 final int requestCode, final int resultCode, final Intent data) {
816 handleActivityResult(requestCode, resultCode, data);
817 if (mLauncherCallbacks != null) {
818 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
819 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 }
821
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600822 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700823 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600824 int[] grantResults) {
825 if (mLauncherCallbacks != null) {
826 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
827 grantResults);
828 }
829 }
830
Adam Cohendb364c32014-05-20 14:23:40 -0700831 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
832 appWidgetId, ItemInfo info) {
833 PendingAddArguments args = new PendingAddArguments();
834 args.requestCode = requestCode;
835 args.intent = data;
836 args.container = info.container;
837 args.screenId = info.screenId;
838 args.cellX = info.cellX;
839 args.cellY = info.cellY;
840 args.appWidgetId = appWidgetId;
841 return args;
842 }
843
844 /**
845 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
846 *
847 * @param screenId the screen id to check
848 * @return the new screen, or screenId if it exists
849 */
850 private long ensurePendingDropLayoutExists(long screenId) {
851 CellLayout dropLayout =
852 (CellLayout) mWorkspace.getScreenWithId(screenId);
853 if (dropLayout == null) {
854 // it's possible that the add screen was removed because it was
855 // empty and a re-bind occurred
856 mWorkspace.addExtraEmptyScreen();
857 return mWorkspace.commitExtraEmptyScreen();
858 } else {
859 return screenId;
860 }
861 }
862
Adam Cohen091440a2015-03-18 14:16:05 -0700863 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700864 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 Runnable onCompleteRunnable = null;
867 int animationType = 0;
868
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700869 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 if (resultCode == RESULT_OK) {
871 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
872 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700873 mPendingAddWidgetInfo);
874 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 onCompleteRunnable = new Runnable() {
876 @Override
877 public void run() {
878 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700880 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
881 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 }
883 };
884 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800885 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700888 if (mDragLayer.getAnimatedView() != null) {
889 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
890 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
891 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700892 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700893 // The animated view may be null in the case of a rotation during widget configuration
894 onCompleteRunnable.run();
895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700899 protected void onStop() {
900 super.onStop();
901 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700902
903 if (mLauncherCallbacks != null) {
904 mLauncherCallbacks.onStop();
905 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700906 }
907
908 @Override
909 protected void onStart() {
910 super.onStart();
911 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700912
913 if (mLauncherCallbacks != null) {
914 mLauncherCallbacks.onStart();
915 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700916 }
917
918 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200920 long startTime = 0;
921 if (DEBUG_RESUME_TIME) {
922 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400923 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200924 }
Adam Cohen9211d422014-10-07 18:14:53 -0700925
926 if (mLauncherCallbacks != null) {
927 mLauncherCallbacks.preOnResume();
928 }
929
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700931
Winson Chung4a2afa32012-07-19 14:53:05 -0700932 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700933 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700934 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800935 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700936 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700937 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700938 // view after launching an app, as they may be depending on the UI to be static to
939 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700940 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700941 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800942 } else if (mOnResumeState == State.WIDGETS) {
943 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700944 }
945 mOnResumeState = State.NONE;
946
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700947 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700948 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
949 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700950
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800951 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700952 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700953 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700954 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700956 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700958 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200959 // We might have postponed some bind calls until onResume (see waitUntilResume) --
960 // execute them here
961 long startTimeCallbacks = 0;
962 if (DEBUG_RESUME_TIME) {
963 startTimeCallbacks = System.currentTimeMillis();
964 }
965
Michael Jurka1e2f4652013-07-08 18:03:46 -0700966 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
967 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200968 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700969 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200970 if (DEBUG_RESUME_TIME) {
971 Log.d(TAG, "Time spent processing callbacks in onResume: " +
972 (System.currentTimeMillis() - startTimeCallbacks));
973 }
Michael Jurka447bf842013-05-15 14:52:15 +0200974 }
Michael Jurka54554252013-08-01 12:52:23 +0200975 if (mOnResumeCallbacks.size() > 0) {
976 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
977 mOnResumeCallbacks.get(i).run();
978 }
979 mOnResumeCallbacks.clear();
980 }
Winson Chunge4e50662012-01-23 14:45:13 -0800981
982 // Reset the pressed state of icons that were locked in the press state while activities
983 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800984 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800985 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800986 mWaitingForResume.setStayPressed(false);
987 }
Winson Chung82963d52013-10-09 11:20:57 -0700988
Adam Cohen06dff352012-06-01 17:17:08 -0700989 // It is possible that widgets can receive updates while launcher is not in the foreground.
990 // Consequently, the widgets will be inflated in the orientation of the foreground activity
991 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
992 // orientation.
Sunny Goyal6bec75f2015-10-01 18:50:48 -0700993 if (!isWorkspaceLoading()) {
994 getWorkspace().reinflateWidgetsIfNecessary();
995 }
Sunny Goyal22235bc2015-04-03 09:23:43 -0700996 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700997
Michael Jurka447bf842013-05-15 14:52:15 +0200998 if (DEBUG_RESUME_TIME) {
999 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1000 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001001
Adam Cohen4b66bf32015-09-18 12:15:19 -07001002 // We want to suppress callbacks about CustomContent being shown if we have just received
1003 // onNewIntent while the user was present within launcher. In that case, we post a call
1004 // to move the user to the main screen (which will occur after onResume). We don't want to
1005 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1006 // suppress here.
1007 if (mWorkspace.getCustomContentCallbacks() != null
1008 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001009 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001010 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001011 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1012 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001013 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001014 }
1015 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001016 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001017 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001018 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001019
Sunny Goyal756adbc2015-04-16 15:20:51 -07001020 if (!isWorkspaceLoading()) {
1021 // Process any items that were added while Launcher was away.
1022 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1023 }
Adam Cohen9211d422014-10-07 18:14:53 -07001024
1025 if (mLauncherCallbacks != null) {
1026 mLauncherCallbacks.onResume();
1027 }
Adam Cohen06dff352012-06-01 17:17:08 -07001028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001032 // Ensure that items added to Launcher are queued until Launcher returns
1033 InstallShortcutReceiver.enableInstallQueue();
1034
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001036 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001037 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001038 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001039
1040 // We call onHide() aggressively. The custom content callbacks should be able to
1041 // debounce excess onHide calls.
1042 if (mWorkspace.getCustomContentCallbacks() != null) {
1043 mWorkspace.getCustomContentCallbacks().onHide();
1044 }
Romain Guycbb89e42009-06-08 15:52:54 -07001045
Adam Cohen9211d422014-10-07 18:14:53 -07001046 if (mLauncherCallbacks != null) {
1047 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001049 }
1050
1051 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1053 // by a onResume or by scrolling otherwise.
1054 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001055
1056 // Custom content is completely hidden
1057 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001058
1059 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1060 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001061
1062 // Indicates whether the user is allowed to scroll away from the custom content.
1063 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001064 }
1065
Adam Cohenc2d6e892014-10-16 09:49:24 -07001066 public interface LauncherOverlay {
1067
1068 /**
1069 * Touch interaction leading to overscroll has begun
1070 */
1071 public void onScrollInteractionBegin();
1072
1073 /**
1074 * Touch interaction related to overscroll has ended
1075 */
1076 public void onScrollInteractionEnd();
1077
1078 /**
1079 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1080 * screen (or in the case of RTL, the rightmost screen).
1081 */
1082 public void onScrollChange(int progress, boolean rtl);
1083
1084 /**
1085 * Screen has stopped scrolling
1086 */
1087 public void onScrollSettled();
1088
1089 /**
1090 * This method can be called by the Launcher in order to force the LauncherOverlay
1091 * to exit fully immersive mode.
1092 */
1093 public void forceExitFullImmersion();
1094 }
1095
Jun Mukai8af0cd82015-05-12 12:32:12 -07001096 public interface LauncherSearchCallbacks {
1097 /**
1098 * Called when the search overlay is shown.
1099 */
1100 public void onSearchOverlayOpened();
1101
1102 /**
1103 * Called when the search overlay is dismissed.
1104 */
1105 public void onSearchOverlayClosed();
1106 }
1107
Adam Cohenc2d6e892014-10-16 09:49:24 -07001108 public interface LauncherOverlayCallbacks {
1109 /**
1110 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1111 * however it doesn't modify any state within the launcher.
1112 */
1113 public boolean canEnterFullImmersion();
1114
1115 /**
1116 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1117 * eg. by occupying the full screen and handling all touch events.
1118 *
1119 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1120 * case, Launcher will modify any necessary state and assumes the overlay is
1121 * handling all interaction. If false, the LauncherOverlay should cancel any
1122 *
1123 */
1124 public boolean enterFullImmersion();
1125
1126 /**
1127 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1128 * full control over UI and state.
1129 */
1130 public void exitFullImmersion();
1131 }
1132
1133 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1134
1135 @Override
1136 public boolean canEnterFullImmersion() {
1137 return mState == State.WORKSPACE;
1138 }
1139
1140 @Override
1141 public boolean enterFullImmersion() {
1142 if (mState == State.WORKSPACE) {
1143 // When fully immersed, disregard any touches which fall through.
1144 mDragLayer.setBlockTouch(true);
1145 return true;
1146 }
1147 return false;
1148 }
1149
1150 @Override
1151 public void exitFullImmersion() {
1152 mDragLayer.setBlockTouch(false);
1153 }
1154 }
1155
Jorim Jaggid017f882014-01-14 17:08:48 -08001156 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001157 if (mLauncherCallbacks != null) {
1158 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001159 } else {
1160 // On devices with a locked orientation, we will at least have the allow rotation
1161 // setting.
1162 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001163 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001164 }
1165
Adam Cohen9211d422014-10-07 18:14:53 -07001166 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001167 CustomContentCallbacks callbacks, String description) {
1168 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001169 }
1170
Adam Cohenedb40762013-07-18 16:45:45 -07001171 // The custom content needs to offset its content to account for the QSB
1172 public int getTopOffsetForCustomContent() {
1173 return mWorkspace.getPaddingTop();
1174 }
1175
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001176 @Override
1177 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001179 if (mModel.isCurrentCallbacks(this)) {
1180 mModel.stopLoader();
1181 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001182 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1183
Romain Guy13c2e7b2010-03-10 19:45:00 -08001184 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001185 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001188 @Override
1189 public void onWindowFocusChanged(boolean hasFocus) {
1190 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001191 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001192
1193 if (mLauncherCallbacks != null) {
1194 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1195 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001196 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 private boolean acceptFilter() {
1199 final InputMethodManager inputManager = (InputMethodManager)
1200 getSystemService(Context.INPUT_METHOD_SERVICE);
1201 return !inputManager.isFullscreenMode();
1202 }
1203
1204 @Override
1205 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001206 final int uniChar = event.getUnicodeChar();
1207 final boolean handled = super.onKeyDown(keyCode, event);
1208 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1209 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1211 keyCode, event);
1212 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001213 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001214 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001215 // showSearchDialog()
1216 // If there are multiple keystrokes before the search dialog takes focus,
1217 // onSearchRequested() will be called for every keystroke,
1218 // but it is idempotent, so it's fine.
1219 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221 }
1222
Joe Onorato8a9625e2010-01-28 15:55:35 -08001223 // Eat the long press event so the keyboard doesn't come up.
1224 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1225 return true;
1226 }
1227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 return handled;
1229 }
1230
Winson46163472015-09-22 17:31:56 -07001231 @Override
1232 public boolean onKeyUp(int keyCode, KeyEvent event) {
1233 if (keyCode == KeyEvent.KEYCODE_MENU) {
1234 // Ignore the menu key if we are currently dragging or are on the custom content screen
1235 if (!isOnCustomContent() && !mDragController.isDragging()) {
1236 // Close any open folders
1237 closeFolder();
1238
1239 // Stop resizing any widgets
1240 mWorkspace.exitWidgetResizeMode();
1241
1242 // Show the overview mode if we are on the workspace
1243 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1244 !mWorkspace.isSwitchingState()) {
1245 mOverviewPanel.requestFocus();
1246 showOverviewMode(true);
1247 }
1248 }
1249 return true;
1250 }
1251 return super.onKeyUp(keyCode, event);
1252 }
1253
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 private String getTypedText() {
1255 return mDefaultKeySsb.toString();
1256 }
1257
1258 private void clearTypedText() {
1259 mDefaultKeySsb.clear();
1260 mDefaultKeySsb.clearSpans();
1261 Selection.setSelection(mDefaultKeySsb, 0);
1262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001265 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1266 * State
1267 */
1268 private static State intToState(int stateOrdinal) {
1269 State state = State.WORKSPACE;
1270 final State[] stateValues = State.values();
1271 for (int i = 0; i < stateValues.length; i++) {
1272 if (stateValues[i].ordinal() == stateOrdinal) {
1273 state = stateValues[i];
1274 break;
1275 }
1276 }
1277 return state;
1278 }
1279
1280 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 * Restores the previous state, if it exists.
1282 *
1283 * @param savedState The previous state.
1284 */
1285 private void restoreState(Bundle savedState) {
1286 if (savedState == null) {
1287 return;
1288 }
1289
Michael Jurka883f55b2010-10-21 15:47:14 -07001290 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001291 if (state == State.APPS || state == State.WIDGETS) {
1292 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001293 }
1294
Adam Cohen21cd0022013-10-09 18:57:02 -07001295 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1296 PagedView.INVALID_RESTORE_PAGE);
1297 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001298 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
Sunny Goyal756cd262015-08-20 12:33:21 -07001301 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1302 if (itemValues != null) {
1303 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001304 AppWidgetProviderInfo info = savedState.getParcelable(
1305 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001306 mPendingAddWidgetInfo = info == null ?
1307 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1308
Adam Cohen4637b5a2013-11-04 18:21:24 -08001309 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001310 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 mRestoring = true;
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
1315 /**
1316 * Finds all the views we need and configure them properly.
1317 */
1318 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001319 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001320
Craig Mautner360310b2012-10-26 15:13:08 -07001321 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001322 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001323 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1324 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001325 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001326 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001327
John Spurlock77e1f472013-09-11 10:09:51 -04001328 mLauncherView.setSystemUiVisibility(
1329 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001330 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331
Winson Chung4c98d922011-05-31 16:50:48 -07001332 // Setup the drag layer
1333 mDragLayer.setup(this, dragController);
1334
Winson Chung3d503fb2011-07-13 17:25:49 -07001335 // Setup the hotseat
1336 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1337 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001338 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 }
1340
Jorim Jaggid017f882014-01-14 17:08:48 -08001341 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Tony Wickhamc75917f2015-09-02 16:11:09 -07001342 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1343 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1344 @Override
1345 public boolean onLongClick(View v) {
1346 return v.performClick();
1347 }
1348 };
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001349 mWidgetsButton = findViewById(R.id.widget_button);
1350 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001351 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001352 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001353 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001354 onClickAddWidgetButton(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001355 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001356 }
1357 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001358 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001359 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001360
1361 View wallpaperButton = findViewById(R.id.wallpaper_button);
1362 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001364 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001365 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001366 onClickWallpaperPicker(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001367 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001368 }
1369 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001370 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
Adam Cohen61f560d2013-09-30 15:58:20 -07001371 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1372
1373 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001374 if (hasSettings()) {
1375 settingsButton.setOnClickListener(new OnClickListener() {
1376 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001377 public void onClick(View view) {
Jorim Jaggid017f882014-01-14 17:08:48 -08001378 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001379 onClickSettingsButton(view);
Jorim Jaggid017f882014-01-14 17:08:48 -08001380 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001381 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001382 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001383 settingsButton.setOnLongClickListener(performClickOnLongClick);
Jorim Jaggid017f882014-01-14 17:08:48 -08001384 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1385 } else {
1386 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001387 }
1388
Adam Cohendbdff6b2013-09-18 19:09:15 -07001389 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390
Winson Chung4c98d922011-05-31 16:50:48 -07001391 // Setup the workspace
1392 mWorkspace.setHapticFeedbackEnabled(false);
1393 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001394 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001395 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001396
Tony Wickham34d2c912015-09-11 09:27:58 -07001397 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001398 mSearchDropTargetBar = (SearchDropTargetBar)
1399 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001400 // Get the app info bar
1401 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1402 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001403
Winson Chungef7f8742015-06-04 17:18:17 -07001404 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001405 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001406 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001407 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1408 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1409 } else {
1410 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1411 }
Craig Mautner360310b2012-10-26 15:13:08 -07001412
Winson Chung3d503fb2011-07-13 17:25:49 -07001413 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001414 dragController.setDragScoller(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001415 dragController.setMoveTarget(mWorkspace);
1416 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001417 if (mSearchDropTargetBar != null) {
1418 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001419 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001420 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001421 if (mAppInfoDropTargetBar != null) {
1422 mAppInfoDropTargetBar.setup(this, dragController);
1423 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001424
Sunny Goyal322d5562015-06-25 19:35:49 -07001425 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1426 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
1430 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001431 * Sets the all apps button. This method is called from {@link Hotseat}.
1432 */
1433 public void setAllAppsButton(View allAppsButton) {
1434 mAllAppsButton = allAppsButton;
1435 }
1436
1437 public View getAllAppsButton() {
1438 return mAllAppsButton;
1439 }
1440
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001441 public View getWidgetsButton() {
1442 return mWidgetsButton;
1443 }
1444
Anjali Koppal5ad44842014-03-10 20:34:39 -07001445 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 * Creates a view representing a shortcut.
1447 *
1448 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001450 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001451 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
1454 /**
1455 * Creates a view representing a shortcut inflated from the specified resource.
1456 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 * @param parent The group the shortcut belongs to.
1458 * @param info The data structure describing the shortcut.
1459 *
1460 * @return A View inflated from layoutResId.
1461 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001462 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001463 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001464 parent, false);
1465 favorite.applyFromShortcutInfo(info, mIconCache);
1466 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001468 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 return favorite;
1470 }
1471
1472 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 * Add a shortcut to the workspace.
1474 *
1475 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 int cellY) {
1479 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001480 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Sunny Goyal5c97f512015-05-19 16:03:28 -07001482 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001483 if (info == null) {
1484 return;
1485 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001486 final View view = createShortcut(info);
1487
Sunny Goyal5c97f512015-05-19 16:03:28 -07001488 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001489 // First we check if we already know the exact location where we want to add this item.
1490 if (cellX >= 0 && cellY >= 0) {
1491 cellXY[0] = cellX;
1492 cellXY[1] = cellY;
1493 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001494
1495 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001496 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001497 true, null,null)) {
1498 return;
1499 }
1500 DragObject dragObject = new DragObject();
1501 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001502 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1503 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001504 return;
1505 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001506 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001508 }
1509
1510 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001511 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 return;
1513 }
1514
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001515 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
1517 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521 }
1522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001526 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 */
Adam Cohen091440a2015-03-18 14:16:05 -07001528 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001529 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1530
1531 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001533 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1534 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Adam Cohen59400422014-03-05 18:07:04 -08001537 if (appWidgetInfo.isCustomWidget) {
1538 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001539 }
1540
Adam Cohen59400422014-03-05 18:07:04 -08001541 LauncherAppWidgetInfo launcherInfo;
1542 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1543 launcherInfo.spanX = info.spanX;
1544 launcherInfo.spanY = info.spanY;
1545 launcherInfo.minSpanX = info.minSpanX;
1546 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001547 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001550 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 if (hostView == null) {
1554 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001555 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1556 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001557 } else {
1558 // The AppWidgetHostView has already been inflated and instantiated
1559 launcherInfo.hostView = hostView;
1560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001562 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001563 launcherInfo.notifyWidgetSizeChanged(this);
1564
Adam Cohen59400422014-03-05 18:07:04 -08001565 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1566 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001567
1568 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001570 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Adam Cohended9f8d2010-11-03 13:25:16 -07001573 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1574 @Override
1575 public void onReceive(Context context, Intent intent) {
1576 final String action = intent.getAction();
1577 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1578 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001579 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001580 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001581
Winson Chungc100e8e2011-08-09 16:02:43 -07001582 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001583 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001584 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001585 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001586 if (!showWorkspace(false)) {
1587 // If we are already on the workspace, then manually reset all apps
1588 mAppsView.reset();
1589 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1592 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001593 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001594 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1595 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001596 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001597 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1598 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1599 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001600 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001601 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1602 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 }
1604 }
1605 };
1606
1607 @Override
1608 public void onAttachedToWindow() {
1609 super.onAttachedToWindow();
1610
1611 // Listen for broadcasts related to user-presence
1612 final IntentFilter filter = new IntentFilter();
1613 filter.addAction(Intent.ACTION_SCREEN_OFF);
1614 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001615 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001616 if (ENABLE_DEBUG_INTENTS) {
1617 filter.addAction(DebugIntents.DELETE_DATABASE);
1618 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1619 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001621 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001622 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001623 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001624 mVisible = true;
1625 }
1626
Adam Cohen0b395352014-06-09 22:54:36 +00001627 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001628 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001629 * This method is a no-op for other platform versions.
1630 */
Sunny Goyald0091012015-01-20 15:55:34 -08001631 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001632 private void setupTransparentSystemBarsForLollipop() {
1633 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001634 Window window = getWindow();
1635 window.getAttributes().systemUiVisibility |=
1636 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1637 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1638 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1639 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1640 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1641 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1642 window.setStatusBarColor(Color.TRANSPARENT);
1643 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001644 }
1645 }
1646
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 @Override
1648 public void onDetachedFromWindow() {
1649 super.onDetachedFromWindow();
1650 mVisible = false;
1651
Adam Cohend113e0c2010-11-11 10:48:05 -08001652 if (mAttached) {
1653 unregisterReceiver(mReceiver);
1654 mAttached = false;
1655 }
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001657 }
1658
1659 public void onWindowVisibilityChanged(int visibility) {
1660 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001661 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001662 // The following code used to be in onResume, but it turns out onResume is called when
1663 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1664 // is a more appropriate event to handle
1665 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 if (!mWorkspaceLoading) {
1667 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001668 // We want to let Launcher draw itself at least once before we force it to build
1669 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001670 // apps is nice and speedy.
1671 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001672 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001673 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001674 if (mStarted) return;
1675 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001676 // We delay the layer building a bit in order to give
1677 // other message processing a time to run. In particular
1678 // this avoids a delay in hiding the IME if it was
1679 // currently shown, because doing that may involve
1680 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001681 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001682 final ViewTreeObserver.OnDrawListener listener = this;
1683 mWorkspace.post(new Runnable() {
1684 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001685 if (mWorkspace != null &&
1686 mWorkspace.getViewTreeObserver() != null) {
1687 mWorkspace.getViewTreeObserver().
1688 removeOnDrawListener(listener);
1689 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001690 }
1691 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001692 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 }
1694 });
1695 }
1696 clearTypedText();
1697 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
1699
Adam Cohen091440a2015-03-18 14:16:05 -07001700 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 mHandler.removeMessages(ADVANCE_MSG);
1702 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1703 mHandler.sendMessageDelayed(msg, delay);
1704 mAutoAdvanceSentTime = System.currentTimeMillis();
1705 }
1706
Adam Cohen091440a2015-03-18 14:16:05 -07001707 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1709 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1710 mAutoAdvanceRunning = autoAdvanceRunning;
1711 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001712 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 sendAdvanceMessage(delay);
1714 } else {
1715 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001716 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1718 }
1719 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001720 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 }
1722 }
1723 }
1724
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001725 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1726
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001728 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 if (msg.what == ADVANCE_MSG) {
1730 int i = 0;
1731 for (View key: mWidgetsToAdvance.keySet()) {
1732 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001733 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 public void run() {
1737 ((Advanceable) v).advance();
1738 }
1739 }, delay);
1740 }
1741 i++;
1742 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001743 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001745 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001747 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001748
Winsonc0b52fe2015-09-09 16:38:15 -07001749 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001750 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1752 if (v instanceof Advanceable) {
1753 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001754 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001755 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
1757 }
1758
Winsonc0b52fe2015-09-09 16:38:15 -07001759 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 if (mWidgetsToAdvance.containsKey(hostView)) {
1761 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001762 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 }
1765
Hyunyoung Song3f471442015-04-08 19:01:34 -07001766 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001767 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1768 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770
Winson Chunga6945242014-01-08 14:04:34 -08001771 public DragLayer getDragLayer() {
1772 return mDragLayer;
1773 }
1774
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001775 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001776 return mAppsView;
1777 }
1778
Hyunyoung Song3f471442015-04-08 19:01:34 -07001779 public WidgetsContainerView getWidgetsView() {
1780 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001781 }
1782
Winson Chunga6945242014-01-08 14:04:34 -08001783 public Workspace getWorkspace() {
1784 return mWorkspace;
1785 }
1786
1787 public Hotseat getHotseat() {
1788 return mHotseat;
1789 }
1790
Jorim Jaggid017f882014-01-14 17:08:48 -08001791 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001792 return mOverviewPanel;
1793 }
1794
Winson Chung006ee262015-08-03 14:40:11 -07001795 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001796 return mSearchDropTargetBar;
1797 }
1798
Tony Wickham34d2c912015-09-11 09:27:58 -07001799 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1800 return mAppInfoDropTargetBar;
1801 }
1802
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001803 public LauncherAppWidgetHost getAppWidgetHost() {
1804 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Winson Chunga9abd0e2010-10-27 17:18:37 -07001807 public LauncherModel getModel() {
1808 return mModel;
1809 }
1810
Winson Chunga6945242014-01-08 14:04:34 -08001811 protected SharedPreferences getSharedPrefs() {
1812 return mSharedPrefs;
1813 }
1814
Adam Cohen2e6da152015-05-06 11:42:25 -07001815 public DeviceProfile getDeviceProfile() {
1816 return mDeviceProfile;
1817 }
1818
Bjorn Bringertc459e522013-06-07 19:36:01 +01001819 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001820 getWindow().closeAllPanels();
1821
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001822 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001823 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001824 }
1825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 @Override
1827 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001828 long startTime = 0;
1829 if (DEBUG_RESUME_TIME) {
1830 startTime = System.currentTimeMillis();
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 super.onNewIntent(intent);
1833
1834 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001835 Folder openFolder = mWorkspace.getOpenFolder();
1836 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1837 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1838 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1839 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1840 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001841 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001842 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001843
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 if (mWorkspace == null) {
1845 // Can be cases where mWorkspace is null, this prevents a NPE
1846 return;
1847 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001848 // In all these cases, only animate if we're already on home
1849 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001850
Adam Cohen6fecd412013-10-02 17:41:50 -07001851 closeFolder();
1852 exitSpringLoadedDragMode();
1853
1854 // If we are already on home, then just animate back to the workspace,
1855 // otherwise, just wait until onResume to set the state back to Workspace
1856 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001857 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001858 } else {
1859 mOnResumeState = State.WORKSPACE;
1860 }
1861
1862 final View v = getWindow().peekDecorView();
1863 if (v != null && v.getWindowToken() != null) {
1864 InputMethodManager imm = (InputMethodManager)getSystemService(
1865 INPUT_METHOD_SERVICE);
1866 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1867 }
1868
Winson Chungb745afb2015-03-02 11:51:23 -08001869 // Reset the apps view
1870 if (!alreadyOnHome && mAppsView != null) {
1871 mAppsView.scrollToTop();
1872 }
1873
Hyunyoung Song3f471442015-04-08 19:01:34 -07001874 // Reset the widgets view
1875 if (!alreadyOnHome && mWidgetsView != null) {
1876 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001878
Adam Cohen9211d422014-10-07 18:14:53 -07001879 if (mLauncherCallbacks != null) {
1880 mLauncherCallbacks.onHomeIntent();
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Adam Cohened307df2013-10-02 09:37:31 -07001883
Adam Cohen9211d422014-10-07 18:14:53 -07001884 if (mLauncherCallbacks != null) {
1885 mLauncherCallbacks.onNewIntent(intent);
1886 }
Winson15f8b172015-08-19 11:02:39 -07001887
1888 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1889 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1890 // animation.
1891 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001892 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1893 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001894 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1895 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001896
1897 // We use this flag to suppress noisy callbacks above custom content state
1898 // from onResume.
1899 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001900 mWorkspace.post(new Runnable() {
1901 @Override
1902 public void run() {
1903 mWorkspace.moveToDefaultScreen(true);
1904 }
1905 });
1906 }
1907 }
1908
1909 if (DEBUG_RESUME_TIME) {
1910 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1911 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001915 public void onRestoreInstanceState(Bundle state) {
1916 super.onRestoreInstanceState(state);
1917 for (int page: mSynchronouslyBoundPages) {
1918 mWorkspace.restoreInstanceStateForChild(page);
1919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
1922 @Override
1923 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001924 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001925 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1926 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001927 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001928 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
Michael Jurka883f55b2010-10-21 15:47:14 -07001930 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001931 // We close any open folder since it will not be re-opened, and we need to make sure
1932 // this state is reflected.
1933 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001937 ContentValues itemValues = new ContentValues();
1938 mPendingAddInfo.writeToValues(itemValues);
1939 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001940 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001941 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
Hyunyoung Song3f471442015-04-08 19:01:34 -07001944 // Save the current widgets tray?
1945 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001946
1947 if (mLauncherCallbacks != null) {
1948 mLauncherCallbacks.onSaveInstanceState(outState);
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
1951
1952 @Override
1953 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001955
Winson Chunge7a03942011-08-05 15:05:12 -07001956 // Remove all pending runnables
1957 mHandler.removeMessages(ADVANCE_MSG);
1958 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001959 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001960
Winson Chungcd2b0142011-06-08 16:02:26 -07001961 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001962 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001963
1964 // It's possible to receive onDestroy after a new Launcher activity has
1965 // been created. In this case, don't interfere with the new Launcher.
1966 if (mModel.isCurrentCallbacks(this)) {
1967 mModel.stopLoader();
1968 app.setLauncher(null);
1969 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001972 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001974 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001976 mAppWidgetHost = null;
1977
1978 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
1980 TextKeyListener.getInstance().release();
1981
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001982 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001983
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001984 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001986 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001987 mWorkspace = null;
1988 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001989
Michael Jurka2ecf9952012-06-18 12:52:28 -07001990 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001991
1992 if (mLauncherCallbacks != null) {
1993 mLauncherCallbacks.onDestroy();
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
Adam Cohena9cf38f2011-05-02 15:36:58 -07001997 public DragController getDragController() {
1998 return mDragController;
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 @Override
2002 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002003 onStartForResult(requestCode);
2004 super.startActivityForResult(intent, requestCode);
2005 }
2006
2007 @Override
2008 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2009 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2010 onStartForResult(requestCode);
2011 try {
2012 super.startIntentSenderForResult(intent, requestCode,
2013 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2014 } catch (IntentSender.SendIntentException e) {
2015 throw new ActivityNotFoundException();
2016 }
2017 }
2018
2019 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002020 if (requestCode >= 0) {
2021 setWaitingForResult(true);
2022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
2024
Winson Chung70d72102011-08-12 11:24:35 -07002025 /**
2026 * Indicates that we want global search for this activity by setting the globalSearch
2027 * argument for {@link #startSearch} to true.
2028 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002030 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002032
Karl Rosaen138a0412009-04-23 19:00:21 -07002033 if (initialQuery == null) {
2034 // Use any text typed in the launcher as the initial query
2035 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 if (appSearchData == null) {
2038 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002039 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
Winson Chungadf0c182012-08-23 14:59:07 -07002041 Rect sourceBounds = new Rect();
2042 if (mSearchDropTargetBar != null) {
2043 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2044 }
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002047 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002048 if (clearTextImmediately) {
2049 clearTypedText();
2050 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002051
2052 // We need to show the workspace after starting the search
2053 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002054 }
2055
Ian Parkinson047036e2014-09-01 15:40:53 +01002056 /**
2057 * Start a text search.
2058 *
2059 * @return {@code true} if the search will start immediately, so any further keypresses
2060 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2061 * to buffer keypresses.
2062 */
2063 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002065 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2066 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2067 sourceBounds);
2068 }
2069
Michael Jurkaa33411c2012-06-14 16:18:21 -07002070 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002071 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002072 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 }
2074
2075 /**
2076 * Starts the global search activity. This code is a copied from SearchManager
2077 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002080 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2082 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2083 if (globalSearchActivity == null) {
2084 Log.w(TAG, "No global search activity found.");
2085 return;
2086 }
2087 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2088 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2089 intent.setComponent(globalSearchActivity);
2090 // Make sure that we have a Bundle to put source in
2091 if (appSearchData == null) {
2092 appSearchData = new Bundle();
2093 } else {
2094 appSearchData = new Bundle(appSearchData);
2095 }
Adam Cohen9211d422014-10-07 18:14:53 -07002096 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002097 if (!appSearchData.containsKey("source")) {
2098 appSearchData.putString("source", getPackageName());
2099 }
2100 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2101 if (!TextUtils.isEmpty(initialQuery)) {
2102 intent.putExtra(SearchManager.QUERY, initialQuery);
2103 }
2104 if (selectInitialQuery) {
2105 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2106 }
2107 intent.setSourceBounds(sourceBounds);
2108 try {
2109 startActivity(intent);
2110 } catch (ActivityNotFoundException ex) {
2111 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114
Winson Chungbedf9232015-07-10 12:38:30 -07002115 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2116 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2117 return;
2118 }
2119
2120 // If not handled, then just start the provided search intent
2121 startActivitySafely(v, searchIntent, null);
2122 }
2123
Yura4f93ec62014-02-04 14:15:21 +00002124 public boolean isOnCustomContent() {
2125 return mWorkspace.isOnOrMovingToCustomContent();
2126 }
2127
Winson Chung70d72102011-08-12 11:24:35 -07002128 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002129 public boolean onPrepareOptionsMenu(Menu menu) {
2130 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002131 if (mLauncherCallbacks != null) {
2132 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2133 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002134 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002137 @Override
2138 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002139 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002140 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002141 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 }
2143
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 public boolean isWorkspaceLocked() {
2145 return mWorkspaceLoading || mWaitingForResult;
2146 }
2147
Adam Cohen517a7f52014-03-01 12:12:59 -08002148 public boolean isWorkspaceLoading() {
2149 return mWorkspaceLoading;
2150 }
2151
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002152 private void setWorkspaceLoading(boolean value) {
2153 boolean isLocked = isWorkspaceLocked();
2154 mWorkspaceLoading = value;
2155 if (isLocked != isWorkspaceLocked()) {
2156 onWorkspaceLockedChanged();
2157 }
2158 }
2159
2160 private void setWaitingForResult(boolean value) {
2161 boolean isLocked = isWorkspaceLocked();
2162 mWaitingForResult = value;
2163 if (isLocked != isWorkspaceLocked()) {
2164 onWorkspaceLockedChanged();
2165 }
2166 }
2167
Adam Cohen9211d422014-10-07 18:14:53 -07002168 protected void onWorkspaceLockedChanged() {
2169 if (mLauncherCallbacks != null) {
2170 mLauncherCallbacks.onWorkspaceLockedChanged();
2171 }
2172 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002177 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2178 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002179 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002181
Sunny Goyale6b63a32015-01-16 16:14:29 -08002182 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002183 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002184 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2185 }
2186
Sunny Goyale6b63a32015-01-16 16:14:29 -08002187 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002188 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2189 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002190 if (appWidgetInfo.configure != null) {
2191 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002192 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002193
Bjorn Bringert7984c942009-12-09 15:38:25 +00002194 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002195 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2196 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002199 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002200 Runnable onComplete = new Runnable() {
2201 @Override
2202 public void run() {
2203 // Exit spring loaded mode if necessary after adding the widget
2204 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2205 null);
2206 }
2207 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002209 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002210 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 }
2212 }
Romain Guycbb89e42009-06-08 15:52:54 -07002213
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002214 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002215 // Close any folders that may be open.
2216 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002217 mWorkspace.moveToCustomContentScreen(animate);
2218 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002219
2220 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2221 int[] cell, int spanX, int spanY) {
2222 switch (info.itemType) {
2223 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2224 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2225 int span[] = new int[2];
2226 span[0] = spanX;
2227 span[1] = spanY;
2228 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2229 container, screenId, cell, span);
2230 break;
2231 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2232 processShortcutFromDrop(info.componentName, container, screenId, cell);
2233 break;
2234 default:
2235 throw new IllegalStateException("Unknown item type: " + info.itemType);
2236 }
2237 }
2238
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 /**
2240 * Process a shortcut drop.
2241 *
2242 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002246 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2247 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251
2252 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.cellX = cell[0];
2254 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256
2257 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2258 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002259 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260 }
2261
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 /**
2263 * Process a widget drop.
2264 *
2265 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002269 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2270 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 mPendingAddInfo.minSpanX = info.minSpanX;
2275 mPendingAddInfo.minSpanY = info.minSpanY;
2276
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.cellX = cell[0];
2279 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 if (span != null) {
2282 mPendingAddInfo.spanX = span[0];
2283 mPendingAddInfo.spanY = span[1];
2284 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285
Adam Cohened66b2b2012-01-23 17:28:51 -08002286 AppWidgetHostView hostView = info.boundWidget;
2287 int appWidgetId;
2288 if (hostView != null) {
2289 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002291
2292 // Clear the boundWidget so that it doesn't get destroyed.
2293 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 // In this case, we either need to start an activity to get permission to bind
2296 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002297 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002298 Bundle options = info.bindOptions;
2299
Sunny Goyalffe83f12014-08-14 17:39:34 -07002300 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2301 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002302 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2308 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002309 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2310 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002311 // TODO: we need to make sure that this accounts for the options bundle.
2312 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2314 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 }
2317
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002320 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 folderInfo.title = getText(R.string.folder_name);
2322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002324 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2325 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002326 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327
2328 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 FolderIcon newFolder =
2330 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002333 // Force measure the new folder icon
2334 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2335 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002336 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 }
Romain Guycbb89e42009-06-08 15:52:54 -07002338
Winsonc0b52fe2015-09-09 16:38:15 -07002339 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002340 * Unbinds the view for the specified item, and removes the item and all its children.
2341 *
2342 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002343 * @param itemInfo the {@link ItemInfo} for this view.
2344 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002345 */
Winson2949fb52015-09-24 09:56:11 -07002346 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002347 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002348 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002349 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2350 if (folderInfo != null) {
2351 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002352 } else {
2353 mWorkspace.removeWorkspaceItem(v);
2354 }
Winsonc0b52fe2015-09-09 16:38:15 -07002355 if (deleteFromDb) {
2356 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2357 }
2358 } else if (itemInfo instanceof FolderInfo) {
2359 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2360 unbindFolder(folderInfo);
2361 mWorkspace.removeWorkspaceItem(v);
2362 if (deleteFromDb) {
2363 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2364 }
2365 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2366 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
Winson44818e02015-10-02 11:25:46 -07002367 mWorkspace.removeWorkspaceItem(v);
Sunny Goyal56c73602015-09-25 12:55:01 -07002368 removeWidgetToAutoAdvance(widgetInfo.hostView);
2369 widgetInfo.hostView = null;
Winsonc0b52fe2015-09-09 16:38:15 -07002370 if (deleteFromDb) {
Sunny Goyal56c73602015-09-25 12:55:01 -07002371 deleteWidgetInfo(widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002372 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002373
Winsonc0b52fe2015-09-09 16:38:15 -07002374 } else {
2375 return false;
2376 }
2377 return true;
2378 }
2379
2380 /**
2381 * Unbinds any launcher references to the folder.
2382 */
2383 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002384 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 }
2386
Winsonc0b52fe2015-09-09 16:38:15 -07002387 /**
Sunny Goyal56c73602015-09-25 12:55:01 -07002388 * Deletes the widget info and the widget id.
Winsonc0b52fe2015-09-09 16:38:15 -07002389 */
Sunny Goyal56c73602015-09-25 12:55:01 -07002390 private void deleteWidgetInfo(final LauncherAppWidgetInfo widgetInfo) {
Winsonc0b52fe2015-09-09 16:38:15 -07002391 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
Sunny Goyal56c73602015-09-25 12:55:01 -07002392 if (appWidgetHost != null && !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
Winsonc0b52fe2015-09-09 16:38:15 -07002393 // Deleting an app widget ID is a void call but writes to disk before returning
2394 // to the caller...
2395 new AsyncTask<Void, Void, Void>() {
2396 public Void doInBackground(Void ... args) {
2397 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2398 return null;
2399 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002400 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002401 }
Sunny Goyal56c73602015-09-25 12:55:01 -07002402 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
Winsonc0b52fe2015-09-09 16:38:15 -07002403 }
2404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 @Override
2406 public boolean dispatchKeyEvent(KeyEvent event) {
2407 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2408 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002410 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002411 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002412 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002413 dumpState();
2414 return true;
2415 }
2416 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 }
2418 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2419 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002420 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 return true;
2422 }
2423 }
2424
2425 return super.dispatchKeyEvent(event);
2426 }
2427
Joe Onorato88ec0992009-11-19 13:16:06 -08002428 @Override
2429 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002430 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2431 return;
2432 }
2433
Sunny Goyal45478022015-06-08 16:52:41 -07002434 if (mDragController.isDragging()) {
2435 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002436 return;
2437 }
2438
Winson Chungb745afb2015-03-02 11:51:23 -08002439 if (isAppsViewVisible()) {
2440 showWorkspace(true);
2441 } else if (isWidgetsViewVisible()) {
2442 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002443 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002444 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002445 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002446 Folder openFolder = mWorkspace.getOpenFolder();
2447 if (openFolder.isEditingName()) {
2448 openFolder.dismissEditingName();
2449 } else {
2450 closeFolder();
2451 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002453 mWorkspace.exitWidgetResizeMode();
2454
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 // Back button is a no-op here, but give at least some feedback for the button press
2456 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002457 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002458 }
2459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002461 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002462 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002463 @Override
2464 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002465 if (mAppWidgetHost != null) {
2466 mAppWidgetHost.startListening();
2467 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002468 }
2469
2470 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 * Launches the intent referred by the clicked shortcut.
2472 *
2473 * @param v The view representing the clicked shortcut.
2474 */
2475 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002476 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2477 // view has detached (it's possible for this to happen if the view is removed mid touch).
2478 if (v.getWindowToken() == null) {
2479 return;
2480 }
2481
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002482 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002483 return;
2484 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002485
Adam Cohen1697b792013-09-17 19:08:21 -07002486 if (v instanceof Workspace) {
2487 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002488 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002489 }
2490 return;
2491 }
2492
2493 if (v instanceof CellLayout) {
2494 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002495 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2496 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002497 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002498 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002499 }
2500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002502 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002505 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002507 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002508 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002509 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002510 } else if (tag instanceof AppInfo) {
2511 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002512 } else if (tag instanceof LauncherAppWidgetInfo) {
2513 if (v instanceof PendingAppWidgetHostView) {
2514 onClickPendingWidget((PendingAppWidgetHostView) v);
2515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 }
2517 }
2518
Sunny Goyal70660032015-05-14 00:07:08 -07002519 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002520 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002521 return false;
2522 }
2523
Michael Jurkaaf442092010-06-10 17:01:57 -07002524 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002525 * Event handler for the app widget view which has not fully restored.
2526 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002527 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002528 if (mIsSafeModeEnabled) {
2529 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2530 return;
2531 }
2532
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002533 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002534 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002535 int widgetId = info.appWidgetId;
2536 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2537 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002538 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2539 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002540 mPendingAddInfo.copyFrom(info);
2541 mPendingAddWidgetId = widgetId;
2542
Sunny Goyalffe83f12014-08-14 17:39:34 -07002543 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2544 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002545 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 } else if (info.installProgress < 0) {
2547 // The install has not been queued
2548 final String packageName = info.providerName.getPackageName();
2549 showBrokenAppInstallDialog(packageName,
2550 new DialogInterface.OnClickListener() {
2551 public void onClick(DialogInterface dialog, int id) {
2552 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2553 }
2554 });
2555 } else {
2556 // Download has started.
2557 final String packageName = info.providerName.getPackageName();
2558 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002559 }
2560 }
2561
2562 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002563 * Event handler for the "grid" button that appears on the home screen, which
2564 * enters all apps mode.
2565 *
2566 * @param v The view that was clicked.
2567 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002568 protected void onClickAllAppsButton(View v) {
2569 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002570 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002571 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002572 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002573
2574 if (mLauncherCallbacks != null) {
2575 mLauncherCallbacks.onClickAllAppsButton(v);
2576 }
Winson Chung76648c52015-07-10 14:33:23 -07002577 }
2578 }
2579
2580 protected void onLongClickAllAppsButton(View v) {
2581 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2582 if (!isAppsViewVisible()) {
2583 showAppsView(true /* animated */, false /* resetListToTop */,
2584 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 }
2586 }
2587
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002588 private void showBrokenAppInstallDialog(final String packageName,
2589 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002590 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002591 .setTitle(R.string.abandoned_promises_title)
2592 .setMessage(R.string.abandoned_promise_explanation)
2593 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2594 .setNeutralButton(R.string.abandoned_clean_this,
2595 new DialogInterface.OnClickListener() {
2596 public void onClick(DialogInterface dialog, int id) {
2597 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2598 mWorkspace.removeAbandonedPromise(packageName, user);
2599 }
2600 })
2601 .create().show();
2602 return;
2603 }
2604
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 /**
2606 * Event handler for an app shortcut click.
2607 *
2608 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2609 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002610 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2612 Object tag = v.getTag();
2613 if (!(tag instanceof ShortcutInfo)) {
2614 throw new IllegalArgumentException("Input must be a Shortcut");
2615 }
2616
2617 // Open shortcut
2618 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002619
2620 if (shortcut.isDisabled != 0) {
2621 int error = R.string.activity_not_available;
2622 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2623 error = R.string.safemode_shortcut_error;
2624 }
2625 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2626 return;
2627 }
2628
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002630 if ((v instanceof BubbleTextView)
2631 && shortcut.isPromise()
2632 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002634 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002637 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002644 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002645
2646 if (mLauncherCallbacks != null) {
2647 mLauncherCallbacks.onClickAppShortcut(v);
2648 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 }
2650
Adam Cohen091440a2015-03-18 14:16:05 -07002651 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002653 final ShortcutInfo shortcut;
2654 final Intent intent;
2655 if (tag instanceof ShortcutInfo) {
2656 shortcut = (ShortcutInfo) tag;
2657 intent = shortcut.intent;
2658 int[] pos = new int[2];
2659 v.getLocationOnScreen(pos);
2660 intent.setSourceBounds(new Rect(pos[0], pos[1],
2661 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002662
Sunny Goyal508da152014-08-14 10:53:27 -07002663 } else if (tag instanceof AppInfo) {
2664 shortcut = null;
2665 intent = ((AppInfo) tag).intent;
2666 } else {
2667 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2668 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669
2670 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002671 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672
2673 if (success && v instanceof BubbleTextView) {
2674 mWaitingForResume = (BubbleTextView) v;
2675 mWaitingForResume.setStayPressed(true);
2676 }
2677 }
2678
2679 /**
2680 * Event handler for a folder icon click.
2681 *
2682 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2683 */
2684 protected void onClickFolderIcon(View v) {
2685 if (LOGD) Log.d(TAG, "onClickFolder");
2686 if (!(v instanceof FolderIcon)){
2687 throw new IllegalArgumentException("Input must be a FolderIcon");
2688 }
2689
Sunny Goyal317698b2015-07-29 11:45:41 -07002690 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 FolderIcon folderIcon = (FolderIcon) v;
2692 final FolderInfo info = folderIcon.getFolderInfo();
2693 Folder openFolder = mWorkspace.getFolderForTag(info);
2694
2695 // If the folder info reports that the associated folder is open, then verify that
2696 // it is actually opened. There have been a few instances where this gets out of sync.
2697 if (info.opened && openFolder == null) {
2698 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2699 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2700 info.opened = false;
2701 }
2702
2703 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2704 // Close any open folder
2705 closeFolder();
2706 // Open the requested folder
2707 openFolder(folderIcon);
2708 } else {
2709 // Find the open folder...
2710 int folderScreen;
2711 if (openFolder != null) {
2712 folderScreen = mWorkspace.getPageForView(openFolder);
2713 // .. and close it
2714 closeFolder(openFolder);
2715 if (folderScreen != mWorkspace.getCurrentPage()) {
2716 // Close any folder open on the current screen
2717 closeFolder();
2718 // Pull the folder onto this screen
2719 openFolder(folderIcon);
2720 }
2721 }
2722 }
Adam Cohen9211d422014-10-07 18:14:53 -07002723
2724 if (mLauncherCallbacks != null) {
2725 mLauncherCallbacks.onClickFolderIcon(v);
2726 }
Michael Jurka5130e402011-10-13 04:55:35 -07002727 }
2728
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002729 /**
2730 * Event handler for the (Add) Widgets button that appears after a long press
2731 * on the home screen.
2732 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002733 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002735 if (mIsSafeModeEnabled) {
2736 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2737 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002738 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002739 if (mLauncherCallbacks != null) {
2740 mLauncherCallbacks.onClickAddWidgetButton(view);
2741 }
Adam Cohen9211d422014-10-07 18:14:53 -07002742 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002743 }
2744
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 /**
2746 * Event handler for the wallpaper picker button that appears after a long press
2747 * on the home screen.
2748 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002749 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002750 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002751 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2752 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2753 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2754 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002755 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002756
2757 if (mLauncherCallbacks != null) {
2758 mLauncherCallbacks.onClickWallpaperPicker(v);
2759 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002760 }
2761
2762 /**
2763 * Event handler for a click on the settings button that appears after a long press
2764 * on the home screen.
2765 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002766 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002770 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002771 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002772 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 }
2774
Adam Cohen61f560d2013-09-30 15:58:20 -07002775 public View.OnTouchListener getHapticFeedbackTouchListener() {
2776 if (mHapticFeedbackTouchListener == null) {
2777 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002778 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002779 @Override
2780 public boolean onTouch(View v, MotionEvent event) {
2781 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2782 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2783 }
2784 return false;
2785 }
2786 };
2787 }
2788 return mHapticFeedbackTouchListener;
2789 }
2790
Adam Cohen9211d422014-10-07 18:14:53 -07002791 public void onDragStarted(View view) {
2792 if (isOnCustomContent()) {
2793 // Custom content screen doesn't participate in drag and drop. If on custom
2794 // content screen, move to default.
2795 moveWorkspaceToDefaultScreen();
2796 }
2797
2798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onDragStarted(view);
2800 }
2801 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002802
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002803 /**
2804 * Called when the user stops interacting with the launcher.
2805 * This implies that the user is now on the homescreen and is not doing housekeeping.
2806 */
Adam Cohen9211d422014-10-07 18:14:53 -07002807 protected void onInteractionEnd() {
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onInteractionEnd();
2810 }
2811 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002812
2813 /**
2814 * Called when the user starts interacting with the launcher.
2815 * The possible interactions are:
2816 * - open all apps
2817 * - reorder an app shortcut, or a widget
2818 * - open the overview mode.
2819 * This is a good time to stop doing things that only make sense
2820 * when the user is on the homescreen and not doing housekeeping.
2821 */
Adam Cohen9211d422014-10-07 18:14:53 -07002822 protected void onInteractionBegin() {
2823 if (mLauncherCallbacks != null) {
2824 mLauncherCallbacks.onInteractionBegin();
2825 }
2826 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002827
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 /** Updates the interaction state. */
2829 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002830 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002831 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002832 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2833 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002834 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002835 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002836 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002837 onInteractionEnd();
2838 }
2839 }
2840
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002842 try {
2843 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002844 launcherApps.showAppDetailsForProfile(componentName, user);
2845 } catch (SecurityException e) {
2846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2847 Log.e(TAG, "Launcher does not have permission to launch settings");
2848 } catch (ActivityNotFoundException e) {
2849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2850 Log.e(TAG, "Unable to launch settings");
2851 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002852 }
2853
Michael Jurka1e2f4652013-07-08 18:03:46 -07002854 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002855 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2856 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002857 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002858 // System applications cannot be installed. For now, show a toast explaining that.
2859 // We may give them the option of disabling apps this way.
2860 int messageId = R.string.uninstall_system_app_text;
2861 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002862 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002863 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002864 String packageName = componentName.getPackageName();
2865 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002866 Intent intent = new Intent(
2867 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2869 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002870 if (user != null) {
2871 user.addToIntent(intent, Intent.EXTRA_USER);
2872 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002873 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002874 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002876 }
2877
Winson Chung8f1eff72015-05-28 17:33:40 -07002878 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002879 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002881 // Only launch using the new animation if the shortcut has not opted out (this is a
2882 // private contract between launcher and may be ignored in the future).
2883 boolean useLaunchAnimation = (v != null) &&
2884 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002885 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2886 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002887
Kenny Guy1317e2d2014-05-08 18:52:50 +01002888 UserHandleCompat user = null;
2889 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2890 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2891 user = userManager.getUserForSerialNumber(serialNumber);
2892 }
2893
2894 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002895 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002896 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002897 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002898 int left = 0, top = 0;
2899 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2900 if (v instanceof TextView) {
2901 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002902 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2903 if (icon != null) {
2904 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002905 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002906 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002907 width = bounds.width();
2908 height = bounds.height();
2909 }
2910 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002911 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2912 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002913 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002914 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002915 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002916 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002917 // On L devices, we use the device default slide-up transition.
2918 // On L MR1 devices, we a custom version of the slide-up transition which
2919 // doesn't have the delay present in the device default.
2920 opts = ActivityOptions.makeCustomAnimation(this,
2921 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002922 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002923 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002924 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002925
2926 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2927 // Could be launching some bookkeeping activity
2928 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002929 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002930 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002931 launcherApps.startActivityForProfile(intent.getComponent(), user,
2932 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002933 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002934 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 } catch (SecurityException e) {
2936 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002937 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002939 "or use the exported attribute for this activity. "
2940 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002942 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002944
Winson Chungbedf9232015-07-10 12:38:30 -07002945 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002946 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002947 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2948 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2949 return false;
2950 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002951 try {
2952 success = startActivity(v, intent, tag);
2953 } catch (ActivityNotFoundException e) {
2954 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2955 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2956 }
2957 return success;
2958 }
2959
Adam Cohen268c4752012-06-06 17:47:33 -07002960 /**
2961 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2962 * in the DragLayer in the exact absolute location of the original FolderIcon.
2963 */
2964 private void copyFolderIconToImage(FolderIcon fi) {
2965 final int width = fi.getMeasuredWidth();
2966 final int height = fi.getMeasuredHeight();
2967
2968 // Lazy load ImageView, Bitmap and Canvas
2969 if (mFolderIconImageView == null) {
2970 mFolderIconImageView = new ImageView(this);
2971 }
2972 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2973 mFolderIconBitmap.getHeight() != height) {
2974 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2975 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2976 }
2977
2978 DragLayer.LayoutParams lp;
2979 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2980 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2981 } else {
2982 lp = new DragLayer.LayoutParams(width, height);
2983 }
2984
Adam Cohen307fe232012-08-16 17:55:58 -07002985 // The layout from which the folder is being opened may be scaled, adjust the starting
2986 // view size by this scale factor.
2987 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002988 lp.customPosition = true;
2989 lp.x = mRectForFolderAnimation.left;
2990 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002991 lp.width = (int) (scale * width);
2992 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002993
2994 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2995 fi.draw(mFolderIconCanvas);
2996 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002997 if (fi.getFolder() != null) {
2998 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2999 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003000 }
Adam Cohen268c4752012-06-06 17:47:33 -07003001 // Just in case this image view is still in the drag layer from a previous animation,
3002 // we remove it and re-add it.
3003 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3004 mDragLayer.removeView(mFolderIconImageView);
3005 }
3006 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003007 if (fi.getFolder() != null) {
3008 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003009 }
Adam Cohen268c4752012-06-06 17:47:33 -07003010 }
3011
Adam Cohen2801caf2011-05-13 20:57:39 -07003012 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003013 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003014 FolderInfo info = (FolderInfo) fi.getTag();
3015 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3016 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003017 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3018 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003019 }
3020
Adam Cohen268c4752012-06-06 17:47:33 -07003021 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3022 copyFolderIconToImage(fi);
3023 fi.setVisibility(View.INVISIBLE);
3024
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003025 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3026 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003027 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003028 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3029 }
3030 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003031 oa.start();
3032 }
3033
Adam Cohen268c4752012-06-06 17:47:33 -07003034 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003035 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003036 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003037
Adam Cohen268c4752012-06-06 17:47:33 -07003038 // We remove and re-draw the FolderIcon in-case it has changed
3039 mDragLayer.removeView(mFolderIconImageView);
3040 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003041 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003042 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 oa.addListener(new AnimatorListenerAdapter() {
3044 @Override
3045 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003046 if (cl != null) {
3047 cl.clearFolderLeaveBehind();
3048 // Remove the ImageView copy of the FolderIcon and make the original visible.
3049 mDragLayer.removeView(mFolderIconImageView);
3050 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003051 }
3052 }
3053 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 oa.start();
3055 }
3056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003058 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 * is animated relative to the specified View. If the View is null, no animation
3060 * is played.
3061 *
3062 * @param folderInfo The FolderInfo describing the folder to open.
3063 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003064 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003065 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003066 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3067 if (openFolder != null && openFolder != folder) {
3068 // Close any open folder before opening a folder.
3069 closeFolder();
3070 }
3071
Adam Cohena9cf38f2011-05-02 15:36:58 -07003072 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073
Adam Cohena9cf38f2011-05-02 15:36:58 -07003074 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075
Sunny Goyalf4066152015-04-15 09:42:19 -07003076 // While the folder is open, the position of the icon cannot change.
3077 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3078
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 // Just verify that the folder hasn't already been added to the DragLayer.
3080 // There was a one-off crash where the folder had a parent already.
3081 if (folder.getParent() == null) {
3082 mDragLayer.addView(folder);
3083 mDragController.addDropTarget((DropTarget) folder);
3084 } else {
3085 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3086 folder.getParent() + ").");
3087 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003088 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003089 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003090
3091 // Notify the accessibility manager that this folder "window" has appeared and occluded
3092 // the workspace items
3093 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3094 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003095 }
3096
3097 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003098 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003099 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003100 if (folder.isEditingName()) {
3101 folder.dismissEditingName();
3102 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 closeFolder(folder);
3104 }
3105 }
3106
Sunny Goyal83a8f042015-05-19 12:52:12 -07003107 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003108 folder.getInfo().opened = false;
3109
3110 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3111 if (parent != null) {
3112 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3113 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003114 if (fi != null) {
3115 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3116 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 }
3118 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003119
3120 // Notify the accessibility manager that this folder "window" has disappeard and no
3121 // longer occludeds the workspace items
3122 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 }
3124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003126 if (!isDraggingEnabled()) return false;
3127 if (isWorkspaceLocked()) return false;
3128 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129
Winson Chung76648c52015-07-10 14:33:23 -07003130 if (v == mAllAppsButton) {
3131 onLongClickAllAppsButton(v);
3132 return true;
3133 }
3134
Adam Cohen1697b792013-09-17 19:08:21 -07003135 if (v instanceof Workspace) {
3136 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003137 if (!mWorkspace.isTouchActive()) {
3138 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003139 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3140 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3141 return true;
3142 } else {
3143 return false;
3144 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003145 } else {
3146 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003147 }
Adam Cohen1697b792013-09-17 19:08:21 -07003148 }
3149
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003150 CellLayout.CellInfo longClickCellInfo = null;
3151 View itemUnderLongClick = null;
3152 if (v.getTag() instanceof ItemInfo) {
3153 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003154 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003155 itemUnderLongClick = longClickCellInfo.cell;
3156 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 }
3158
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 // The hotseat touch handling does not go through Workspace, and we always allow long press
3160 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003161 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003162 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003163 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003164 // User long pressed on empty space
3165 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3166 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003167 if (mWorkspace.isInOverviewMode()) {
3168 mWorkspace.startReordering(v);
3169 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003170 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003173 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3174 mHotseat.getOrderInHotseat(
3175 longClickCellInfo.cellX,
3176 longClickCellInfo.cellY));
3177 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003179 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 }
3181 }
3182 }
3183 return true;
3184 }
3185
Winson Chung3d503fb2011-07-13 17:25:49 -07003186 boolean isHotseatLayout(View layout) {
3187 return mHotseat != null && layout != null &&
3188 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3189 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003190
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 /**
3192 * Returns the CellLayout of the specified container at the specified screen.
3193 */
Sunny Goyal92820592015-03-02 11:31:35 -08003194 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3196 if (mHotseat != null) {
3197 return mHotseat.getLayout();
3198 } else {
3199 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003200 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003202 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003203 }
3204 }
3205
Winson Chungb745afb2015-03-02 11:51:23 -08003206 /**
3207 * For overridden classes.
3208 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003209 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003210 return isAppsViewVisible();
3211 }
3212
3213 public boolean isAppsViewVisible() {
3214 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3215 }
3216
3217 public boolean isWidgetsViewVisible() {
3218 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003219 }
3220
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003221 private void setWorkspaceBackground(int background) {
3222 switch (background) {
3223 case WORKSPACE_BACKGROUND_TRANSPARENT:
3224 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3225 break;
3226 case WORKSPACE_BACKGROUND_BLACK:
3227 getWindow().setBackgroundDrawable(null);
3228 break;
3229 default:
3230 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3231 }
Craig Mautner360310b2012-10-26 15:13:08 -07003232 }
3233
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003234 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3236 int curflags = getWindow().getAttributes().flags
3237 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3238 if (wpflags != curflags) {
3239 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3240 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003241 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003242 }
3243
Michael Jurkae326f182011-11-21 14:05:46 -08003244 @Override
3245 public void onTrimMemory(int level) {
3246 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003247 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3248 // The widget preview db can result in holding onto over
3249 // 3MB of memory for caching which isn't necessary.
3250 SQLiteDatabase.releaseMemory();
3251
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003252 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003253 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003254 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003255 if (mLauncherCallbacks != null) {
3256 mLauncherCallbacks.onTrimMemory(level);
3257 }
Michael Jurkae326f182011-11-21 14:05:46 -08003258 }
3259
Winson5c6bdbb2015-09-03 11:36:19 -07003260 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003261 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003262 }
3263
Winson5c6bdbb2015-09-03 11:36:19 -07003264 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003265 boolean changed = mState != State.WORKSPACE ||
3266 mWorkspace.getState() != Workspace.State.NORMAL;
3267 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003268 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003269 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003270 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003271
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 // Set focus to the AppsCustomize button
3273 if (mAllAppsButton != null) {
3274 mAllAppsButton.requestFocus();
3275 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003276 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003277
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003278 // Change the state *after* we've called all the transition code
3279 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003280
Winson Chung5fb63472011-02-02 17:03:37 -08003281 // Resume the auto-advance of widgets
3282 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003283 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003284
Winson Chung0f785722015-04-08 10:27:49 -07003285 if (changed) {
3286 // Send an accessibility event to announce the context change
3287 getWindow().getDecorView()
3288 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003289 }
Winson5c6bdbb2015-09-03 11:36:19 -07003290 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003291 }
3292
Adam Cohened307df2013-10-02 09:37:31 -07003293 void showOverviewMode(boolean animated) {
3294 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003295 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003296 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003297 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003298 }
3299
Winson Chungb745afb2015-03-02 11:51:23 -08003300 /**
3301 * Shows the apps view.
3302 */
Winson Chung76648c52015-07-10 14:33:23 -07003303 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3304 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003305 if (resetListToTop) {
3306 mAppsView.scrollToTop();
3307 }
Winson Chung4ac30062015-05-08 17:34:17 -07003308 if (updatePredictedApps) {
3309 tryAndUpdatePredictedApps();
3310 }
Winson Chung76648c52015-07-10 14:33:23 -07003311 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003312 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313
Winson Chungb745afb2015-03-02 11:51:23 -08003314 /**
3315 * Shows the widgets view.
3316 */
3317 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003318 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003319 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003321 }
Winson Chung76648c52015-07-10 14:33:23 -07003322 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003323
3324 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003325 @Override
3326 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003327 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003328 }
3329 });
Winson Chungb745afb2015-03-02 11:51:23 -08003330 }
3331
3332 /**
3333 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003334 *
3335 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003336 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003337 // TODO: calling method should use the return value so that when {@code false} is returned
3338 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003339 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003340 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3341 mState != State.WIDGETS_SPRING_LOADED) {
3342 return false;
3343 }
3344 if (toState != State.APPS && toState != State.WIDGETS) {
3345 return false;
3346 }
Winson Chungb745afb2015-03-02 11:51:23 -08003347
3348 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003349 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3350 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003351 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003352 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003353 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003356 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357
3358 // Pause the auto-advance of widgets until we are out of AllApps
3359 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003360 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361 closeFolder();
3362
3363 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003364 getWindow().getDecorView()
3365 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003366 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003367 }
3368
Winson Chungcd99cd32015-04-29 11:03:24 -07003369 /**
3370 * Updates the workspace and interaction state on state change, and return the animation to this
3371 * new state.
3372 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003373 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003374 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003375 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003376 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003377 updateInteraction(fromState, toState);
3378 return anim;
3379 }
3380
Jun Mukaif0033da2015-08-04 18:34:30 -07003381 public void onLauncherClingShown() {
3382 // When a launcher cling appears, it should cover the underlying layers, so their focus
3383 // should be blocked.
3384 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3385 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3386 }
3387 }
3388
3389 public void onLauncherClingDismissed() {
3390 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3391 }
3392
Hyunyoung Song3f471442015-04-08 19:01:34 -07003393 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003394 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003395 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003396 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003397 }
Winson Chungb745afb2015-03-02 11:51:23 -08003398
Winson Chung006ee262015-08-03 14:40:11 -07003399 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003400 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003401 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003402
3403 if (isAppsViewVisible()) {
3404 mState = State.APPS_SPRING_LOADED;
3405 } else if (isWidgetsViewVisible()) {
3406 mState = State.WIDGETS_SPRING_LOADED;
3407 } else {
3408 mState = State.WORKSPACE_SPRING_LOADED;
3409 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003410 }
Adam Cohen7777d962011-08-18 18:58:38 -07003411
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003413 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003414 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003415
Winson Chunge7a03942011-08-05 15:05:12 -07003416 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003417 @Override
3418 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003419 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3421 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003422 // Before we show workspace, hide all apps again because
3423 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3424 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003425 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003426 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003427 } else {
3428 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003429 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003430 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003431 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003432 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003433
Tony Wickham94e0d372015-09-11 12:17:48 -07003434 private boolean isStateSpringLoaded() {
3435 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3436 || mState == State.WIDGETS_SPRING_LOADED;
3437 }
3438
Michael Jurkad3ef3062010-11-23 16:23:58 -08003439 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003440 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003441 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003442 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003443 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003444 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003445 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3446 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003447 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003448 }
3449
Winson Chung4ac30062015-05-08 17:34:17 -07003450 /**
3451 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3452 * resumed.
3453 */
3454 private void tryAndUpdatePredictedApps() {
3455 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003456 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003457 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003458 mAppsView.setPredictedApps(apps);
3459 }
3460 }
3461 }
3462
Michael Jurkab3e22d92011-10-31 15:58:33 -07003463 void lockAllApps() {
3464 // TODO
3465 }
3466
3467 void unlockAllApps() {
3468 // TODO
3469 }
3470
Sunny Goyal594d76d2014-11-06 10:12:54 -08003471 protected void disableVoiceButtonProxy(boolean disable) {
3472 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003473 }
3474
Winsonf768d932015-09-25 16:12:35 -07003475 public boolean launcherCallbacksProvidesSearch() {
3476 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3477 }
3478
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003479 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003480 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003481 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003482 }
3483
Adam Cohen24ce0b32014-01-14 16:18:14 -08003484 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003485 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3486 if (searchProvider == null) {
3487 return null;
3488 }
3489
3490 Bundle opts = new Bundle();
3491 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003492 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003493
Tony Wickham3a3517f2015-10-06 11:27:04 -07003494 // Determine the min and max dimensions of the widget.
3495 LauncherAppState app = LauncherAppState.getInstance();
3496 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3497 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3498 float density = getResources().getDisplayMetrics().density;
3499 Rect searchBounds = portraitProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3500 int maxHeight = (int) (searchBounds.height() / density);
3501 int minHeight = maxHeight;
3502 int maxWidth = (int) (searchBounds.width() / density);
3503 int minWidth = maxWidth;
3504 if (!landscapeProfile.isVerticalBarLayout()) {
3505 searchBounds = landscapeProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3506 maxHeight = (int) Math.max(maxHeight, searchBounds.height() / density);
3507 minHeight = (int) Math.min(minHeight, searchBounds.height() / density);
3508 maxWidth = (int) Math.max(maxWidth, searchBounds.width() / density);
3509 minWidth = (int) Math.min(minWidth, searchBounds.width() / density);
3510 }
3511 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3512 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3513 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3514 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
3515
Sunny Goyal594d76d2014-11-06 10:12:54 -08003516 SharedPreferences sp = getSharedPreferences(
3517 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3518 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003519 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003520 if (!searchProvider.provider.flattenToString().equals(
3521 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003522 || (widgetInfo == null)
3523 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003524 // A valid widget is not already bound.
3525 if (widgetId > -1) {
3526 mAppWidgetHost.deleteAppWidgetId(widgetId);
3527 widgetId = -1;
3528 }
3529
3530 // Try to bind a new widget
3531 widgetId = mAppWidgetHost.allocateAppWidgetId();
3532
3533 if (!AppWidgetManagerCompat.getInstance(this)
3534 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3535 mAppWidgetHost.deleteAppWidgetId(widgetId);
3536 widgetId = -1;
3537 }
3538
3539 sp.edit()
3540 .putInt(QSB_WIDGET_ID, widgetId)
3541 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3542 .commit();
3543 }
3544
Sunny Goyal8d595092015-07-06 12:22:14 -07003545 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003546 if (widgetId != -1) {
3547 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003548 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003549 mQsb.updateAppWidgetOptions(opts);
3550 mQsb.setPadding(0, 0, 0, 0);
3551 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003552 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003553 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003554 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003555 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003556 }
3557
Sunny Goyal22235bc2015-04-03 09:23:43 -07003558 private void reinflateQSBIfNecessary() {
3559 if (mQsb instanceof LauncherAppWidgetHostView &&
3560 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3561 mSearchDropTargetBar.removeView(mQsb);
3562 mQsb = null;
3563 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3564 }
3565 }
3566
Michael Jurkad7c28052012-04-27 15:43:36 -07003567 @Override
3568 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003569 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003570 final List<CharSequence> text = event.getText();
3571 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003572 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003573 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003574 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003575 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003576 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003577 } else if (mWorkspace != null) {
3578 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003579 } else {
3580 text.add(getString(R.string.all_apps_home_button_label));
3581 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003582 return result;
3583 }
3584
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003585 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003586 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003587 */
Adam Cohen091440a2015-03-18 14:16:05 -07003588 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003589 @Override
3590 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003591 closeSystemDialogs();
3592 }
3593 }
3594
3595 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 * If the activity is currently paused, signal that we need to run the passed Runnable
3597 * in onResume.
3598 *
3599 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003600 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3601 * wrong when we're not running, and if the activity comes back to what the configuration was
3602 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003603 *
3604 * Implementation of the method from LauncherModel.Callbacks.
3605 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003606 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003607 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003608 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003609 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003610 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003611 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003612 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003613 }
3614 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003615 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003616 return true;
3617 } else {
3618 return false;
3619 }
3620 }
3621
Michael Jurkac402cd92013-05-20 15:49:32 +02003622 private boolean waitUntilResume(Runnable run) {
3623 return waitUntilResume(run, false);
3624 }
3625
Michael Jurka1e2f4652013-07-08 18:03:46 -07003626 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003627 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003628 }
3629
Michael Jurka7607c2f2013-04-03 14:33:19 -07003630 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003631 * If the activity is currently paused, signal that we need to re-run the loader
3632 * in onResume.
3633 *
3634 * This needs to be called from incoming places where resources might have been loaded
3635 * while we are paused. That is becaues the Configuration might be wrong
3636 * when we're not running, and if it comes back to what it was when we
3637 * were paused, we are not restarted.
3638 *
3639 * Implementation of the method from LauncherModel.Callbacks.
3640 *
3641 * @return true if we are currently paused. The caller might be able to
3642 * skip some work in that case since we will come back again.
3643 */
3644 public boolean setLoadOnResume() {
3645 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003646 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003647 mOnResumeNeedsLoad = true;
3648 return true;
3649 } else {
3650 return false;
3651 }
3652 }
3653
3654 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003658 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003659 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003660 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003661 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003664
Joe Onorato9c1289c2009-08-17 11:03:03 -04003665 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003666 * Clear any pending bind callbacks. This is called when is loader is planning to
3667 * perform a full rebind from scratch.
3668 */
3669 @Override
3670 public void clearPendingBinds() {
3671 mBindOnResumeCallbacks.clear();
3672 if (mPendingExecutor != null) {
3673 mPendingExecutor.markCompleted();
3674 mPendingExecutor = null;
3675 }
3676 }
3677
3678 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 * Refreshes the shortcuts shown on the workspace.
3680 *
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 */
3683 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003684 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003685
Winson Chungd64d1762013-08-20 14:37:16 -07003686 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003687 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003688 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003689
Michael Jurka05bf644e2011-11-30 20:28:53 -08003690 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003691 if (mHotseat != null) {
3692 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003693 }
3694 }
3695
Adam Cohendcd297f2013-06-18 13:13:40 -07003696 @Override
3697 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003698 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003699
Adam Cohen5084cba2013-09-03 12:01:16 -07003700 // If there are no screens, we need to have an empty screen
3701 if (orderedScreenIds.size() == 0) {
3702 mWorkspace.addExtraEmptyScreen();
3703 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003704
3705 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003706 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003707 if (hasCustomContentToLeft()) {
3708 mWorkspace.createCustomContentContainer();
3709 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003710 }
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712
3713 @Override
3714 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003715 int count = orderedScreenIds.size();
3716 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003717 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003718 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003719 }
3720
Winson Chungd64d1762013-08-20 14:37:16 -07003721 public void bindAppsAdded(final ArrayList<Long> newScreens,
3722 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003723 final ArrayList<ItemInfo> addAnimated,
3724 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003725 Runnable r = new Runnable() {
3726 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003727 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003728 }
3729 };
3730 if (waitUntilResume(r)) {
3731 return;
3732 }
3733
Winson Chungd64d1762013-08-20 14:37:16 -07003734 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003735 if (newScreens != null) {
3736 bindAddScreens(newScreens);
3737 }
Winson Chungd64d1762013-08-20 14:37:16 -07003738
3739 // We add the items without animation on non-visible pages, and with
3740 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003741 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003742 bindItems(addNotAnimated, 0,
3743 addNotAnimated.size(), false);
3744 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003745 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003746 bindItems(addAnimated, 0,
3747 addAnimated.size(), true);
3748 }
Winson Chungc58497e2013-09-03 17:48:37 -07003749
Winson Chung87412982013-10-03 18:34:14 -07003750 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003751 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003752
Winson Chungb745afb2015-03-02 11:51:23 -08003753 if (addedApps != null && mAppsView != null) {
3754 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003755 }
Winson Chungd64d1762013-08-20 14:37:16 -07003756 }
3757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 * Bind the items start-end from the list.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762 */
Tony Wickham0bb211a2015-10-02 16:22:08 -07003763 @Override
Winson Chung64359a52013-07-08 17:17:08 -07003764 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3765 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 Runnable r = new Runnable() {
3767 public void run() {
3768 bindItems(shortcuts, start, end, forceAnimateIcons);
3769 }
3770 };
3771 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003772 return;
3773 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003774
Winson Chungf0c6ae02012-03-21 16:10:31 -07003775 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003776 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3777 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003778 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003779 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003780 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003781 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003783
3784 // Short circuit if we are loading dock items for a configuration which has no dock
3785 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3786 mHotseat == null) {
3787 continue;
3788 }
3789
Sunny Goyal639e9062015-08-19 19:17:06 -07003790 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 switch (item.itemType) {
3792 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3793 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003795 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003796
Winson Chung64359a52013-07-08 17:17:08 -07003797 /*
3798 * TODO: FIX collision case
3799 */
Winson Chungce376632013-07-11 16:12:41 -07003800 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3801 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3802 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003803 View v = cl.getChildAt(item.cellX, item.cellY);
3804 Object tag = v.getTag();
3805 String desc = "Collision while binding workspace item: " + item
3806 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003807 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003808 throw (new RuntimeException(desc));
3809 } else {
3810 Log.d(TAG, desc);
3811 }
Winson Chungce376632013-07-11 16:12:41 -07003812 }
Winson Chung64359a52013-07-08 17:17:08 -07003813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003815 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003816 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003817 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003818 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003820 default:
3821 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003823
3824 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3825 item.cellY, 1, 1);
3826 if (animateIcons) {
3827 // Animate all the applications up now
3828 view.setAlpha(0f);
3829 view.setScaleX(0f);
3830 view.setScaleY(0f);
3831 bounceAnims.add(createNewAppBounceAnimation(view, i));
3832 newShortcutsScreenId = item.screenId;
3833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003834 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835
Winson Chung997a9232013-07-24 15:33:46 -07003836 if (animateIcons) {
3837 // Animate to the correct page
3838 if (newShortcutsScreenId > -1) {
3839 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003840 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003841 final Runnable startBounceAnimRunnable = new Runnable() {
3842 public void run() {
3843 anim.playTogether(bounceAnims);
3844 anim.start();
3845 }
3846 };
Winson Chung997a9232013-07-24 15:33:46 -07003847 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003848 // We post the animation slightly delayed to prevent slowdowns
3849 // when we are loading right after we return to launcher.
3850 mWorkspace.postDelayed(new Runnable() {
3851 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003852 if (mWorkspace != null) {
3853 mWorkspace.snapToPage(newScreenIndex);
3854 mWorkspace.postDelayed(startBounceAnimRunnable,
3855 NEW_APPS_ANIMATION_DELAY);
3856 }
Winson Chung94d67682013-09-25 16:29:40 -07003857 }
3858 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003859 } else {
3860 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003861 }
3862 }
Winson Chung64359a52013-07-08 17:17:08 -07003863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003865 }
3866
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 /**
3868 * Implementation of the method from LauncherModel.Callbacks.
3869 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003870 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003871 Runnable r = new Runnable() {
3872 public void run() {
3873 bindFolders(folders);
3874 }
3875 };
3876 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003877 return;
3878 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003879 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881
3882 /**
3883 * Add the views for a widget to the workspace.
3884 *
3885 * Implementation of the method from LauncherModel.Callbacks.
3886 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003887 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003888 Runnable r = new Runnable() {
3889 public void run() {
3890 bindAppWidget(item);
3891 }
3892 };
3893 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003894 return;
3895 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003896
Daniel Sandler843e8602010-06-07 14:59:01 -04003897 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3898 if (DEBUG_WIDGETS) {
3899 Log.d(TAG, "bindAppWidget: " + item);
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 final Workspace workspace = mWorkspace;
3902
Adam Cohen59400422014-03-05 18:07:04 -08003903 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003904 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003905
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003906 if (!mIsSafeModeEnabled
3907 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003908 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3909
Sunny Goyalff572272014-07-23 13:58:07 -07003910 if (appWidgetInfo == null) {
3911 if (DEBUG_WIDGETS) {
3912 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3913 + " belongs to component " + item.providerName
3914 + ", as the povider is null");
3915 }
3916 LauncherModel.deleteItemFromDatabase(this, item);
3917 return;
3918 }
Sunny Goyalff572272014-07-23 13:58:07 -07003919
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003920 // If we do not have a valid id, try to bind an id.
3921 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3922 // Note: This assumes that the id remap broadcast is received before this step.
3923 // If that is not the case, the id remap will be ignored and user may see the
3924 // click to setup view.
3925 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3926 pendingInfo.spanX = item.spanX;
3927 pendingInfo.spanY = item.spanY;
3928 pendingInfo.minSpanX = item.minSpanX;
3929 pendingInfo.minSpanY = item.minSpanY;
3930 Bundle options = null;
3931 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003932
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003933 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3934 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3935 newWidgetId, appWidgetInfo, options);
3936
3937 // TODO consider showing a permission dialog when the widget is clicked.
3938 if (!success) {
3939 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3940 if (DEBUG_WIDGETS) {
3941 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3942 + " belongs to component " + item.providerName
3943 + ", as the launcher is unable to bing a new widget id");
3944 }
3945 LauncherModel.deleteItemFromDatabase(this, item);
3946 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003947 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003948
3949 item.appWidgetId = newWidgetId;
3950
3951 // If the widget has a configure activity, it is still needs to set it up, otherwise
3952 // the widget is ready to go.
3953 item.restoreStatus = (appWidgetInfo.configure == null)
3954 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3955 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3956
3957 LauncherModel.updateItemInDatabase(this, item);
3958 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3959 && (appWidgetInfo.configure == null)) {
3960 // If the ID is already valid, verify if we need to configure or not.
3961 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3962 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003963 }
Sunny Goyalff572272014-07-23 13:58:07 -07003964 }
3965
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003966 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003967 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003968 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003969 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3970 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003971 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003972
Sunny Goyal56c73602015-09-25 12:55:01 -07003973 // Verify that we own the widget
3974 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
3975 if (info == null || appWidgetInfo == null ||
3976 !info.provider.equals(appWidgetInfo.provider)) {
3977 Log.e(TAG, "Removing invalid widget: id=" + item.appWidgetId);
3978 deleteWidgetInfo(item);
3979 return;
3980 }
3981
Sunny Goyal651077b2014-06-30 14:15:31 -07003982 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003983 item.minSpanX = appWidgetInfo.minSpanX;
3984 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003985 } else {
3986 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003987 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3988 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003989 view.updateIcon(mIconCache);
3990 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003991 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003992 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003996 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997
Adam Cohendcd297f2013-06-18 13:13:40 -07003998 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004000 if (!item.isCustomWidget()) {
4001 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4002 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004003
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 workspace.requestLayout();
4005
Daniel Sandler843e8602010-06-07 14:59:01 -04004006 if (DEBUG_WIDGETS) {
4007 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4008 + (SystemClock.uptimeMillis()-start) + "ms");
4009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 }
4011
Sunny Goyalff572272014-07-23 13:58:07 -07004012 /**
4013 * Restores a pending widget.
4014 *
4015 * @param appWidgetId The app widget id
4016 * @param cellInfo The position on screen where to create the widget.
4017 */
4018 private void completeRestoreAppWidget(final int appWidgetId) {
4019 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4020 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4021 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4022 return;
4023 }
4024
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004025 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004026 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4027
4028 mWorkspace.reinflateWidgetsIfNecessary();
4029 LauncherModel.updateItemInDatabase(this, info);
4030 }
4031
Adam Cohen1462de32012-07-24 22:34:36 -07004032 public void onPageBoundSynchronously(int page) {
4033 mSynchronouslyBoundPages.add(page);
4034 }
4035
Sunny Goyal527c7d32015-08-28 15:19:36 -07004036 @Override
4037 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4038 if (mPendingExecutor != null) {
4039 mPendingExecutor.markCompleted();
4040 }
4041 mPendingExecutor = executor;
4042 executor.attachTo(this);
4043 }
4044
4045 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4046 if (mPendingExecutor == executor) {
4047 mPendingExecutor = null;
4048 }
4049 }
4050
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 /**
4052 * Callback saying that there aren't any more items to bind.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004056 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004057 Runnable r = new Runnable() {
4058 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004059 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004060 }
4061 };
4062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004063 return;
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 if (mSavedState != null) {
4066 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004067 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 mSavedState = null;
4070 }
4071
Adam Cohen1462de32012-07-24 22:34:36 -07004072 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004074 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004075 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004076
4077 // If we received the result of any pending adds while the loader was running (e.g. the
4078 // widget configuration forced an orientation change), process them now.
4079 if (sPendingAddItem != null) {
4080 final long screenId = completeAdd(sPendingAddItem);
4081
4082 // TODO: this moves the user to the page where the pending item was added. Ideally,
4083 // the screen would be guaranteed to exist after bind, and the page would be set through
4084 // the workspace restore process.
4085 mWorkspace.post(new Runnable() {
4086 @Override
4087 public void run() {
4088 mWorkspace.snapToScreenId(screenId);
4089 }
4090 });
4091 sPendingAddItem = null;
4092 }
4093
Sunny Goyal756adbc2015-04-16 15:20:51 -07004094 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004095
4096 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004097 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004098 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004099 }
4100
Adam Cohen3ed316a2014-07-23 18:21:20 -07004101 private void sendLoadingCompleteBroadcastIfNecessary() {
4102 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4103 String permission =
4104 getResources().getString(R.string.receive_first_load_broadcast_permission);
4105 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4106 sendBroadcast(intent, permission);
4107 SharedPreferences.Editor editor = mSharedPrefs.edit();
4108 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4109 editor.apply();
4110 }
4111 }
4112
Winson Chunga0b7e862013-09-05 16:03:15 -07004113 public boolean isAllAppsButtonRank(int rank) {
4114 if (mHotseat != null) {
4115 return mHotseat.isAllAppsButtonRank(rank);
4116 }
4117 return false;
4118 }
4119
Winson Chunga2413752012-04-03 14:22:34 -07004120 private boolean canRunNewAppsAnimation() {
4121 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004122 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4123 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004124 }
4125
Winson Chungc9168342013-06-26 14:54:55 -07004126 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004127 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004128 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4129 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004130 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004131 return bounceAnim;
4132 }
4133
Adam Cohen27772732013-11-11 14:00:56 +00004134 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004135 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004136 }
4137
Tony Wickham3a3517f2015-10-06 11:27:04 -07004138 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004139 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004140 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004141 }
4142
Tony Wickham55616cd2015-09-23 14:55:17 -07004143 public int getSearchBarHeight() {
4144 if (mLauncherCallbacks != null) {
4145 return mLauncherCallbacks.getSearchBarHeight();
4146 }
4147 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4148 }
4149
Winson Chung88fa7412015-08-03 14:25:28 -07004150 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004151 if (mSearchDropTargetBar == null) {
4152 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004153 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004154 if (mQsb != null) {
4155 mSearchDropTargetBar.removeView(mQsb);
4156 mQsb = null;
4157 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004158 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004159 }
4160
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004161 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004162 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4163 * multiple calls to bind the same list.)
4164 */
4165 @Thunk ArrayList<AppInfo> mTmpAppsList;
4166 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4167 public void run() {
4168 bindAllApplications(mTmpAppsList);
4169 mTmpAppsList = null;
4170 }
4171 };
4172
4173 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004174 * Add the icons for all apps.
4175 *
4176 * Implementation of the method from LauncherModel.Callbacks.
4177 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004178 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004179 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4180 mTmpAppsList = apps;
4181 return;
4182 }
4183
Winson Chungb745afb2015-03-02 11:51:23 -08004184 if (mAppsView != null) {
4185 mAppsView.setApps(apps);
4186 }
Adam Cohen9211d422014-10-07 18:14:53 -07004187 if (mLauncherCallbacks != null) {
4188 mLauncherCallbacks.bindAllApplications(apps);
4189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004190 }
4191
4192 /**
4193 * A package was updated.
4194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004197 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004198 Runnable r = new Runnable() {
4199 public void run() {
4200 bindAppsUpdated(apps);
4201 }
4202 };
4203 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004204 return;
4205 }
4206
Winson Chungb745afb2015-03-02 11:51:23 -08004207 if (mAppsView != null) {
4208 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004209 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004210 }
4211
Sunny Goyal4390ace2014-10-13 11:33:11 -07004212 @Override
4213 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4214 Runnable r = new Runnable() {
4215 public void run() {
4216 bindWidgetsRestored(widgets);
4217 }
4218 };
4219 if (waitUntilResume(r)) {
4220 return;
4221 }
4222 mWorkspace.widgetsRestored(widgets);
4223 }
4224
Joe Onorato9c1289c2009-08-17 11:03:03 -04004225 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004226 * Some shortcuts were updated in the background.
4227 *
4228 * Implementation of the method from LauncherModel.Callbacks.
4229 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004230 @Override
4231 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4232 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004233 Runnable r = new Runnable() {
4234 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004235 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004236 }
4237 };
4238 if (waitUntilResume(r)) {
4239 return;
4240 }
4241
Sunny Goyal4390ace2014-10-13 11:33:11 -07004242 if (!updated.isEmpty()) {
4243 mWorkspace.updateShortcuts(updated);
4244 }
4245
4246 if (!removed.isEmpty()) {
4247 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4248 for (ShortcutInfo si : removed) {
4249 removedComponents.add(si.getTargetComponent());
4250 }
4251 mWorkspace.removeItemsByComponentName(removedComponents, user);
4252 // Notify the drag controller
4253 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004254 }
4255 }
4256
4257 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004258 * Update the state of a package, typically related to install state.
4259 *
4260 * Implementation of the method from LauncherModel.Callbacks.
4261 */
Sunny Goyale755d462014-07-22 13:48:29 -07004262 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004263 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4264 Runnable r = new Runnable() {
4265 public void run() {
4266 bindRestoreItemsChange(updates);
4267 }
4268 };
4269 if (waitUntilResume(r)) {
4270 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004271 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004272
Sunny Goyal756adbc2015-04-16 15:20:51 -07004273 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004274 }
4275
4276 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004277 * A package was uninstalled. We take both the super set of packageNames
4278 * in addition to specific applications to remove, the reason being that
4279 * this can be called when a package is updated as well. In that scenario,
4280 * we only remove specific components from the workspace, where as
4281 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004282 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004283 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004284 * Implementation of the method from LauncherModel.Callbacks.
4285 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004286 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004287 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004288 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004289 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004290 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004291 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004292 }
Winson Chungd64d1762013-08-20 14:37:16 -07004293 };
4294 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004295 return;
4296 }
4297
Sunny Goyal1a745e82014-10-02 15:58:31 -07004298 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004299 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4300 for (AppInfo info : appInfos) {
4301 removedComponents.add(info.componentName);
4302 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004303 if (!packageNames.isEmpty()) {
4304 mWorkspace.removeItemsByPackageName(packageNames, user);
4305 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004306 if (!removedComponents.isEmpty()) {
4307 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004308 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004309 // Notify the drag controller
4310 mDragController.onAppsRemoved(packageNames, removedComponents);
4311
Sunny Goyal1a745e82014-10-02 15:58:31 -07004312 } else {
4313 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004314 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004315
Winson Chungdf95eb12013-10-16 14:57:07 -07004316 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004317 if (mAppsView != null) {
4318 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 }
Joe Onoratobe386092009-11-17 17:32:16 -08004321
Michael Jurkac402cd92013-05-20 15:49:32 +02004322 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004323 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004324 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004325 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004326 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004327
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004328 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004329 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004330 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004331 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004332 return;
4333 }
4334
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004335 if (mWidgetsView != null && model != null) {
4336 mWidgetsView.addWidgets(model);
4337 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004338 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004339 }
4340
Winson Chung400438b2011-01-16 17:53:48 -08004341 private int mapConfigurationOriActivityInfoOri(int configOri) {
4342 final Display d = getWindowManager().getDefaultDisplay();
4343 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4344 switch (d.getRotation()) {
4345 case Surface.ROTATION_0:
4346 case Surface.ROTATION_180:
4347 // We are currently in the same basic orientation as the natural orientation
4348 naturalOri = configOri;
4349 break;
4350 case Surface.ROTATION_90:
4351 case Surface.ROTATION_270:
4352 // We are currently in the other basic orientation to the natural orientation
4353 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4354 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4355 break;
4356 }
4357
4358 int[] oriMap = {
4359 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4360 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4361 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4362 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4363 };
4364 // Since the map starts at portrait, we need to offset if this device's natural orientation
4365 // is landscape.
4366 int indexOffset = 0;
4367 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4368 indexOffset = 1;
4369 }
4370 return oriMap[(d.getRotation() + indexOffset) % 4];
4371 }
Adam Cohen446e9402011-09-15 18:21:21 -07004372
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004373 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004374 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004375 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004376 if (Utilities.ATLEAST_JB_MR2) {
4377 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4378 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004379 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4380 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004381 }
Winson Chung4b919f82012-05-01 10:44:08 -07004382 }
4383 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004384
Winson Chung4b919f82012-05-01 10:44:08 -07004385 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004386 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004387 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004388 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004389 } else {
4390 mHandler.postDelayed(new Runnable() {
4391 public void run() {
4392 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4393 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004394 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004395 }
Winson Chung4b919f82012-05-01 10:44:08 -07004396 }
Winson Chung400438b2011-01-16 17:53:48 -08004397 }
4398
Winson Chunge43a1e72014-01-15 10:33:02 -08004399 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004400 if (mLauncherCallbacks != null) {
4401 return mLauncherCallbacks.isLauncherPreinstalled();
4402 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004403 PackageManager pm = getPackageManager();
4404 try {
4405 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4406 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4407 return true;
4408 } else {
4409 return false;
4410 }
4411 } catch (NameNotFoundException e) {
4412 e.printStackTrace();
4413 return false;
4414 }
4415 }
4416
Adam Cohenea90f832014-05-21 15:03:34 -07004417 /**
4418 * This method indicates whether or not we should suggest default wallpaper dimensions
4419 * when our wallpaper cropper was not yet used to set a wallpaper.
4420 */
4421 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004422 if (mLauncherCallbacks != null) {
4423 return mLauncherCallbacks.overrideWallpaperDimensions();
4424 }
Adam Cohenea90f832014-05-21 15:03:34 -07004425 return true;
4426 }
4427
Adam Cohen432609a2014-03-13 17:03:22 -07004428 /**
4429 * To be overridden by subclasses to indicate that there is an activity to launch
4430 * before showing the standard launcher experience.
4431 */
4432 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004433 if (mLauncherCallbacks != null) {
4434 return mLauncherCallbacks.hasFirstRunActivity();
4435 }
Adam Cohen432609a2014-03-13 17:03:22 -07004436 return false;
4437 }
4438
4439 /**
4440 * To be overridden by subclasses to launch any first run activity
4441 */
4442 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004443 if (mLauncherCallbacks != null) {
4444 return mLauncherCallbacks.getFirstRunActivity();
4445 }
Adam Cohen432609a2014-03-13 17:03:22 -07004446 return null;
4447 }
4448
Winson Chunga6945242014-01-08 14:04:34 -08004449 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004450 return !ActivityManager.isRunningInTestHarness() &&
4451 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004452 }
4453
Adam Cohen4a9423d2014-03-28 14:22:31 -07004454 protected boolean hasRunFirstRunActivity() {
4455 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4456 }
4457
Adam Cohen432609a2014-03-13 17:03:22 -07004458 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004459 if (shouldRunFirstRunActivity() &&
4460 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004461 Intent firstRunIntent = getFirstRunActivity();
4462 if (firstRunIntent != null) {
4463 startActivity(firstRunIntent);
4464 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004465 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004466 }
4467 }
Adam Cohen432609a2014-03-13 17:03:22 -07004468 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004469 }
4470
4471 private void markFirstRunActivityShown() {
4472 SharedPreferences.Editor editor = mSharedPrefs.edit();
4473 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4474 editor.apply();
4475 }
4476
Adam Cohen432609a2014-03-13 17:03:22 -07004477 /**
4478 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4479 * screen that must be displayed and dismissed.
4480 */
4481 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004482 if (mLauncherCallbacks != null) {
4483 return mLauncherCallbacks.hasDismissableIntroScreen();
4484 }
Adam Cohen432609a2014-03-13 17:03:22 -07004485 return false;
4486 }
4487
4488 /**
4489 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4490 */
4491 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004492 if (mLauncherCallbacks != null) {
4493 return mLauncherCallbacks.getIntroScreen();
4494 }
Adam Cohen432609a2014-03-13 17:03:22 -07004495 return null;
4496 }
4497
4498 /**
4499 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4500 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4501 */
4502 private boolean shouldShowIntroScreen() {
4503 return hasDismissableIntroScreen() &&
4504 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4505 }
4506
4507 protected void showIntroScreen() {
4508 View introScreen = getIntroScreen();
4509 changeWallpaperVisiblity(false);
4510 if (introScreen != null) {
4511 mDragLayer.showOverlayView(introScreen);
4512 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004513 if (mLauncherOverlayContainer != null) {
4514 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4515 }
Adam Cohen432609a2014-03-13 17:03:22 -07004516 }
4517
4518 public void dismissIntroScreen() {
4519 markIntroScreenDismissed();
4520 if (showFirstRunActivity()) {
4521 // We delay hiding the intro view until the first run activity is showing. This
4522 // avoids a blip.
4523 mWorkspace.postDelayed(new Runnable() {
4524 @Override
4525 public void run() {
4526 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004527 if (mLauncherOverlayContainer != null) {
4528 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4529 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004530 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004531 }
4532 }, ACTIVITY_START_DELAY);
4533 } else {
4534 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004535 if (mLauncherOverlayContainer != null) {
4536 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4537 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004538 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004539 }
4540 changeWallpaperVisiblity(true);
4541 }
4542
4543 private void markIntroScreenDismissed() {
4544 SharedPreferences.Editor editor = mSharedPrefs.edit();
4545 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4546 editor.apply();
4547 }
4548
Adam Cohen091440a2015-03-18 14:16:05 -07004549 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004550 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4551 // on the device, then we always show the first run cling experience (or if there is no
4552 // launcher2). Otherwise, we prompt the user upon started for migration
4553 LauncherClings launcherClings = new LauncherClings(this);
4554 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004555 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004556 if (mModel.canMigrateFromOldLauncherDb(this)) {
4557 launcherClings.showMigrationCling();
4558 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004559 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004560 }
4561 }
4562 }
4563
Winson Chunga6945242014-01-08 14:04:34 -08004564 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004565 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4566 if (mHotseat != null) mHotseat.setAlpha(1f);
4567 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004568 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4569 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004570 }
4571
4572 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004573 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4574 if (mHotseat != null) mHotseat.setAlpha(0f);
4575 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004576 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4577 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004578 }
4579
Winson Chung5ffd51d2015-06-25 11:36:50 -07004580 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004581 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004582 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004583 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004584 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004585 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004586 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4587 if (userHandle != null) {
4588 user = UserHandleCompat.fromUser(userHandle);
4589 }
4590 }
4591 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004592 }
4593
4594 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004595 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004596 if (user == null) {
4597 user = UserHandleCompat.myUserHandle();
4598 }
4599
4600 // Called from search suggestion, add the profile extra to the intent to ensure that we
4601 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004602 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004603 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004604 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004605 return null;
4606 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004607 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004608 }
4609
Winson Chung5ffd51d2015-06-25 11:36:50 -07004610 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004611 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4612 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004613 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004614 UserHandleCompat.myUserHandle());
4615 }
4616
Winson Chung5ffd51d2015-06-25 11:36:50 -07004617 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004618 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4619 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004620 mWorkspace.onExternalDragStartedWithItem(dragView);
4621 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004622 }
4623
Winson Chung5ffd51d2015-06-25 11:36:50 -07004624 protected void moveWorkspaceToDefaultScreen() {
4625 mWorkspace.moveToDefaultScreen(false);
4626 }
4627
Anjali Koppalf5d29132014-02-28 13:33:27 -08004628 @Override
4629 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004630 if (mLauncherCallbacks != null) {
4631 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4632 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004633 }
4634
Winson Chung80baf5a2010-08-09 16:03:15 -07004635 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004636 * Returns a FastBitmapDrawable with the icon, accurately sized.
4637 */
4638 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4639 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4640 d.setFilterBitmap(true);
4641 resizeIconDrawable(d);
4642 return d;
4643 }
4644
4645 /**
4646 * Resizes an icon drawable to the correct icon size.
4647 */
Winson5fbe0742015-09-30 15:33:00 -07004648 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004649 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004650 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004651 }
4652
4653 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004654 * Prints out out state for debugging.
4655 */
4656 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004657 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004658 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004659 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4660 Log.d(TAG, "mRestoring=" + mRestoring);
4661 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004662 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004663 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004664 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004665
Daniel Sandler325dc232013-06-05 22:57:57 -04004666 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004667 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004668
4669 @Override
4670 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4671 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004672 // Dump workspace
4673 writer.println(prefix + "Workspace Items");
4674 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4675 writer.println(prefix + " Homescreen " + i);
4676
4677 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4678 for (int j = 0; j < layout.getChildCount(); j++) {
4679 Object tag = layout.getChildAt(j).getTag();
4680 if (tag != null) {
4681 writer.println(prefix + " " + tag.toString());
4682 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004683 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004684 }
Sunny Goyala1365452015-10-01 15:46:24 -07004685
4686 writer.println(prefix + " Hotseat");
4687 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4688 for (int j = 0; j < layout.getChildCount(); j++) {
4689 Object tag = layout.getChildAt(j).getTag();
4690 if (tag != null) {
4691 writer.println(prefix + " " + tag.toString());
4692 }
4693 }
4694
4695 synchronized (sDumpLogs) {
4696 writer.println();
4697 writer.println(prefix + "Debug logs");
4698 for (String log : sDumpLogs) {
4699 writer.println(prefix + " " + log);
4700 }
4701 }
4702
Adam Cohen9211d422014-10-07 18:14:53 -07004703 if (mLauncherCallbacks != null) {
4704 mLauncherCallbacks.dump(prefix, fd, writer, args);
4705 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004706 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004707
Sunny Goyala1365452015-10-01 15:46:24 -07004708 public static void addDumpLog(String tag, String log) {
4709 Log.d(tag, log);
4710 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004711 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004712 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004713 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004714 }
4715
Adam Cohen59400422014-03-05 18:07:04 -08004716 public static CustomAppWidget getCustomAppWidget(String name) {
4717 return sCustomAppWidgets.get(name);
4718 }
4719
4720 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4721 return sCustomAppWidgets;
4722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004723}
Michael Jurka2763be32011-02-24 11:19:57 -08004724
Dan Sandlerd5024042014-01-09 15:01:33 -05004725interface DebugIntents {
4726 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4727 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004728}