blob: 0816d46118b93a7d164ac486bd59a332ba60c3a5 [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;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
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 ?
412 app.getInvariantDeviceProfile().landscapeProfile
413 : app.getInvariantDeviceProfile().portraitProfile;
414
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.
Adam Cohen3d509322012-06-06 14:10:04 -0700993 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -0700994 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700995
Michael Jurka447bf842013-05-15 14:52:15 +0200996 if (DEBUG_RESUME_TIME) {
997 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
998 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700999
Adam Cohen4b66bf32015-09-18 12:15:19 -07001000 // We want to suppress callbacks about CustomContent being shown if we have just received
1001 // onNewIntent while the user was present within launcher. In that case, we post a call
1002 // to move the user to the main screen (which will occur after onResume). We don't want to
1003 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1004 // suppress here.
1005 if (mWorkspace.getCustomContentCallbacks() != null
1006 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001007 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001008 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001009 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1010 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001011 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001012 }
1013 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001014 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001015 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001016 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001017
Sunny Goyal756adbc2015-04-16 15:20:51 -07001018 if (!isWorkspaceLoading()) {
1019 // Process any items that were added while Launcher was away.
1020 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1021 }
Adam Cohen9211d422014-10-07 18:14:53 -07001022
1023 if (mLauncherCallbacks != null) {
1024 mLauncherCallbacks.onResume();
1025 }
Adam Cohen06dff352012-06-01 17:17:08 -07001026 }
1027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001030 // Ensure that items added to Launcher are queued until Launcher returns
1031 InstallShortcutReceiver.enableInstallQueue();
1032
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001033 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001034 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001035 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001036 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001037
1038 // We call onHide() aggressively. The custom content callbacks should be able to
1039 // debounce excess onHide calls.
1040 if (mWorkspace.getCustomContentCallbacks() != null) {
1041 mWorkspace.getCustomContentCallbacks().onHide();
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Adam Cohen9211d422014-10-07 18:14:53 -07001044 if (mLauncherCallbacks != null) {
1045 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001046 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001047 }
1048
1049 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001050 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1051 // by a onResume or by scrolling otherwise.
1052 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001053
1054 // Custom content is completely hidden
1055 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001056
1057 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1058 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001059
1060 // Indicates whether the user is allowed to scroll away from the custom content.
1061 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
1063
Adam Cohenc2d6e892014-10-16 09:49:24 -07001064 public interface LauncherOverlay {
1065
1066 /**
1067 * Touch interaction leading to overscroll has begun
1068 */
1069 public void onScrollInteractionBegin();
1070
1071 /**
1072 * Touch interaction related to overscroll has ended
1073 */
1074 public void onScrollInteractionEnd();
1075
1076 /**
1077 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1078 * screen (or in the case of RTL, the rightmost screen).
1079 */
1080 public void onScrollChange(int progress, boolean rtl);
1081
1082 /**
1083 * Screen has stopped scrolling
1084 */
1085 public void onScrollSettled();
1086
1087 /**
1088 * This method can be called by the Launcher in order to force the LauncherOverlay
1089 * to exit fully immersive mode.
1090 */
1091 public void forceExitFullImmersion();
1092 }
1093
Jun Mukai8af0cd82015-05-12 12:32:12 -07001094 public interface LauncherSearchCallbacks {
1095 /**
1096 * Called when the search overlay is shown.
1097 */
1098 public void onSearchOverlayOpened();
1099
1100 /**
1101 * Called when the search overlay is dismissed.
1102 */
1103 public void onSearchOverlayClosed();
1104 }
1105
Adam Cohenc2d6e892014-10-16 09:49:24 -07001106 public interface LauncherOverlayCallbacks {
1107 /**
1108 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1109 * however it doesn't modify any state within the launcher.
1110 */
1111 public boolean canEnterFullImmersion();
1112
1113 /**
1114 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1115 * eg. by occupying the full screen and handling all touch events.
1116 *
1117 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1118 * case, Launcher will modify any necessary state and assumes the overlay is
1119 * handling all interaction. If false, the LauncherOverlay should cancel any
1120 *
1121 */
1122 public boolean enterFullImmersion();
1123
1124 /**
1125 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1126 * full control over UI and state.
1127 */
1128 public void exitFullImmersion();
1129 }
1130
1131 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1132
1133 @Override
1134 public boolean canEnterFullImmersion() {
1135 return mState == State.WORKSPACE;
1136 }
1137
1138 @Override
1139 public boolean enterFullImmersion() {
1140 if (mState == State.WORKSPACE) {
1141 // When fully immersed, disregard any touches which fall through.
1142 mDragLayer.setBlockTouch(true);
1143 return true;
1144 }
1145 return false;
1146 }
1147
1148 @Override
1149 public void exitFullImmersion() {
1150 mDragLayer.setBlockTouch(false);
1151 }
1152 }
1153
Jorim Jaggid017f882014-01-14 17:08:48 -08001154 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001155 if (mLauncherCallbacks != null) {
1156 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001157 } else {
1158 // On devices with a locked orientation, we will at least have the allow rotation
1159 // setting.
1160 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001161 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001162 }
1163
Adam Cohen9211d422014-10-07 18:14:53 -07001164 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001165 CustomContentCallbacks callbacks, String description) {
1166 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001167 }
1168
Adam Cohenedb40762013-07-18 16:45:45 -07001169 // The custom content needs to offset its content to account for the QSB
1170 public int getTopOffsetForCustomContent() {
1171 return mWorkspace.getPaddingTop();
1172 }
1173
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001174 @Override
1175 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001177 if (mModel.isCurrentCallbacks(this)) {
1178 mModel.stopLoader();
1179 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001180 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1181
Romain Guy13c2e7b2010-03-10 19:45:00 -08001182 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001183 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001186 @Override
1187 public void onWindowFocusChanged(boolean hasFocus) {
1188 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001189 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001190
1191 if (mLauncherCallbacks != null) {
1192 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1193 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 private boolean acceptFilter() {
1197 final InputMethodManager inputManager = (InputMethodManager)
1198 getSystemService(Context.INPUT_METHOD_SERVICE);
1199 return !inputManager.isFullscreenMode();
1200 }
1201
1202 @Override
1203 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001204 final int uniChar = event.getUnicodeChar();
1205 final boolean handled = super.onKeyDown(keyCode, event);
1206 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1207 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1209 keyCode, event);
1210 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001211 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001212 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001213 // showSearchDialog()
1214 // If there are multiple keystrokes before the search dialog takes focus,
1215 // onSearchRequested() will be called for every keystroke,
1216 // but it is idempotent, so it's fine.
1217 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219 }
1220
Joe Onorato8a9625e2010-01-28 15:55:35 -08001221 // Eat the long press event so the keyboard doesn't come up.
1222 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1223 return true;
1224 }
1225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 return handled;
1227 }
1228
Winson46163472015-09-22 17:31:56 -07001229 @Override
1230 public boolean onKeyUp(int keyCode, KeyEvent event) {
1231 if (keyCode == KeyEvent.KEYCODE_MENU) {
1232 // Ignore the menu key if we are currently dragging or are on the custom content screen
1233 if (!isOnCustomContent() && !mDragController.isDragging()) {
1234 // Close any open folders
1235 closeFolder();
1236
1237 // Stop resizing any widgets
1238 mWorkspace.exitWidgetResizeMode();
1239
1240 // Show the overview mode if we are on the workspace
1241 if (mState == State.WORKSPACE && !mWorkspace.isInOverviewMode() &&
1242 !mWorkspace.isSwitchingState()) {
1243 mOverviewPanel.requestFocus();
1244 showOverviewMode(true);
1245 }
1246 }
1247 return true;
1248 }
1249 return super.onKeyUp(keyCode, event);
1250 }
1251
Karl Rosaen138a0412009-04-23 19:00:21 -07001252 private String getTypedText() {
1253 return mDefaultKeySsb.toString();
1254 }
1255
1256 private void clearTypedText() {
1257 mDefaultKeySsb.clear();
1258 mDefaultKeySsb.clearSpans();
1259 Selection.setSelection(mDefaultKeySsb, 0);
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001263 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1264 * State
1265 */
1266 private static State intToState(int stateOrdinal) {
1267 State state = State.WORKSPACE;
1268 final State[] stateValues = State.values();
1269 for (int i = 0; i < stateValues.length; i++) {
1270 if (stateValues[i].ordinal() == stateOrdinal) {
1271 state = stateValues[i];
1272 break;
1273 }
1274 }
1275 return state;
1276 }
1277
1278 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 * Restores the previous state, if it exists.
1280 *
1281 * @param savedState The previous state.
1282 */
1283 private void restoreState(Bundle savedState) {
1284 if (savedState == null) {
1285 return;
1286 }
1287
Michael Jurka883f55b2010-10-21 15:47:14 -07001288 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001289 if (state == State.APPS || state == State.WIDGETS) {
1290 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001291 }
1292
Adam Cohen21cd0022013-10-09 18:57:02 -07001293 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1294 PagedView.INVALID_RESTORE_PAGE);
1295 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001296 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
Sunny Goyal756cd262015-08-20 12:33:21 -07001299 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1300 if (itemValues != null) {
1301 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001302 AppWidgetProviderInfo info = savedState.getParcelable(
1303 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001304 mPendingAddWidgetInfo = info == null ?
1305 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1306
Adam Cohen4637b5a2013-11-04 18:21:24 -08001307 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001308 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 mRestoring = true;
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
1313 /**
1314 * Finds all the views we need and configure them properly.
1315 */
1316 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001317 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001318
Craig Mautner360310b2012-10-26 15:13:08 -07001319 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001320 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001321 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1322 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001323 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001324 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001325
John Spurlock77e1f472013-09-11 10:09:51 -04001326 mLauncherView.setSystemUiVisibility(
1327 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001328 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329
Winson Chung4c98d922011-05-31 16:50:48 -07001330 // Setup the drag layer
1331 mDragLayer.setup(this, dragController);
1332
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 // Setup the hotseat
1334 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1335 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001336 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 }
1338
Jorim Jaggid017f882014-01-14 17:08:48 -08001339 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Tony Wickhamc75917f2015-09-02 16:11:09 -07001340 // Long-clicking buttons in the overview panel does the same thing as clicking them.
1341 OnLongClickListener performClickOnLongClick = new OnLongClickListener() {
1342 @Override
1343 public boolean onLongClick(View v) {
1344 return v.performClick();
1345 }
1346 };
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001347 mWidgetsButton = findViewById(R.id.widget_button);
1348 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001349 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001350 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001351 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001352 onClickAddWidgetButton(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001353 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001354 }
1355 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001356 mWidgetsButton.setOnLongClickListener(performClickOnLongClick);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001357 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001358
1359 View wallpaperButton = findViewById(R.id.wallpaper_button);
1360 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001361 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001362 public void onClick(View view) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001363 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001364 onClickWallpaperPicker(view);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001365 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001366 }
1367 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001368 wallpaperButton.setOnLongClickListener(performClickOnLongClick);
Adam Cohen61f560d2013-09-30 15:58:20 -07001369 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370
1371 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001372 if (hasSettings()) {
1373 settingsButton.setOnClickListener(new OnClickListener() {
1374 @Override
Tony Wickhamc75917f2015-09-02 16:11:09 -07001375 public void onClick(View view) {
Jorim Jaggid017f882014-01-14 17:08:48 -08001376 if (!mWorkspace.isSwitchingState()) {
Tony Wickhamc75917f2015-09-02 16:11:09 -07001377 onClickSettingsButton(view);
Jorim Jaggid017f882014-01-14 17:08:48 -08001378 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001379 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001380 });
Tony Wickhamc75917f2015-09-02 16:11:09 -07001381 settingsButton.setOnLongClickListener(performClickOnLongClick);
Jorim Jaggid017f882014-01-14 17:08:48 -08001382 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1383 } else {
1384 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001385 }
1386
Adam Cohendbdff6b2013-09-18 19:09:15 -07001387 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388
Winson Chung4c98d922011-05-31 16:50:48 -07001389 // Setup the workspace
1390 mWorkspace.setHapticFeedbackEnabled(false);
1391 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001392 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001393 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001394
Tony Wickham34d2c912015-09-11 09:27:58 -07001395 // Get the search/delete/uninstall bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001396 mSearchDropTargetBar = (SearchDropTargetBar)
1397 mDragLayer.findViewById(R.id.search_drop_target_bar);
Tony Wickham34d2c912015-09-11 09:27:58 -07001398 // Get the app info bar
1399 mAppInfoDropTargetBar = (AppInfoDropTargetBar)
1400 mDragLayer.findViewById(R.id.app_info_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001401
Winson Chungef7f8742015-06-04 17:18:17 -07001402 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001403 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001404 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001405 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1406 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1407 } else {
1408 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1409 }
Craig Mautner360310b2012-10-26 15:13:08 -07001410
Winson Chung3d503fb2011-07-13 17:25:49 -07001411 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001412 dragController.setDragScoller(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001413 dragController.setMoveTarget(mWorkspace);
1414 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001415 if (mSearchDropTargetBar != null) {
1416 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001417 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001418 }
Tony Wickham34d2c912015-09-11 09:27:58 -07001419 if (mAppInfoDropTargetBar != null) {
1420 mAppInfoDropTargetBar.setup(this, dragController);
1421 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001422
Sunny Goyal322d5562015-06-25 19:35:49 -07001423 if (TestingUtils.MEMORY_DUMP_ENABLED) {
1424 TestingUtils.addWeightWatcher(this);
Daniel Sandler924b9932013-06-12 00:38:25 -04001425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
1427
1428 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001429 * Sets the all apps button. This method is called from {@link Hotseat}.
1430 */
1431 public void setAllAppsButton(View allAppsButton) {
1432 mAllAppsButton = allAppsButton;
1433 }
1434
1435 public View getAllAppsButton() {
1436 return mAllAppsButton;
1437 }
1438
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001439 public View getWidgetsButton() {
1440 return mWidgetsButton;
1441 }
1442
Anjali Koppal5ad44842014-03-10 20:34:39 -07001443 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 * Creates a view representing a shortcut.
1445 *
1446 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001448 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001449 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
1452 /**
1453 * Creates a view representing a shortcut inflated from the specified resource.
1454 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 * @param parent The group the shortcut belongs to.
1456 * @param info The data structure describing the shortcut.
1457 *
1458 * @return A View inflated from layoutResId.
1459 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001460 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001461 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001462 parent, false);
1463 favorite.applyFromShortcutInfo(info, mIconCache);
1464 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001466 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 return favorite;
1468 }
1469
1470 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 * Add a shortcut to the workspace.
1472 *
1473 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001475 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001476 int cellY) {
1477 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001478 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001479
Sunny Goyal5c97f512015-05-19 16:03:28 -07001480 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001481 if (info == null) {
1482 return;
1483 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001484 final View view = createShortcut(info);
1485
Sunny Goyal5c97f512015-05-19 16:03:28 -07001486 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001487 // First we check if we already know the exact location where we want to add this item.
1488 if (cellX >= 0 && cellY >= 0) {
1489 cellXY[0] = cellX;
1490 cellXY[1] = cellY;
1491 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001492
1493 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001494 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001495 true, null,null)) {
1496 return;
1497 }
1498 DragObject dragObject = new DragObject();
1499 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001500 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1501 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001502 return;
1503 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001504 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001506 }
1507
1508 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001509 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 return;
1511 }
1512
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001513 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
1515 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
1519 }
1520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001522 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001524 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 */
Adam Cohen091440a2015-03-18 14:16:05 -07001526 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001527 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1528
1529 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001531 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1532 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 }
Romain Guycbb89e42009-06-08 15:52:54 -07001534
Adam Cohen59400422014-03-05 18:07:04 -08001535 if (appWidgetInfo.isCustomWidget) {
1536 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001537 }
1538
Adam Cohen59400422014-03-05 18:07:04 -08001539 LauncherAppWidgetInfo launcherInfo;
1540 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1541 launcherInfo.spanX = info.spanX;
1542 launcherInfo.spanY = info.spanY;
1543 launcherInfo.minSpanX = info.minSpanX;
1544 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001545 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001548 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (hostView == null) {
1552 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001553 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1554 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 } else {
1556 // The AppWidgetHostView has already been inflated and instantiated
1557 launcherInfo.hostView = hostView;
1558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001560 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001561 launcherInfo.notifyWidgetSizeChanged(this);
1562
Adam Cohen59400422014-03-05 18:07:04 -08001563 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1564 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001565
1566 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001568 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1572 @Override
1573 public void onReceive(Context context, Intent intent) {
1574 final String action = intent.getAction();
1575 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1576 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001577 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001578 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001579
Winson Chungc100e8e2011-08-09 16:02:43 -07001580 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001581 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001582 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001583 mPendingAddInfo.container == ItemInfo.NO_ID) {
Winson5c6bdbb2015-09-03 11:36:19 -07001584 if (!showWorkspace(false)) {
1585 // If we are already on the workspace, then manually reset all apps
1586 mAppsView.reset();
1587 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001588 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001589 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1590 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001591 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001592 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1593 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001594 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001595 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1596 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1597 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001598 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001599 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1600 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 }
1602 }
1603 };
1604
1605 @Override
1606 public void onAttachedToWindow() {
1607 super.onAttachedToWindow();
1608
1609 // Listen for broadcasts related to user-presence
1610 final IntentFilter filter = new IntentFilter();
1611 filter.addAction(Intent.ACTION_SCREEN_OFF);
1612 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001613 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001614 if (ENABLE_DEBUG_INTENTS) {
1615 filter.addAction(DebugIntents.DELETE_DATABASE);
1616 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1617 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001618 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001619 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001620 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001621 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 mVisible = true;
1623 }
1624
Adam Cohen0b395352014-06-09 22:54:36 +00001625 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001626 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001627 * This method is a no-op for other platform versions.
1628 */
Sunny Goyald0091012015-01-20 15:55:34 -08001629 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001630 private void setupTransparentSystemBarsForLollipop() {
1631 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001632 Window window = getWindow();
1633 window.getAttributes().systemUiVisibility |=
1634 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1635 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1636 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1637 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1638 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1639 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1640 window.setStatusBarColor(Color.TRANSPARENT);
1641 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001642 }
1643 }
1644
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 @Override
1646 public void onDetachedFromWindow() {
1647 super.onDetachedFromWindow();
1648 mVisible = false;
1649
Adam Cohend113e0c2010-11-11 10:48:05 -08001650 if (mAttached) {
1651 unregisterReceiver(mReceiver);
1652 mAttached = false;
1653 }
Winson Chungb745afb2015-03-02 11:51:23 -08001654 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 }
1656
1657 public void onWindowVisibilityChanged(int visibility) {
1658 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001660 // The following code used to be in onResume, but it turns out onResume is called when
1661 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1662 // is a more appropriate event to handle
1663 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001664 if (!mWorkspaceLoading) {
1665 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 // We want to let Launcher draw itself at least once before we force it to build
1667 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001668 // apps is nice and speedy.
1669 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001671 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001672 if (mStarted) return;
1673 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001674 // We delay the layer building a bit in order to give
1675 // other message processing a time to run. In particular
1676 // this avoids a delay in hiding the IME if it was
1677 // currently shown, because doing that may involve
1678 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001679 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001680 final ViewTreeObserver.OnDrawListener listener = this;
1681 mWorkspace.post(new Runnable() {
1682 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001683 if (mWorkspace != null &&
1684 mWorkspace.getViewTreeObserver() != null) {
1685 mWorkspace.getViewTreeObserver().
1686 removeOnDrawListener(listener);
1687 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001688 }
1689 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001690 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001691 }
1692 });
1693 }
1694 clearTypedText();
1695 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 }
1697
Adam Cohen091440a2015-03-18 14:16:05 -07001698 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 mHandler.removeMessages(ADVANCE_MSG);
1700 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1701 mHandler.sendMessageDelayed(msg, delay);
1702 mAutoAdvanceSentTime = System.currentTimeMillis();
1703 }
1704
Adam Cohen091440a2015-03-18 14:16:05 -07001705 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1707 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1708 mAutoAdvanceRunning = autoAdvanceRunning;
1709 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001710 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 sendAdvanceMessage(delay);
1712 } else {
1713 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001714 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1716 }
1717 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001718 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 }
1720 }
1721 }
1722
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001723 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1724
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001726 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 if (msg.what == ADVANCE_MSG) {
1728 int i = 0;
1729 for (View key: mWidgetsToAdvance.keySet()) {
1730 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 public void run() {
1735 ((Advanceable) v).advance();
1736 }
1737 }, delay);
1738 }
1739 i++;
1740 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001741 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001745 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001746
Winsonc0b52fe2015-09-09 16:38:15 -07001747 private void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001748 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1750 if (v instanceof Advanceable) {
1751 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001752 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001753 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755 }
1756
Winsonc0b52fe2015-09-09 16:38:15 -07001757 private void removeWidgetToAutoAdvance(View hostView) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 if (mWidgetsToAdvance.containsKey(hostView)) {
1759 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001760 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001762 }
1763
Hyunyoung Song3f471442015-04-08 19:01:34 -07001764 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001765 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1766 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768
Winson Chunga6945242014-01-08 14:04:34 -08001769 public DragLayer getDragLayer() {
1770 return mDragLayer;
1771 }
1772
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001773 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001774 return mAppsView;
1775 }
1776
Hyunyoung Song3f471442015-04-08 19:01:34 -07001777 public WidgetsContainerView getWidgetsView() {
1778 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public Workspace getWorkspace() {
1782 return mWorkspace;
1783 }
1784
1785 public Hotseat getHotseat() {
1786 return mHotseat;
1787 }
1788
Jorim Jaggid017f882014-01-14 17:08:48 -08001789 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001790 return mOverviewPanel;
1791 }
1792
Winson Chung006ee262015-08-03 14:40:11 -07001793 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001794 return mSearchDropTargetBar;
1795 }
1796
Tony Wickham34d2c912015-09-11 09:27:58 -07001797 public AppInfoDropTargetBar getAppInfoDropTargetBar() {
1798 return mAppInfoDropTargetBar;
1799 }
1800
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001801 public LauncherAppWidgetHost getAppWidgetHost() {
1802 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 }
Romain Guycbb89e42009-06-08 15:52:54 -07001804
Winson Chunga9abd0e2010-10-27 17:18:37 -07001805 public LauncherModel getModel() {
1806 return mModel;
1807 }
1808
Winson Chunga6945242014-01-08 14:04:34 -08001809 protected SharedPreferences getSharedPrefs() {
1810 return mSharedPrefs;
1811 }
1812
Adam Cohen2e6da152015-05-06 11:42:25 -07001813 public DeviceProfile getDeviceProfile() {
1814 return mDeviceProfile;
1815 }
1816
Bjorn Bringertc459e522013-06-07 19:36:01 +01001817 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001818 getWindow().closeAllPanels();
1819
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001820 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001821 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001822 }
1823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 @Override
1825 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001826 long startTime = 0;
1827 if (DEBUG_RESUME_TIME) {
1828 startTime = System.currentTimeMillis();
1829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 super.onNewIntent(intent);
1831
1832 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001833 Folder openFolder = mWorkspace.getOpenFolder();
1834 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1835 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1836 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1837 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1838 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001839 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001840 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001841
Adam Cohen6fecd412013-10-02 17:41:50 -07001842 if (mWorkspace == null) {
1843 // Can be cases where mWorkspace is null, this prevents a NPE
1844 return;
1845 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 // In all these cases, only animate if we're already on home
1847 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001848
Adam Cohen6fecd412013-10-02 17:41:50 -07001849 closeFolder();
1850 exitSpringLoadedDragMode();
1851
1852 // If we are already on home, then just animate back to the workspace,
1853 // otherwise, just wait until onResume to set the state back to Workspace
1854 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001855 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 } else {
1857 mOnResumeState = State.WORKSPACE;
1858 }
1859
1860 final View v = getWindow().peekDecorView();
1861 if (v != null && v.getWindowToken() != null) {
1862 InputMethodManager imm = (InputMethodManager)getSystemService(
1863 INPUT_METHOD_SERVICE);
1864 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1865 }
1866
Winson Chungb745afb2015-03-02 11:51:23 -08001867 // Reset the apps view
1868 if (!alreadyOnHome && mAppsView != null) {
1869 mAppsView.scrollToTop();
1870 }
1871
Hyunyoung Song3f471442015-04-08 19:01:34 -07001872 // Reset the widgets view
1873 if (!alreadyOnHome && mWidgetsView != null) {
1874 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001876
Adam Cohen9211d422014-10-07 18:14:53 -07001877 if (mLauncherCallbacks != null) {
1878 mLauncherCallbacks.onHomeIntent();
1879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
Adam Cohened307df2013-10-02 09:37:31 -07001881
Adam Cohen9211d422014-10-07 18:14:53 -07001882 if (mLauncherCallbacks != null) {
1883 mLauncherCallbacks.onNewIntent(intent);
1884 }
Winson15f8b172015-08-19 11:02:39 -07001885
1886 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1887 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1888 // animation.
1889 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001890 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1891 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001892 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1893 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001894
1895 // We use this flag to suppress noisy callbacks above custom content state
1896 // from onResume.
1897 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001898 mWorkspace.post(new Runnable() {
1899 @Override
1900 public void run() {
1901 mWorkspace.moveToDefaultScreen(true);
1902 }
1903 });
1904 }
1905 }
1906
1907 if (DEBUG_RESUME_TIME) {
1908 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1909 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001913 public void onRestoreInstanceState(Bundle state) {
1914 super.onRestoreInstanceState(state);
1915 for (int page: mSynchronouslyBoundPages) {
1916 mWorkspace.restoreInstanceStateForChild(page);
1917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919
1920 @Override
1921 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001922 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001923 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1924 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001925 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001926 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927
Michael Jurka883f55b2010-10-21 15:47:14 -07001928 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001929 // We close any open folder since it will not be re-opened, and we need to make sure
1930 // this state is reflected.
1931 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001935 ContentValues itemValues = new ContentValues();
1936 mPendingAddInfo.writeToValues(itemValues);
1937 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001938 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001939 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941
Hyunyoung Song3f471442015-04-08 19:01:34 -07001942 // Save the current widgets tray?
1943 // TODO(hyunyoungs)
Adam Cohen9211d422014-10-07 18:14:53 -07001944
1945 if (mLauncherCallbacks != null) {
1946 mLauncherCallbacks.onSaveInstanceState(outState);
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
1950 @Override
1951 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001953
Winson Chunge7a03942011-08-05 15:05:12 -07001954 // Remove all pending runnables
1955 mHandler.removeMessages(ADVANCE_MSG);
1956 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001957 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001958
Winson Chungcd2b0142011-06-08 16:02:26 -07001959 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001960 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001961
1962 // It's possible to receive onDestroy after a new Launcher activity has
1963 // been created. In this case, don't interfere with the new Launcher.
1964 if (mModel.isCurrentCallbacks(this)) {
1965 mModel.stopLoader();
1966 app.setLauncher(null);
1967 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001970 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001972 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001974 mAppWidgetHost = null;
1975
1976 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
1978 TextKeyListener.getInstance().release();
1979
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001980 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001981
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001982 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001983 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001984 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985 mWorkspace = null;
1986 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001987
Michael Jurka2ecf9952012-06-18 12:52:28 -07001988 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001989
1990 if (mLauncherCallbacks != null) {
1991 mLauncherCallbacks.onDestroy();
1992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 }
1994
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 public DragController getDragController() {
1996 return mDragController;
1997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 @Override
2000 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002001 onStartForResult(requestCode);
2002 super.startActivityForResult(intent, requestCode);
2003 }
2004
2005 @Override
2006 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2007 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2008 onStartForResult(requestCode);
2009 try {
2010 super.startIntentSenderForResult(intent, requestCode,
2011 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2012 } catch (IntentSender.SendIntentException e) {
2013 throw new ActivityNotFoundException();
2014 }
2015 }
2016
2017 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002018 if (requestCode >= 0) {
2019 setWaitingForResult(true);
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Winson Chung70d72102011-08-12 11:24:35 -07002023 /**
2024 * Indicates that we want global search for this activity by setting the globalSearch
2025 * argument for {@link #startSearch} to true.
2026 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002028 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002030
Karl Rosaen138a0412009-04-23 19:00:21 -07002031 if (initialQuery == null) {
2032 // Use any text typed in the launcher as the initial query
2033 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 if (appSearchData == null) {
2036 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002037 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
Winson Chungadf0c182012-08-23 14:59:07 -07002039 Rect sourceBounds = new Rect();
2040 if (mSearchDropTargetBar != null) {
2041 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2042 }
Romain Guycbb89e42009-06-08 15:52:54 -07002043
Ian Parkinson047036e2014-09-01 15:40:53 +01002044 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002045 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002046 if (clearTextImmediately) {
2047 clearTypedText();
2048 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002049
2050 // We need to show the workspace after starting the search
2051 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002052 }
2053
Ian Parkinson047036e2014-09-01 15:40:53 +01002054 /**
2055 * Start a text search.
2056 *
2057 * @return {@code true} if the search will start immediately, so any further keypresses
2058 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2059 * to buffer keypresses.
2060 */
2061 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002063 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2064 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2065 sourceBounds);
2066 }
2067
Michael Jurkaa33411c2012-06-14 16:18:21 -07002068 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002070 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 }
2072
2073 /**
2074 * Starts the global search activity. This code is a copied from SearchManager
2075 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002078 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2080 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2081 if (globalSearchActivity == null) {
2082 Log.w(TAG, "No global search activity found.");
2083 return;
2084 }
2085 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2086 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2087 intent.setComponent(globalSearchActivity);
2088 // Make sure that we have a Bundle to put source in
2089 if (appSearchData == null) {
2090 appSearchData = new Bundle();
2091 } else {
2092 appSearchData = new Bundle(appSearchData);
2093 }
Adam Cohen9211d422014-10-07 18:14:53 -07002094 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 if (!appSearchData.containsKey("source")) {
2096 appSearchData.putString("source", getPackageName());
2097 }
2098 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2099 if (!TextUtils.isEmpty(initialQuery)) {
2100 intent.putExtra(SearchManager.QUERY, initialQuery);
2101 }
2102 if (selectInitialQuery) {
2103 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2104 }
2105 intent.setSourceBounds(sourceBounds);
2106 try {
2107 startActivity(intent);
2108 } catch (ActivityNotFoundException ex) {
2109 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
2112
Winson Chungbedf9232015-07-10 12:38:30 -07002113 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2114 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2115 return;
2116 }
2117
2118 // If not handled, then just start the provided search intent
2119 startActivitySafely(v, searchIntent, null);
2120 }
2121
Yura4f93ec62014-02-04 14:15:21 +00002122 public boolean isOnCustomContent() {
2123 return mWorkspace.isOnOrMovingToCustomContent();
2124 }
2125
Winson Chung70d72102011-08-12 11:24:35 -07002126 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002127 public boolean onPrepareOptionsMenu(Menu menu) {
2128 super.onPrepareOptionsMenu(menu);
Adam Cohen9211d422014-10-07 18:14:53 -07002129 if (mLauncherCallbacks != null) {
2130 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2131 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002132 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 @Override
2136 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002137 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002138 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002139 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002140 }
2141
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public boolean isWorkspaceLocked() {
2143 return mWorkspaceLoading || mWaitingForResult;
2144 }
2145
Adam Cohen517a7f52014-03-01 12:12:59 -08002146 public boolean isWorkspaceLoading() {
2147 return mWorkspaceLoading;
2148 }
2149
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002150 private void setWorkspaceLoading(boolean value) {
2151 boolean isLocked = isWorkspaceLocked();
2152 mWorkspaceLoading = value;
2153 if (isLocked != isWorkspaceLocked()) {
2154 onWorkspaceLockedChanged();
2155 }
2156 }
2157
2158 private void setWaitingForResult(boolean value) {
2159 boolean isLocked = isWorkspaceLocked();
2160 mWaitingForResult = value;
2161 if (isLocked != isWorkspaceLocked()) {
2162 onWorkspaceLockedChanged();
2163 }
2164 }
2165
Adam Cohen9211d422014-10-07 18:14:53 -07002166 protected void onWorkspaceLockedChanged() {
2167 if (mLauncherCallbacks != null) {
2168 mLauncherCallbacks.onWorkspaceLockedChanged();
2169 }
2170 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002171
Michael Jurka0280c3b2010-09-17 15:00:07 -07002172 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002173 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002174 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2176 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002177 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002178 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002179
Sunny Goyale6b63a32015-01-16 16:14:29 -08002180 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002181 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002182 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2183 }
2184
Sunny Goyale6b63a32015-01-16 16:14:29 -08002185 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002186 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2187 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002188 if (appWidgetInfo.configure != null) {
2189 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002190 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002191
Bjorn Bringert7984c942009-12-09 15:38:25 +00002192 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002193 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2194 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002197 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002198 Runnable onComplete = new Runnable() {
2199 @Override
2200 public void run() {
2201 // Exit spring loaded mode if necessary after adding the widget
2202 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2203 null);
2204 }
2205 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002206 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002207 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002208 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210 }
Romain Guycbb89e42009-06-08 15:52:54 -07002211
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002212 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002213 // Close any folders that may be open.
2214 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002215 mWorkspace.moveToCustomContentScreen(animate);
2216 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002217
2218 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2219 int[] cell, int spanX, int spanY) {
2220 switch (info.itemType) {
2221 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2222 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2223 int span[] = new int[2];
2224 span[0] = spanX;
2225 span[1] = spanY;
2226 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2227 container, screenId, cell, span);
2228 break;
2229 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2230 processShortcutFromDrop(info.componentName, container, screenId, cell);
2231 break;
2232 default:
2233 throw new IllegalStateException("Unknown item type: " + info.itemType);
2234 }
2235 }
2236
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 /**
2238 * Process a shortcut drop.
2239 *
2240 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002244 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2245 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249
2250 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.cellX = cell[0];
2252 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254
2255 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2256 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002257 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 }
2259
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 /**
2261 * Process a widget drop.
2262 *
2263 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002267 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2268 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002272 mPendingAddInfo.minSpanX = info.minSpanX;
2273 mPendingAddInfo.minSpanY = info.minSpanY;
2274
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.cellX = cell[0];
2277 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002279 if (span != null) {
2280 mPendingAddInfo.spanX = span[0];
2281 mPendingAddInfo.spanY = span[1];
2282 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283
Adam Cohened66b2b2012-01-23 17:28:51 -08002284 AppWidgetHostView hostView = info.boundWidget;
2285 int appWidgetId;
2286 if (hostView != null) {
2287 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002288 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002289
2290 // Clear the boundWidget so that it doesn't get destroyed.
2291 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 // In this case, we either need to start an activity to get permission to bind
2294 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002296 Bundle options = info.bindOptions;
2297
Sunny Goyalffe83f12014-08-14 17:39:34 -07002298 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2299 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002300 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002301 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002302 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2305 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002307 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2308 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002309 // TODO: we need to make sure that this accounts for the options bundle.
2310 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002311 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2312 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 }
2315
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002318 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 folderInfo.title = getText(R.string.folder_name);
2320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002322 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2323 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002324 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325
2326 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 FolderIcon newFolder =
2328 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002331 // Force measure the new folder icon
2332 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2333 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002334 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
Romain Guycbb89e42009-06-08 15:52:54 -07002336
Winsonc0b52fe2015-09-09 16:38:15 -07002337 /**
Winsonfa56b3f2015-09-14 12:01:13 -07002338 * Unbinds the view for the specified item, and removes the item and all its children.
2339 *
2340 * @param v the view being removed.
Winsonfa56b3f2015-09-14 12:01:13 -07002341 * @param itemInfo the {@link ItemInfo} for this view.
2342 * @param deleteFromDb whether or not to delete this item from the db.
Winsonc0b52fe2015-09-09 16:38:15 -07002343 */
Winson2949fb52015-09-24 09:56:11 -07002344 public boolean removeItem(View v, ItemInfo itemInfo, boolean deleteFromDb) {
Winsonc0b52fe2015-09-09 16:38:15 -07002345 if (itemInfo instanceof ShortcutInfo) {
Winsonfa56b3f2015-09-14 12:01:13 -07002346 // Remove the shortcut from the folder before removing it from launcher
Winson2949fb52015-09-24 09:56:11 -07002347 FolderInfo folderInfo = sFolders.get(itemInfo.container);
2348 if (folderInfo != null) {
2349 folderInfo.remove((ShortcutInfo) itemInfo);
Winsonfa56b3f2015-09-14 12:01:13 -07002350 } else {
2351 mWorkspace.removeWorkspaceItem(v);
2352 }
Winsonc0b52fe2015-09-09 16:38:15 -07002353 if (deleteFromDb) {
2354 LauncherModel.deleteItemFromDatabase(this, itemInfo);
2355 }
2356 } else if (itemInfo instanceof FolderInfo) {
2357 final FolderInfo folderInfo = (FolderInfo) itemInfo;
2358 unbindFolder(folderInfo);
2359 mWorkspace.removeWorkspaceItem(v);
2360 if (deleteFromDb) {
2361 LauncherModel.deleteFolderAndContentsFromDatabase(this, folderInfo);
2362 }
2363 } else if (itemInfo instanceof LauncherAppWidgetInfo) {
2364 final LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) itemInfo;
2365 unbindAppWidget(widgetInfo, deleteFromDb);
2366 if (deleteFromDb) {
2367 LauncherModel.deleteItemFromDatabase(this, widgetInfo);
2368 }
2369 } else {
2370 return false;
2371 }
2372 return true;
2373 }
2374
2375 /**
2376 * Unbinds any launcher references to the folder.
2377 */
2378 private void unbindFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002379 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002380 }
2381
Winsonc0b52fe2015-09-09 16:38:15 -07002382 /**
2383 * Unbinds any launcher references to the widget and deletes the app widget id.
2384 */
2385 private void unbindAppWidget(final LauncherAppWidgetInfo widgetInfo, boolean deleteAppWidgetId) {
2386 final LauncherAppWidgetHost appWidgetHost = getAppWidgetHost();
2387 if (deleteAppWidgetId && appWidgetHost != null &&
2388 !widgetInfo.isCustomWidget() && widgetInfo.isWidgetIdValid()) {
2389 // Deleting an app widget ID is a void call but writes to disk before returning
2390 // to the caller...
2391 new AsyncTask<Void, Void, Void>() {
2392 public Void doInBackground(Void ... args) {
2393 appWidgetHost.deleteAppWidgetId(widgetInfo.appWidgetId);
2394 return null;
2395 }
Sunny Goyalf27cb0e2015-09-23 16:12:02 -07002396 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Winsonc0b52fe2015-09-09 16:38:15 -07002397 }
2398 removeWidgetToAutoAdvance(widgetInfo.hostView);
2399 widgetInfo.hostView = null;
2400 }
2401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 @Override
2403 public boolean dispatchKeyEvent(KeyEvent event) {
2404 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2405 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002407 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002408 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002409 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002410 dumpState();
2411 return true;
2412 }
2413 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002414 }
2415 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2416 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002417 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 return true;
2419 }
2420 }
2421
2422 return super.dispatchKeyEvent(event);
2423 }
2424
Joe Onorato88ec0992009-11-19 13:16:06 -08002425 @Override
2426 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002427 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2428 return;
2429 }
2430
Sunny Goyal45478022015-06-08 16:52:41 -07002431 if (mDragController.isDragging()) {
2432 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002433 return;
2434 }
2435
Winson Chungb745afb2015-03-02 11:51:23 -08002436 if (isAppsViewVisible()) {
2437 showWorkspace(true);
2438 } else if (isWidgetsViewVisible()) {
2439 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002440 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002441 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002442 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002443 Folder openFolder = mWorkspace.getOpenFolder();
2444 if (openFolder.isEditingName()) {
2445 openFolder.dismissEditingName();
2446 } else {
2447 closeFolder();
2448 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002449 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002450 mWorkspace.exitWidgetResizeMode();
2451
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 // Back button is a no-op here, but give at least some feedback for the button press
2453 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002454 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002455 }
2456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002458 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002459 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002460 @Override
2461 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002462 if (mAppWidgetHost != null) {
2463 mAppWidgetHost.startListening();
2464 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002465 }
2466
2467 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 * Launches the intent referred by the clicked shortcut.
2469 *
2470 * @param v The view representing the clicked shortcut.
2471 */
2472 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002473 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2474 // view has detached (it's possible for this to happen if the view is removed mid touch).
2475 if (v.getWindowToken() == null) {
2476 return;
2477 }
2478
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002479 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002480 return;
2481 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002482
Adam Cohen1697b792013-09-17 19:08:21 -07002483 if (v instanceof Workspace) {
2484 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002485 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002486 }
2487 return;
2488 }
2489
2490 if (v instanceof CellLayout) {
2491 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002492 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2493 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002495 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002499 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002505 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002507 } else if (tag instanceof AppInfo) {
2508 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002509 } else if (tag instanceof LauncherAppWidgetInfo) {
2510 if (v instanceof PendingAppWidgetHostView) {
2511 onClickPendingWidget((PendingAppWidgetHostView) v);
2512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
2514 }
2515
Sunny Goyal70660032015-05-14 00:07:08 -07002516 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002517 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002518 return false;
2519 }
2520
Michael Jurkaaf442092010-06-10 17:01:57 -07002521 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002522 * Event handler for the app widget view which has not fully restored.
2523 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002524 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002525 if (mIsSafeModeEnabled) {
2526 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2527 return;
2528 }
2529
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002530 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002531 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002532 int widgetId = info.appWidgetId;
2533 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2534 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002535 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2536 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002537 mPendingAddInfo.copyFrom(info);
2538 mPendingAddWidgetId = widgetId;
2539
Sunny Goyalffe83f12014-08-14 17:39:34 -07002540 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2541 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002542 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002543 } else if (info.installProgress < 0) {
2544 // The install has not been queued
2545 final String packageName = info.providerName.getPackageName();
2546 showBrokenAppInstallDialog(packageName,
2547 new DialogInterface.OnClickListener() {
2548 public void onClick(DialogInterface dialog, int id) {
2549 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2550 }
2551 });
2552 } else {
2553 // Download has started.
2554 final String packageName = info.providerName.getPackageName();
2555 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002556 }
2557 }
2558
2559 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002560 * Event handler for the "grid" button that appears on the home screen, which
2561 * enters all apps mode.
2562 *
2563 * @param v The view that was clicked.
2564 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002565 protected void onClickAllAppsButton(View v) {
2566 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002567 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002568 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002569 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002570
2571 if (mLauncherCallbacks != null) {
2572 mLauncherCallbacks.onClickAllAppsButton(v);
2573 }
Winson Chung76648c52015-07-10 14:33:23 -07002574 }
2575 }
2576
2577 protected void onLongClickAllAppsButton(View v) {
2578 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2579 if (!isAppsViewVisible()) {
2580 showAppsView(true /* animated */, false /* resetListToTop */,
2581 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002582 }
2583 }
2584
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002585 private void showBrokenAppInstallDialog(final String packageName,
2586 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002587 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002588 .setTitle(R.string.abandoned_promises_title)
2589 .setMessage(R.string.abandoned_promise_explanation)
2590 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2591 .setNeutralButton(R.string.abandoned_clean_this,
2592 new DialogInterface.OnClickListener() {
2593 public void onClick(DialogInterface dialog, int id) {
2594 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2595 mWorkspace.removeAbandonedPromise(packageName, user);
2596 }
2597 })
2598 .create().show();
2599 return;
2600 }
2601
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 /**
2603 * Event handler for an app shortcut click.
2604 *
2605 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2606 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002607 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2609 Object tag = v.getTag();
2610 if (!(tag instanceof ShortcutInfo)) {
2611 throw new IllegalArgumentException("Input must be a Shortcut");
2612 }
2613
2614 // Open shortcut
2615 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002616
2617 if (shortcut.isDisabled != 0) {
2618 int error = R.string.activity_not_available;
2619 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2620 error = R.string.safemode_shortcut_error;
2621 }
2622 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2623 return;
2624 }
2625
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002627 if ((v instanceof BubbleTextView)
2628 && shortcut.isPromise()
2629 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002631 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002634 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002636 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 return;
2638 }
2639
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002641 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002642
2643 if (mLauncherCallbacks != null) {
2644 mLauncherCallbacks.onClickAppShortcut(v);
2645 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 }
2647
Adam Cohen091440a2015-03-18 14:16:05 -07002648 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002650 final ShortcutInfo shortcut;
2651 final Intent intent;
2652 if (tag instanceof ShortcutInfo) {
2653 shortcut = (ShortcutInfo) tag;
2654 intent = shortcut.intent;
2655 int[] pos = new int[2];
2656 v.getLocationOnScreen(pos);
2657 intent.setSourceBounds(new Rect(pos[0], pos[1],
2658 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002659
Sunny Goyal508da152014-08-14 10:53:27 -07002660 } else if (tag instanceof AppInfo) {
2661 shortcut = null;
2662 intent = ((AppInfo) tag).intent;
2663 } else {
2664 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2665 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666
2667 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002668 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669
2670 if (success && v instanceof BubbleTextView) {
2671 mWaitingForResume = (BubbleTextView) v;
2672 mWaitingForResume.setStayPressed(true);
2673 }
2674 }
2675
2676 /**
2677 * Event handler for a folder icon click.
2678 *
2679 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2680 */
2681 protected void onClickFolderIcon(View v) {
2682 if (LOGD) Log.d(TAG, "onClickFolder");
2683 if (!(v instanceof FolderIcon)){
2684 throw new IllegalArgumentException("Input must be a FolderIcon");
2685 }
2686
Sunny Goyal317698b2015-07-29 11:45:41 -07002687 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002688 FolderIcon folderIcon = (FolderIcon) v;
2689 final FolderInfo info = folderIcon.getFolderInfo();
2690 Folder openFolder = mWorkspace.getFolderForTag(info);
2691
2692 // If the folder info reports that the associated folder is open, then verify that
2693 // it is actually opened. There have been a few instances where this gets out of sync.
2694 if (info.opened && openFolder == null) {
2695 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2696 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2697 info.opened = false;
2698 }
2699
2700 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2701 // Close any open folder
2702 closeFolder();
2703 // Open the requested folder
2704 openFolder(folderIcon);
2705 } else {
2706 // Find the open folder...
2707 int folderScreen;
2708 if (openFolder != null) {
2709 folderScreen = mWorkspace.getPageForView(openFolder);
2710 // .. and close it
2711 closeFolder(openFolder);
2712 if (folderScreen != mWorkspace.getCurrentPage()) {
2713 // Close any folder open on the current screen
2714 closeFolder();
2715 // Pull the folder onto this screen
2716 openFolder(folderIcon);
2717 }
2718 }
2719 }
Adam Cohen9211d422014-10-07 18:14:53 -07002720
2721 if (mLauncherCallbacks != null) {
2722 mLauncherCallbacks.onClickFolderIcon(v);
2723 }
Michael Jurka5130e402011-10-13 04:55:35 -07002724 }
2725
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002726 /**
2727 * Event handler for the (Add) Widgets button that appears after a long press
2728 * on the home screen.
2729 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002730 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002731 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002732 if (mIsSafeModeEnabled) {
2733 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2734 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002735 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002736 if (mLauncherCallbacks != null) {
2737 mLauncherCallbacks.onClickAddWidgetButton(view);
2738 }
Adam Cohen9211d422014-10-07 18:14:53 -07002739 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002740 }
2741
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002742 /**
2743 * Event handler for the wallpaper picker button that appears after a long press
2744 * on the home screen.
2745 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002746 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002747 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham785f7a52015-08-31 17:28:32 -07002748 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2749 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2750 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2751 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002752 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002753
2754 if (mLauncherCallbacks != null) {
2755 mLauncherCallbacks.onClickWallpaperPicker(v);
2756 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 }
2758
2759 /**
2760 * Event handler for a click on the settings button that appears after a long press
2761 * on the home screen.
2762 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002763 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002764 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002765 if (mLauncherCallbacks != null) {
2766 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002767 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002768 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002769 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770 }
2771
Adam Cohen61f560d2013-09-30 15:58:20 -07002772 public View.OnTouchListener getHapticFeedbackTouchListener() {
2773 if (mHapticFeedbackTouchListener == null) {
2774 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002775 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002776 @Override
2777 public boolean onTouch(View v, MotionEvent event) {
2778 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2779 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2780 }
2781 return false;
2782 }
2783 };
2784 }
2785 return mHapticFeedbackTouchListener;
2786 }
2787
Adam Cohen9211d422014-10-07 18:14:53 -07002788 public void onDragStarted(View view) {
2789 if (isOnCustomContent()) {
2790 // Custom content screen doesn't participate in drag and drop. If on custom
2791 // content screen, move to default.
2792 moveWorkspaceToDefaultScreen();
2793 }
2794
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onDragStarted(view);
2797 }
2798 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002799
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002800 /**
2801 * Called when the user stops interacting with the launcher.
2802 * This implies that the user is now on the homescreen and is not doing housekeeping.
2803 */
Adam Cohen9211d422014-10-07 18:14:53 -07002804 protected void onInteractionEnd() {
2805 if (mLauncherCallbacks != null) {
2806 mLauncherCallbacks.onInteractionEnd();
2807 }
2808 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002809
2810 /**
2811 * Called when the user starts interacting with the launcher.
2812 * The possible interactions are:
2813 * - open all apps
2814 * - reorder an app shortcut, or a widget
2815 * - open the overview mode.
2816 * This is a good time to stop doing things that only make sense
2817 * when the user is on the homescreen and not doing housekeeping.
2818 */
Adam Cohen9211d422014-10-07 18:14:53 -07002819 protected void onInteractionBegin() {
2820 if (mLauncherCallbacks != null) {
2821 mLauncherCallbacks.onInteractionBegin();
2822 }
2823 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002824
Winson Chungcd99cd32015-04-29 11:03:24 -07002825 /** Updates the interaction state. */
2826 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002827 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002828 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002829 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2830 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002831 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002832 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002833 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002834 onInteractionEnd();
2835 }
2836 }
2837
Kenny Guyf07af7b2014-07-31 11:39:16 +01002838 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002839 try {
2840 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 launcherApps.showAppDetailsForProfile(componentName, user);
2842 } catch (SecurityException e) {
2843 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2844 Log.e(TAG, "Launcher does not have permission to launch settings");
2845 } catch (ActivityNotFoundException e) {
2846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2847 Log.e(TAG, "Unable to launch settings");
2848 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002849 }
2850
Michael Jurka1e2f4652013-07-08 18:03:46 -07002851 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002852 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2853 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002854 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002855 // System applications cannot be installed. For now, show a toast explaining that.
2856 // We may give them the option of disabling apps this way.
2857 int messageId = R.string.uninstall_system_app_text;
2858 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002859 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002860 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002861 String packageName = componentName.getPackageName();
2862 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002863 Intent intent = new Intent(
2864 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002865 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2866 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002867 if (user != null) {
2868 user.addToIntent(intent, Intent.EXTRA_USER);
2869 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002870 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002871 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002873 }
2874
Winson Chung8f1eff72015-05-28 17:33:40 -07002875 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002876 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002878 // Only launch using the new animation if the shortcut has not opted out (this is a
2879 // private contract between launcher and may be ignored in the future).
2880 boolean useLaunchAnimation = (v != null) &&
2881 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002882 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2883 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002884
Kenny Guy1317e2d2014-05-08 18:52:50 +01002885 UserHandleCompat user = null;
2886 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2887 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2888 user = userManager.getUserForSerialNumber(serialNumber);
2889 }
2890
2891 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002892 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002893 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002894 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002895 int left = 0, top = 0;
2896 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2897 if (v instanceof TextView) {
2898 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002899 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2900 if (icon != null) {
2901 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002902 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002903 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002904 width = bounds.width();
2905 height = bounds.height();
2906 }
2907 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002908 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2909 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002910 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002911 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002912 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002913 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002914 // On L devices, we use the device default slide-up transition.
2915 // On L MR1 devices, we a custom version of the slide-up transition which
2916 // doesn't have the delay present in the device default.
2917 opts = ActivityOptions.makeCustomAnimation(this,
2918 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002919 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002920 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002921 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002922
2923 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2924 // Could be launching some bookkeeping activity
2925 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002926 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002927 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002928 launcherApps.startActivityForProfile(intent.getComponent(), user,
2929 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002930 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002931 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 } catch (SecurityException e) {
2933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002934 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002936 "or use the exported attribute for this activity. "
2937 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002939 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002941
Winson Chungbedf9232015-07-10 12:38:30 -07002942 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002943 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002944 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2945 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2946 return false;
2947 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002948 try {
2949 success = startActivity(v, intent, tag);
2950 } catch (ActivityNotFoundException e) {
2951 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2952 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2953 }
2954 return success;
2955 }
2956
Adam Cohen268c4752012-06-06 17:47:33 -07002957 /**
2958 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2959 * in the DragLayer in the exact absolute location of the original FolderIcon.
2960 */
2961 private void copyFolderIconToImage(FolderIcon fi) {
2962 final int width = fi.getMeasuredWidth();
2963 final int height = fi.getMeasuredHeight();
2964
2965 // Lazy load ImageView, Bitmap and Canvas
2966 if (mFolderIconImageView == null) {
2967 mFolderIconImageView = new ImageView(this);
2968 }
2969 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2970 mFolderIconBitmap.getHeight() != height) {
2971 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2972 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2973 }
2974
2975 DragLayer.LayoutParams lp;
2976 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2977 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2978 } else {
2979 lp = new DragLayer.LayoutParams(width, height);
2980 }
2981
Adam Cohen307fe232012-08-16 17:55:58 -07002982 // The layout from which the folder is being opened may be scaled, adjust the starting
2983 // view size by this scale factor.
2984 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002985 lp.customPosition = true;
2986 lp.x = mRectForFolderAnimation.left;
2987 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002988 lp.width = (int) (scale * width);
2989 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002990
2991 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2992 fi.draw(mFolderIconCanvas);
2993 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002994 if (fi.getFolder() != null) {
2995 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2996 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // Just in case this image view is still in the drag layer from a previous animation,
2999 // we remove it and re-add it.
3000 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3001 mDragLayer.removeView(mFolderIconImageView);
3002 }
3003 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003004 if (fi.getFolder() != null) {
3005 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003006 }
Adam Cohen268c4752012-06-06 17:47:33 -07003007 }
3008
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003010 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07003011 FolderInfo info = (FolderInfo) fi.getTag();
3012 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3013 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003014 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3015 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003016 }
3017
Adam Cohen268c4752012-06-06 17:47:33 -07003018 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3019 copyFolderIconToImage(fi);
3020 fi.setVisibility(View.INVISIBLE);
3021
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003022 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
3023 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003024 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003025 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3026 }
3027 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003028 oa.start();
3029 }
3030
Adam Cohen268c4752012-06-06 17:47:33 -07003031 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003032 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07003033 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003034
Adam Cohen268c4752012-06-06 17:47:33 -07003035 // We remove and re-draw the FolderIcon in-case it has changed
3036 mDragLayer.removeView(mFolderIconImageView);
3037 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07003038 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003039 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003040 oa.addListener(new AnimatorListenerAdapter() {
3041 @Override
3042 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003043 if (cl != null) {
3044 cl.clearFolderLeaveBehind();
3045 // Remove the ImageView copy of the FolderIcon and make the original visible.
3046 mDragLayer.removeView(mFolderIconImageView);
3047 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 }
3049 }
3050 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003051 oa.start();
3052 }
3053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003055 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 * is animated relative to the specified View. If the View is null, no animation
3057 * is played.
3058 *
3059 * @param folderInfo The FolderInfo describing the folder to open.
3060 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003061 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003062 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003063 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3064 if (openFolder != null && openFolder != folder) {
3065 // Close any open folder before opening a folder.
3066 closeFolder();
3067 }
3068
Adam Cohena9cf38f2011-05-02 15:36:58 -07003069 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070
Adam Cohena9cf38f2011-05-02 15:36:58 -07003071 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072
Sunny Goyalf4066152015-04-15 09:42:19 -07003073 // While the folder is open, the position of the icon cannot change.
3074 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3075
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 // Just verify that the folder hasn't already been added to the DragLayer.
3077 // There was a one-off crash where the folder had a parent already.
3078 if (folder.getParent() == null) {
3079 mDragLayer.addView(folder);
3080 mDragController.addDropTarget((DropTarget) folder);
3081 } else {
3082 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3083 folder.getParent() + ").");
3084 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003085 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003086 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003087
3088 // Notify the accessibility manager that this folder "window" has appeared and occluded
3089 // the workspace items
3090 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3091 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 }
3093
3094 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003095 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003097 if (folder.isEditingName()) {
3098 folder.dismissEditingName();
3099 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 closeFolder(folder);
3101 }
3102 }
3103
Sunny Goyal83a8f042015-05-19 12:52:12 -07003104 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003105 folder.getInfo().opened = false;
3106
3107 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3108 if (parent != null) {
3109 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3110 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003111 if (fi != null) {
3112 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3113 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003114 }
3115 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003116
3117 // Notify the accessibility manager that this folder "window" has disappeard and no
3118 // longer occludeds the workspace items
3119 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
3121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003123 if (!isDraggingEnabled()) return false;
3124 if (isWorkspaceLocked()) return false;
3125 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126
Winson Chung76648c52015-07-10 14:33:23 -07003127 if (v == mAllAppsButton) {
3128 onLongClickAllAppsButton(v);
3129 return true;
3130 }
3131
Adam Cohen1697b792013-09-17 19:08:21 -07003132 if (v instanceof Workspace) {
3133 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003134 if (!mWorkspace.isTouchActive()) {
3135 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003136 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3137 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3138 return true;
3139 } else {
3140 return false;
3141 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003142 } else {
3143 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003144 }
Adam Cohen1697b792013-09-17 19:08:21 -07003145 }
3146
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003147 CellLayout.CellInfo longClickCellInfo = null;
3148 View itemUnderLongClick = null;
3149 if (v.getTag() instanceof ItemInfo) {
3150 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003151 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003152 itemUnderLongClick = longClickCellInfo.cell;
3153 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154 }
3155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 // The hotseat touch handling does not go through Workspace, and we always allow long press
3157 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003158 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003159 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003160 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003161 // User long pressed on empty space
3162 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3163 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003164 if (mWorkspace.isInOverviewMode()) {
3165 mWorkspace.startReordering(v);
3166 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003167 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003170 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3171 mHotseat.getOrderInHotseat(
3172 longClickCellInfo.cellX,
3173 longClickCellInfo.cellY));
3174 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003176 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 }
3178 }
3179 }
3180 return true;
3181 }
3182
Winson Chung3d503fb2011-07-13 17:25:49 -07003183 boolean isHotseatLayout(View layout) {
3184 return mHotseat != null && layout != null &&
3185 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3186 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003187
Winson Chung3d503fb2011-07-13 17:25:49 -07003188 /**
3189 * Returns the CellLayout of the specified container at the specified screen.
3190 */
Sunny Goyal92820592015-03-02 11:31:35 -08003191 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3193 if (mHotseat != null) {
3194 return mHotseat.getLayout();
3195 } else {
3196 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003197 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003199 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003200 }
3201 }
3202
Winson Chungb745afb2015-03-02 11:51:23 -08003203 /**
3204 * For overridden classes.
3205 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003206 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003207 return isAppsViewVisible();
3208 }
3209
3210 public boolean isAppsViewVisible() {
3211 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3212 }
3213
3214 public boolean isWidgetsViewVisible() {
3215 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003216 }
3217
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003218 private void setWorkspaceBackground(int background) {
3219 switch (background) {
3220 case WORKSPACE_BACKGROUND_TRANSPARENT:
3221 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3222 break;
3223 case WORKSPACE_BACKGROUND_BLACK:
3224 getWindow().setBackgroundDrawable(null);
3225 break;
3226 default:
3227 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3228 }
Craig Mautner360310b2012-10-26 15:13:08 -07003229 }
3230
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003231 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003232 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3233 int curflags = getWindow().getAttributes().flags
3234 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3235 if (wpflags != curflags) {
3236 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3237 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003238 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003239 }
3240
Michael Jurkae326f182011-11-21 14:05:46 -08003241 @Override
3242 public void onTrimMemory(int level) {
3243 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003244 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3245 // The widget preview db can result in holding onto over
3246 // 3MB of memory for caching which isn't necessary.
3247 SQLiteDatabase.releaseMemory();
3248
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003249 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003250 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003251 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003252 if (mLauncherCallbacks != null) {
3253 mLauncherCallbacks.onTrimMemory(level);
3254 }
Michael Jurkae326f182011-11-21 14:05:46 -08003255 }
3256
Winson5c6bdbb2015-09-03 11:36:19 -07003257 public boolean showWorkspace(boolean animated) {
Winson10612a72015-09-09 17:06:09 -07003258 return showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003259 }
3260
Winson5c6bdbb2015-09-03 11:36:19 -07003261 public boolean showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003262 boolean changed = mState != State.WORKSPACE ||
3263 mWorkspace.getState() != Workspace.State.NORMAL;
3264 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003265 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003266 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003267 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003268
Michael Jurkab3e22d92011-10-31 15:58:33 -07003269 // Set focus to the AppsCustomize button
3270 if (mAllAppsButton != null) {
3271 mAllAppsButton.requestFocus();
3272 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003273 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003274
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003275 // Change the state *after* we've called all the transition code
3276 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003277
Winson Chung5fb63472011-02-02 17:03:37 -08003278 // Resume the auto-advance of widgets
3279 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003280 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003281
Winson Chung0f785722015-04-08 10:27:49 -07003282 if (changed) {
3283 // Send an accessibility event to announce the context change
3284 getWindow().getDecorView()
3285 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003286 }
Winson5c6bdbb2015-09-03 11:36:19 -07003287 return changed;
Bjorn Bringertc459e522013-06-07 19:36:01 +01003288 }
3289
Adam Cohened307df2013-10-02 09:37:31 -07003290 void showOverviewMode(boolean animated) {
3291 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003292 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003293 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003294 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003295 }
3296
Winson Chungb745afb2015-03-02 11:51:23 -08003297 /**
3298 * Shows the apps view.
3299 */
Winson Chung76648c52015-07-10 14:33:23 -07003300 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3301 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003302 if (resetListToTop) {
3303 mAppsView.scrollToTop();
3304 }
Winson Chung4ac30062015-05-08 17:34:17 -07003305 if (updatePredictedApps) {
3306 tryAndUpdatePredictedApps();
3307 }
Winson Chung76648c52015-07-10 14:33:23 -07003308 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003309 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003310
Winson Chungb745afb2015-03-02 11:51:23 -08003311 /**
3312 * Shows the widgets view.
3313 */
3314 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003315 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003316 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003318 }
Winson Chung76648c52015-07-10 14:33:23 -07003319 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320
3321 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003322 @Override
3323 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003324 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003325 }
3326 });
Winson Chungb745afb2015-03-02 11:51:23 -08003327 }
3328
3329 /**
3330 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003331 *
3332 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003333 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003334 // TODO: calling method should use the return value so that when {@code false} is returned
3335 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003336 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003337 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3338 mState != State.WIDGETS_SPRING_LOADED) {
3339 return false;
3340 }
3341 if (toState != State.APPS && toState != State.WIDGETS) {
3342 return false;
3343 }
Winson Chungb745afb2015-03-02 11:51:23 -08003344
3345 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003346 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3347 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003348 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003349 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003350 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003351
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003353 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354
3355 // Pause the auto-advance of widgets until we are out of AllApps
3356 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003357 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358 closeFolder();
3359
3360 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003361 getWindow().getDecorView()
3362 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003363 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 }
3365
Winson Chungcd99cd32015-04-29 11:03:24 -07003366 /**
3367 * Updates the workspace and interaction state on state change, and return the animation to this
3368 * new state.
3369 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003370 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003371 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003372 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003373 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003374 updateInteraction(fromState, toState);
3375 return anim;
3376 }
3377
Jun Mukaif0033da2015-08-04 18:34:30 -07003378 public void onLauncherClingShown() {
3379 // When a launcher cling appears, it should cover the underlying layers, so their focus
3380 // should be blocked.
3381 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3382 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3383 }
3384 }
3385
3386 public void onLauncherClingDismissed() {
3387 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3388 }
3389
Hyunyoung Song3f471442015-04-08 19:01:34 -07003390 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003391 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Tony Wickham94e0d372015-09-11 12:17:48 -07003392 if (isStateSpringLoaded()) {
Winson Chungb745afb2015-03-02 11:51:23 -08003393 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003394 }
Winson Chungb745afb2015-03-02 11:51:23 -08003395
Winson Chung006ee262015-08-03 14:40:11 -07003396 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003397 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003398 null /* onCompleteRunnable */);
Tony Wickham94e0d372015-09-11 12:17:48 -07003399
3400 if (isAppsViewVisible()) {
3401 mState = State.APPS_SPRING_LOADED;
3402 } else if (isWidgetsViewVisible()) {
3403 mState = State.WIDGETS_SPRING_LOADED;
3404 } else {
3405 mState = State.WORKSPACE_SPRING_LOADED;
3406 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003407 }
Adam Cohen7777d962011-08-18 18:58:38 -07003408
Hyunyoung Song3f471442015-04-08 19:01:34 -07003409 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003410 final Runnable onCompleteRunnable) {
Tony Wickham94e0d372015-09-11 12:17:48 -07003411 if (!isStateSpringLoaded()) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003412
Winson Chunge7a03942011-08-05 15:05:12 -07003413 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003414 @Override
3415 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003416 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003417 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3418 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003419 // Before we show workspace, hide all apps again because
3420 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3421 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003422 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003423 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003424 } else {
3425 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003426 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003427 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003428 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003429 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003430
Tony Wickham94e0d372015-09-11 12:17:48 -07003431 private boolean isStateSpringLoaded() {
3432 return mState == State.WORKSPACE_SPRING_LOADED || mState == State.APPS_SPRING_LOADED
3433 || mState == State.WIDGETS_SPRING_LOADED;
3434 }
3435
Michael Jurkad3ef3062010-11-23 16:23:58 -08003436 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003437 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003438 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003439 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003440 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003441 showWidgetsView(true, false);
Tony Wickham94e0d372015-09-11 12:17:48 -07003442 } else if (mState == State.WORKSPACE_SPRING_LOADED) {
3443 showWorkspace(true);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003444 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003445 }
3446
Winson Chung4ac30062015-05-08 17:34:17 -07003447 /**
3448 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3449 * resumed.
3450 */
3451 private void tryAndUpdatePredictedApps() {
3452 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003453 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003454 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003455 mAppsView.setPredictedApps(apps);
3456 }
3457 }
3458 }
3459
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460 void lockAllApps() {
3461 // TODO
3462 }
3463
3464 void unlockAllApps() {
3465 // TODO
3466 }
3467
Sunny Goyal594d76d2014-11-06 10:12:54 -08003468 protected void disableVoiceButtonProxy(boolean disable) {
3469 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003470 }
3471
Winsonf768d932015-09-25 16:12:35 -07003472 public boolean launcherCallbacksProvidesSearch() {
3473 return (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch());
3474 }
3475
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003476 public View getOrCreateQsbBar() {
Winsonf768d932015-09-25 16:12:35 -07003477 if (launcherCallbacksProvidesSearch()) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003478 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003479 }
3480
Adam Cohen24ce0b32014-01-14 16:18:14 -08003481 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003482 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3483 if (searchProvider == null) {
3484 return null;
3485 }
3486
3487 Bundle opts = new Bundle();
3488 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003489 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003490
3491 SharedPreferences sp = getSharedPreferences(
3492 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3493 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003494 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003495 if (!searchProvider.provider.flattenToString().equals(
3496 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003497 || (widgetInfo == null)
3498 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003499 // A valid widget is not already bound.
3500 if (widgetId > -1) {
3501 mAppWidgetHost.deleteAppWidgetId(widgetId);
3502 widgetId = -1;
3503 }
3504
3505 // Try to bind a new widget
3506 widgetId = mAppWidgetHost.allocateAppWidgetId();
3507
3508 if (!AppWidgetManagerCompat.getInstance(this)
3509 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3510 mAppWidgetHost.deleteAppWidgetId(widgetId);
3511 widgetId = -1;
3512 }
3513
3514 sp.edit()
3515 .putInt(QSB_WIDGET_ID, widgetId)
3516 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3517 .commit();
3518 }
3519
Sunny Goyal8d595092015-07-06 12:22:14 -07003520 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003521 if (widgetId != -1) {
3522 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003523 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003524 mQsb.updateAppWidgetOptions(opts);
3525 mQsb.setPadding(0, 0, 0, 0);
3526 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003527 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003528 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003529 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003530 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003531 }
3532
Sunny Goyal22235bc2015-04-03 09:23:43 -07003533 private void reinflateQSBIfNecessary() {
3534 if (mQsb instanceof LauncherAppWidgetHostView &&
3535 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3536 mSearchDropTargetBar.removeView(mQsb);
3537 mQsb = null;
3538 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3539 }
3540 }
3541
Michael Jurkad7c28052012-04-27 15:43:36 -07003542 @Override
3543 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003544 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003545 final List<CharSequence> text = event.getText();
3546 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003547 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003548 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003549 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003550 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003551 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003552 } else if (mWorkspace != null) {
3553 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003554 } else {
3555 text.add(getString(R.string.all_apps_home_button_label));
3556 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003557 return result;
3558 }
3559
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003560 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003561 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003562 */
Adam Cohen091440a2015-03-18 14:16:05 -07003563 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003564 @Override
3565 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003566 closeSystemDialogs();
3567 }
3568 }
3569
3570 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003571 * If the activity is currently paused, signal that we need to run the passed Runnable
3572 * in onResume.
3573 *
3574 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003575 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3576 * wrong when we're not running, and if the activity comes back to what the configuration was
3577 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003578 *
3579 * Implementation of the method from LauncherModel.Callbacks.
3580 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003581 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003582 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003583 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003584 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003585 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003586 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003587 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003588 }
3589 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003590 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 return true;
3592 } else {
3593 return false;
3594 }
3595 }
3596
Michael Jurkac402cd92013-05-20 15:49:32 +02003597 private boolean waitUntilResume(Runnable run) {
3598 return waitUntilResume(run, false);
3599 }
3600
Michael Jurka1e2f4652013-07-08 18:03:46 -07003601 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003602 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003603 }
3604
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003606 * If the activity is currently paused, signal that we need to re-run the loader
3607 * in onResume.
3608 *
3609 * This needs to be called from incoming places where resources might have been loaded
3610 * while we are paused. That is becaues the Configuration might be wrong
3611 * when we're not running, and if it comes back to what it was when we
3612 * were paused, we are not restarted.
3613 *
3614 * Implementation of the method from LauncherModel.Callbacks.
3615 *
3616 * @return true if we are currently paused. The caller might be able to
3617 * skip some work in that case since we will come back again.
3618 */
3619 public boolean setLoadOnResume() {
3620 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003621 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003622 mOnResumeNeedsLoad = true;
3623 return true;
3624 } else {
3625 return false;
3626 }
3627 }
3628
3629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003633 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003634 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003635 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003636 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003637 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003639
Joe Onorato9c1289c2009-08-17 11:03:03 -04003640 /**
Sunny Goyal527c7d32015-08-28 15:19:36 -07003641 * Clear any pending bind callbacks. This is called when is loader is planning to
3642 * perform a full rebind from scratch.
3643 */
3644 @Override
3645 public void clearPendingBinds() {
3646 mBindOnResumeCallbacks.clear();
3647 if (mPendingExecutor != null) {
3648 mPendingExecutor.markCompleted();
3649 mPendingExecutor = null;
3650 }
3651 }
3652
3653 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 * Refreshes the shortcuts shown on the workspace.
3655 *
3656 * Implementation of the method from LauncherModel.Callbacks.
3657 */
3658 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003659 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003660
Winson Chungd64d1762013-08-20 14:37:16 -07003661 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003662 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003663 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003664
Michael Jurka05bf644e2011-11-30 20:28:53 -08003665 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003666 if (mHotseat != null) {
3667 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003668 }
3669 }
3670
Adam Cohendcd297f2013-06-18 13:13:40 -07003671 @Override
3672 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003673 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003674
Adam Cohen5084cba2013-09-03 12:01:16 -07003675 // If there are no screens, we need to have an empty screen
3676 if (orderedScreenIds.size() == 0) {
3677 mWorkspace.addExtraEmptyScreen();
3678 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003679
3680 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003681 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003682 if (hasCustomContentToLeft()) {
3683 mWorkspace.createCustomContentContainer();
3684 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003685 }
Winson Chung64359a52013-07-08 17:17:08 -07003686 }
3687
3688 @Override
3689 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003690 int count = orderedScreenIds.size();
3691 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003692 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003693 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003694 }
3695
Winson Chungd64d1762013-08-20 14:37:16 -07003696 public void bindAppsAdded(final ArrayList<Long> newScreens,
3697 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003698 final ArrayList<ItemInfo> addAnimated,
3699 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003700 Runnable r = new Runnable() {
3701 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003702 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003703 }
3704 };
3705 if (waitUntilResume(r)) {
3706 return;
3707 }
3708
Winson Chungd64d1762013-08-20 14:37:16 -07003709 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003710 if (newScreens != null) {
3711 bindAddScreens(newScreens);
3712 }
Winson Chungd64d1762013-08-20 14:37:16 -07003713
3714 // We add the items without animation on non-visible pages, and with
3715 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003716 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003717 bindItems(addNotAnimated, 0,
3718 addNotAnimated.size(), false);
3719 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003720 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003721 bindItems(addAnimated, 0,
3722 addAnimated.size(), true);
3723 }
Winson Chungc58497e2013-09-03 17:48:37 -07003724
Winson Chung87412982013-10-03 18:34:14 -07003725 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003726 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003727
Winson Chungb745afb2015-03-02 11:51:23 -08003728 if (addedApps != null && mAppsView != null) {
3729 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003730 }
Winson Chungd64d1762013-08-20 14:37:16 -07003731 }
3732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 * Bind the items start-end from the list.
3735 *
3736 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 */
Winson Chung64359a52013-07-08 17:17:08 -07003738 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3739 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 Runnable r = new Runnable() {
3741 public void run() {
3742 bindItems(shortcuts, start, end, forceAnimateIcons);
3743 }
3744 };
3745 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 return;
3747 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003748
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003750 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3751 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003752 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003754 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003755 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003757
3758 // Short circuit if we are loading dock items for a configuration which has no dock
3759 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3760 mHotseat == null) {
3761 continue;
3762 }
3763
Sunny Goyal639e9062015-08-19 19:17:06 -07003764 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 switch (item.itemType) {
3766 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3767 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003768 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003769 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003770
Winson Chung64359a52013-07-08 17:17:08 -07003771 /*
3772 * TODO: FIX collision case
3773 */
Winson Chungce376632013-07-11 16:12:41 -07003774 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3775 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3776 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003777 View v = cl.getChildAt(item.cellX, item.cellY);
3778 Object tag = v.getTag();
3779 String desc = "Collision while binding workspace item: " + item
3780 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003781 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003782 throw (new RuntimeException(desc));
3783 } else {
3784 Log.d(TAG, desc);
3785 }
Winson Chungce376632013-07-11 16:12:41 -07003786 }
Winson Chung64359a52013-07-08 17:17:08 -07003787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003789 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003790 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003791 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003792 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003794 default:
3795 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003797
3798 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3799 item.cellY, 1, 1);
3800 if (animateIcons) {
3801 // Animate all the applications up now
3802 view.setAlpha(0f);
3803 view.setScaleX(0f);
3804 view.setScaleY(0f);
3805 bounceAnims.add(createNewAppBounceAnimation(view, i));
3806 newShortcutsScreenId = item.screenId;
3807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003808 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809
Winson Chung997a9232013-07-24 15:33:46 -07003810 if (animateIcons) {
3811 // Animate to the correct page
3812 if (newShortcutsScreenId > -1) {
3813 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003814 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003815 final Runnable startBounceAnimRunnable = new Runnable() {
3816 public void run() {
3817 anim.playTogether(bounceAnims);
3818 anim.start();
3819 }
3820 };
Winson Chung997a9232013-07-24 15:33:46 -07003821 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003822 // We post the animation slightly delayed to prevent slowdowns
3823 // when we are loading right after we return to launcher.
3824 mWorkspace.postDelayed(new Runnable() {
3825 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003826 if (mWorkspace != null) {
3827 mWorkspace.snapToPage(newScreenIndex);
3828 mWorkspace.postDelayed(startBounceAnimRunnable,
3829 NEW_APPS_ANIMATION_DELAY);
3830 }
Winson Chung94d67682013-09-25 16:29:40 -07003831 }
3832 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003833 } else {
3834 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003835 }
3836 }
Winson Chung64359a52013-07-08 17:17:08 -07003837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003839 }
3840
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841 /**
3842 * Implementation of the method from LauncherModel.Callbacks.
3843 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003844 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003845 Runnable r = new Runnable() {
3846 public void run() {
3847 bindFolders(folders);
3848 }
3849 };
3850 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003851 return;
3852 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003853 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855
3856 /**
3857 * Add the views for a widget to the workspace.
3858 *
3859 * Implementation of the method from LauncherModel.Callbacks.
3860 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003861 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003862 Runnable r = new Runnable() {
3863 public void run() {
3864 bindAppWidget(item);
3865 }
3866 };
3867 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003868 return;
3869 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003870
Daniel Sandler843e8602010-06-07 14:59:01 -04003871 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3872 if (DEBUG_WIDGETS) {
3873 Log.d(TAG, "bindAppWidget: " + item);
3874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 final Workspace workspace = mWorkspace;
3876
Adam Cohen59400422014-03-05 18:07:04 -08003877 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003878 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003879
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003880 if (!mIsSafeModeEnabled
3881 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003882 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3883
Sunny Goyalff572272014-07-23 13:58:07 -07003884 if (appWidgetInfo == null) {
3885 if (DEBUG_WIDGETS) {
3886 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3887 + " belongs to component " + item.providerName
3888 + ", as the povider is null");
3889 }
3890 LauncherModel.deleteItemFromDatabase(this, item);
3891 return;
3892 }
Sunny Goyalff572272014-07-23 13:58:07 -07003893
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003894 // If we do not have a valid id, try to bind an id.
3895 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3896 // Note: This assumes that the id remap broadcast is received before this step.
3897 // If that is not the case, the id remap will be ignored and user may see the
3898 // click to setup view.
3899 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3900 pendingInfo.spanX = item.spanX;
3901 pendingInfo.spanY = item.spanY;
3902 pendingInfo.minSpanX = item.minSpanX;
3903 pendingInfo.minSpanY = item.minSpanY;
3904 Bundle options = null;
3905 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003906
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003907 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3908 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3909 newWidgetId, appWidgetInfo, options);
3910
3911 // TODO consider showing a permission dialog when the widget is clicked.
3912 if (!success) {
3913 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3914 if (DEBUG_WIDGETS) {
3915 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3916 + " belongs to component " + item.providerName
3917 + ", as the launcher is unable to bing a new widget id");
3918 }
3919 LauncherModel.deleteItemFromDatabase(this, item);
3920 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003921 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003922
3923 item.appWidgetId = newWidgetId;
3924
3925 // If the widget has a configure activity, it is still needs to set it up, otherwise
3926 // the widget is ready to go.
3927 item.restoreStatus = (appWidgetInfo.configure == null)
3928 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3929 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3930
3931 LauncherModel.updateItemInDatabase(this, item);
3932 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3933 && (appWidgetInfo.configure == null)) {
3934 // If the ID is already valid, verify if we need to configure or not.
3935 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3936 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003937 }
Sunny Goyalff572272014-07-23 13:58:07 -07003938 }
3939
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003940 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003941 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003942 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003943 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3944 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003945 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003946
Sunny Goyal651077b2014-06-30 14:15:31 -07003947 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003948 item.minSpanX = appWidgetInfo.minSpanX;
3949 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003950 } else {
3951 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003952 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3953 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003954 view.updateIcon(mIconCache);
3955 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003956 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003957 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003961 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962
Adam Cohendcd297f2013-06-18 13:13:40 -07003963 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003965 if (!item.isCustomWidget()) {
3966 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3967 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003968
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 workspace.requestLayout();
3970
Daniel Sandler843e8602010-06-07 14:59:01 -04003971 if (DEBUG_WIDGETS) {
3972 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3973 + (SystemClock.uptimeMillis()-start) + "ms");
3974 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 }
3976
Sunny Goyalff572272014-07-23 13:58:07 -07003977 /**
3978 * Restores a pending widget.
3979 *
3980 * @param appWidgetId The app widget id
3981 * @param cellInfo The position on screen where to create the widget.
3982 */
3983 private void completeRestoreAppWidget(final int appWidgetId) {
3984 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3985 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3986 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3987 return;
3988 }
3989
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003990 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003991 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3992
3993 mWorkspace.reinflateWidgetsIfNecessary();
3994 LauncherModel.updateItemInDatabase(this, info);
3995 }
3996
Adam Cohen1462de32012-07-24 22:34:36 -07003997 public void onPageBoundSynchronously(int page) {
3998 mSynchronouslyBoundPages.add(page);
3999 }
4000
Sunny Goyal527c7d32015-08-28 15:19:36 -07004001 @Override
4002 public void executeOnNextDraw(ViewOnDrawExecutor executor) {
4003 if (mPendingExecutor != null) {
4004 mPendingExecutor.markCompleted();
4005 }
4006 mPendingExecutor = executor;
4007 executor.attachTo(this);
4008 }
4009
4010 public void clearPendingExecutor(ViewOnDrawExecutor executor) {
4011 if (mPendingExecutor == executor) {
4012 mPendingExecutor = null;
4013 }
4014 }
4015
Joe Onorato9c1289c2009-08-17 11:03:03 -04004016 /**
4017 * Callback saying that there aren't any more items to bind.
4018 *
4019 * Implementation of the method from LauncherModel.Callbacks.
4020 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004021 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004022 Runnable r = new Runnable() {
4023 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004024 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004025 }
4026 };
4027 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004028 return;
4029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 if (mSavedState != null) {
4031 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004032 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004033 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034 mSavedState = null;
4035 }
4036
Adam Cohen1462de32012-07-24 22:34:36 -07004037 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004039 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004040 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004041
4042 // If we received the result of any pending adds while the loader was running (e.g. the
4043 // widget configuration forced an orientation change), process them now.
4044 if (sPendingAddItem != null) {
4045 final long screenId = completeAdd(sPendingAddItem);
4046
4047 // TODO: this moves the user to the page where the pending item was added. Ideally,
4048 // the screen would be guaranteed to exist after bind, and the page would be set through
4049 // the workspace restore process.
4050 mWorkspace.post(new Runnable() {
4051 @Override
4052 public void run() {
4053 mWorkspace.snapToScreenId(screenId);
4054 }
4055 });
4056 sPendingAddItem = null;
4057 }
4058
Sunny Goyal756adbc2015-04-16 15:20:51 -07004059 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004060
4061 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004062 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004063 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004064 }
4065
Adam Cohen3ed316a2014-07-23 18:21:20 -07004066 private void sendLoadingCompleteBroadcastIfNecessary() {
4067 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4068 String permission =
4069 getResources().getString(R.string.receive_first_load_broadcast_permission);
4070 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4071 sendBroadcast(intent, permission);
4072 SharedPreferences.Editor editor = mSharedPrefs.edit();
4073 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4074 editor.apply();
4075 }
4076 }
4077
Winson Chunga0b7e862013-09-05 16:03:15 -07004078 public boolean isAllAppsButtonRank(int rank) {
4079 if (mHotseat != null) {
4080 return mHotseat.isAllAppsButtonRank(rank);
4081 }
4082 return false;
4083 }
4084
Winson Chunga2413752012-04-03 14:22:34 -07004085 private boolean canRunNewAppsAnimation() {
4086 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004087 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4088 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004089 }
4090
Winson Chungc9168342013-06-26 14:54:55 -07004091 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004092 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004093 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4094 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004095 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004096 return bounceAnim;
4097 }
4098
Adam Cohen27772732013-11-11 14:00:56 +00004099 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004100 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004101 }
4102
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004103 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004104 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004105 }
4106
Winson Chung88fa7412015-08-03 14:25:28 -07004107 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004108 if (mSearchDropTargetBar == null) {
4109 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004110 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004111 if (mQsb != null) {
4112 mSearchDropTargetBar.removeView(mQsb);
4113 mQsb = null;
4114 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004115 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004116 }
4117
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004118 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004119 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4120 * multiple calls to bind the same list.)
4121 */
4122 @Thunk ArrayList<AppInfo> mTmpAppsList;
4123 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4124 public void run() {
4125 bindAllApplications(mTmpAppsList);
4126 mTmpAppsList = null;
4127 }
4128 };
4129
4130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 * Add the icons for all apps.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004135 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004136 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4137 mTmpAppsList = apps;
4138 return;
4139 }
4140
Winson Chungb745afb2015-03-02 11:51:23 -08004141 if (mAppsView != null) {
4142 mAppsView.setApps(apps);
4143 }
Adam Cohen9211d422014-10-07 18:14:53 -07004144 if (mLauncherCallbacks != null) {
4145 mLauncherCallbacks.bindAllApplications(apps);
4146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004147 }
4148
4149 /**
4150 * A package was updated.
4151 *
4152 * Implementation of the method from LauncherModel.Callbacks.
4153 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004154 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004155 Runnable r = new Runnable() {
4156 public void run() {
4157 bindAppsUpdated(apps);
4158 }
4159 };
4160 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004161 return;
4162 }
4163
Winson Chungb745afb2015-03-02 11:51:23 -08004164 if (mAppsView != null) {
4165 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 }
4168
Sunny Goyal4390ace2014-10-13 11:33:11 -07004169 @Override
4170 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindWidgetsRestored(widgets);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
4177 return;
4178 }
4179 mWorkspace.widgetsRestored(widgets);
4180 }
4181
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004183 * Some shortcuts were updated in the background.
4184 *
4185 * Implementation of the method from LauncherModel.Callbacks.
4186 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004187 @Override
4188 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4189 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004190 Runnable r = new Runnable() {
4191 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004192 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004193 }
4194 };
4195 if (waitUntilResume(r)) {
4196 return;
4197 }
4198
Sunny Goyal4390ace2014-10-13 11:33:11 -07004199 if (!updated.isEmpty()) {
4200 mWorkspace.updateShortcuts(updated);
4201 }
4202
4203 if (!removed.isEmpty()) {
4204 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4205 for (ShortcutInfo si : removed) {
4206 removedComponents.add(si.getTargetComponent());
4207 }
4208 mWorkspace.removeItemsByComponentName(removedComponents, user);
4209 // Notify the drag controller
4210 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004211 }
4212 }
4213
4214 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004215 * Update the state of a package, typically related to install state.
4216 *
4217 * Implementation of the method from LauncherModel.Callbacks.
4218 */
Sunny Goyale755d462014-07-22 13:48:29 -07004219 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004220 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4221 Runnable r = new Runnable() {
4222 public void run() {
4223 bindRestoreItemsChange(updates);
4224 }
4225 };
4226 if (waitUntilResume(r)) {
4227 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004228 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004229
Sunny Goyal756adbc2015-04-16 15:20:51 -07004230 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004231 }
4232
4233 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004234 * A package was uninstalled. We take both the super set of packageNames
4235 * in addition to specific applications to remove, the reason being that
4236 * this can be called when a package is updated as well. In that scenario,
4237 * we only remove specific components from the workspace, where as
4238 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004239 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004240 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004241 * Implementation of the method from LauncherModel.Callbacks.
4242 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004243 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004244 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004245 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004246 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004247 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004248 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004249 }
Winson Chungd64d1762013-08-20 14:37:16 -07004250 };
4251 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004252 return;
4253 }
4254
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004256 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4257 for (AppInfo info : appInfos) {
4258 removedComponents.add(info.componentName);
4259 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004260 if (!packageNames.isEmpty()) {
4261 mWorkspace.removeItemsByPackageName(packageNames, user);
4262 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004263 if (!removedComponents.isEmpty()) {
4264 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 // Notify the drag controller
4267 mDragController.onAppsRemoved(packageNames, removedComponents);
4268
Sunny Goyal1a745e82014-10-02 15:58:31 -07004269 } else {
4270 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004271 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004272
Winson Chungdf95eb12013-10-16 14:57:07 -07004273 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004274 if (mAppsView != null) {
4275 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004277 }
Joe Onoratobe386092009-11-17 17:32:16 -08004278
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004280 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004281 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004282 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004283 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004284
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004285 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004286 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004287 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004288 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004289 return;
4290 }
4291
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004292 if (mWidgetsView != null && model != null) {
4293 mWidgetsView.addWidgets(model);
4294 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004295 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004296 }
4297
Winson Chung400438b2011-01-16 17:53:48 -08004298 private int mapConfigurationOriActivityInfoOri(int configOri) {
4299 final Display d = getWindowManager().getDefaultDisplay();
4300 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4301 switch (d.getRotation()) {
4302 case Surface.ROTATION_0:
4303 case Surface.ROTATION_180:
4304 // We are currently in the same basic orientation as the natural orientation
4305 naturalOri = configOri;
4306 break;
4307 case Surface.ROTATION_90:
4308 case Surface.ROTATION_270:
4309 // We are currently in the other basic orientation to the natural orientation
4310 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4311 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4312 break;
4313 }
4314
4315 int[] oriMap = {
4316 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4317 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4318 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4319 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4320 };
4321 // Since the map starts at portrait, we need to offset if this device's natural orientation
4322 // is landscape.
4323 int indexOffset = 0;
4324 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4325 indexOffset = 1;
4326 }
4327 return oriMap[(d.getRotation() + indexOffset) % 4];
4328 }
Adam Cohen446e9402011-09-15 18:21:21 -07004329
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004330 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004331 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004332 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004333 if (Utilities.ATLEAST_JB_MR2) {
4334 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4335 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004336 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4337 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004338 }
Winson Chung4b919f82012-05-01 10:44:08 -07004339 }
4340 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004341
Winson Chung4b919f82012-05-01 10:44:08 -07004342 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004343 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004344 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004345 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004346 } else {
4347 mHandler.postDelayed(new Runnable() {
4348 public void run() {
4349 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4350 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004351 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004352 }
Winson Chung4b919f82012-05-01 10:44:08 -07004353 }
Winson Chung400438b2011-01-16 17:53:48 -08004354 }
4355
Winson Chunge43a1e72014-01-15 10:33:02 -08004356 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004357 if (mLauncherCallbacks != null) {
4358 return mLauncherCallbacks.isLauncherPreinstalled();
4359 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004360 PackageManager pm = getPackageManager();
4361 try {
4362 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4363 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4364 return true;
4365 } else {
4366 return false;
4367 }
4368 } catch (NameNotFoundException e) {
4369 e.printStackTrace();
4370 return false;
4371 }
4372 }
4373
Adam Cohenea90f832014-05-21 15:03:34 -07004374 /**
4375 * This method indicates whether or not we should suggest default wallpaper dimensions
4376 * when our wallpaper cropper was not yet used to set a wallpaper.
4377 */
4378 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004379 if (mLauncherCallbacks != null) {
4380 return mLauncherCallbacks.overrideWallpaperDimensions();
4381 }
Adam Cohenea90f832014-05-21 15:03:34 -07004382 return true;
4383 }
4384
Adam Cohen432609a2014-03-13 17:03:22 -07004385 /**
4386 * To be overridden by subclasses to indicate that there is an activity to launch
4387 * before showing the standard launcher experience.
4388 */
4389 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004390 if (mLauncherCallbacks != null) {
4391 return mLauncherCallbacks.hasFirstRunActivity();
4392 }
Adam Cohen432609a2014-03-13 17:03:22 -07004393 return false;
4394 }
4395
4396 /**
4397 * To be overridden by subclasses to launch any first run activity
4398 */
4399 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004400 if (mLauncherCallbacks != null) {
4401 return mLauncherCallbacks.getFirstRunActivity();
4402 }
Adam Cohen432609a2014-03-13 17:03:22 -07004403 return null;
4404 }
4405
Winson Chunga6945242014-01-08 14:04:34 -08004406 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004407 return !ActivityManager.isRunningInTestHarness() &&
4408 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004409 }
4410
Adam Cohen4a9423d2014-03-28 14:22:31 -07004411 protected boolean hasRunFirstRunActivity() {
4412 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4413 }
4414
Adam Cohen432609a2014-03-13 17:03:22 -07004415 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004416 if (shouldRunFirstRunActivity() &&
4417 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004418 Intent firstRunIntent = getFirstRunActivity();
4419 if (firstRunIntent != null) {
4420 startActivity(firstRunIntent);
4421 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004422 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004423 }
4424 }
Adam Cohen432609a2014-03-13 17:03:22 -07004425 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004426 }
4427
4428 private void markFirstRunActivityShown() {
4429 SharedPreferences.Editor editor = mSharedPrefs.edit();
4430 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4431 editor.apply();
4432 }
4433
Adam Cohen432609a2014-03-13 17:03:22 -07004434 /**
4435 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4436 * screen that must be displayed and dismissed.
4437 */
4438 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004439 if (mLauncherCallbacks != null) {
4440 return mLauncherCallbacks.hasDismissableIntroScreen();
4441 }
Adam Cohen432609a2014-03-13 17:03:22 -07004442 return false;
4443 }
4444
4445 /**
4446 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4447 */
4448 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004449 if (mLauncherCallbacks != null) {
4450 return mLauncherCallbacks.getIntroScreen();
4451 }
Adam Cohen432609a2014-03-13 17:03:22 -07004452 return null;
4453 }
4454
4455 /**
4456 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4457 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4458 */
4459 private boolean shouldShowIntroScreen() {
4460 return hasDismissableIntroScreen() &&
4461 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4462 }
4463
4464 protected void showIntroScreen() {
4465 View introScreen = getIntroScreen();
4466 changeWallpaperVisiblity(false);
4467 if (introScreen != null) {
4468 mDragLayer.showOverlayView(introScreen);
4469 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004470 if (mLauncherOverlayContainer != null) {
4471 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4472 }
Adam Cohen432609a2014-03-13 17:03:22 -07004473 }
4474
4475 public void dismissIntroScreen() {
4476 markIntroScreenDismissed();
4477 if (showFirstRunActivity()) {
4478 // We delay hiding the intro view until the first run activity is showing. This
4479 // avoids a blip.
4480 mWorkspace.postDelayed(new Runnable() {
4481 @Override
4482 public void run() {
4483 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004484 if (mLauncherOverlayContainer != null) {
4485 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4486 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004487 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004488 }
4489 }, ACTIVITY_START_DELAY);
4490 } else {
4491 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004492 if (mLauncherOverlayContainer != null) {
4493 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4494 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004495 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004496 }
4497 changeWallpaperVisiblity(true);
4498 }
4499
4500 private void markIntroScreenDismissed() {
4501 SharedPreferences.Editor editor = mSharedPrefs.edit();
4502 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4503 editor.apply();
4504 }
4505
Adam Cohen091440a2015-03-18 14:16:05 -07004506 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004507 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4508 // on the device, then we always show the first run cling experience (or if there is no
4509 // launcher2). Otherwise, we prompt the user upon started for migration
4510 LauncherClings launcherClings = new LauncherClings(this);
4511 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004512 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004513 if (mModel.canMigrateFromOldLauncherDb(this)) {
4514 launcherClings.showMigrationCling();
4515 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004516 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004517 }
4518 }
4519 }
4520
Winson Chunga6945242014-01-08 14:04:34 -08004521 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004522 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4523 if (mHotseat != null) mHotseat.setAlpha(1f);
4524 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004525 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4526 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004527 }
4528
4529 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004530 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4531 if (mHotseat != null) mHotseat.setAlpha(0f);
4532 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004533 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4534 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004535 }
4536
Winson Chung5ffd51d2015-06-25 11:36:50 -07004537 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004538 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004539 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004540 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004541 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004542 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004543 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4544 if (userHandle != null) {
4545 user = UserHandleCompat.fromUser(userHandle);
4546 }
4547 }
4548 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004549 }
4550
4551 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004552 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004553 if (user == null) {
4554 user = UserHandleCompat.myUserHandle();
4555 }
4556
4557 // Called from search suggestion, add the profile extra to the intent to ensure that we
4558 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004559 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004560 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004561 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004562 return null;
4563 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004564 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004565 }
4566
Winson Chung5ffd51d2015-06-25 11:36:50 -07004567 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004568 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4569 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004570 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004571 UserHandleCompat.myUserHandle());
4572 }
4573
Winson Chung5ffd51d2015-06-25 11:36:50 -07004574 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004575 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4576 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004577 mWorkspace.onExternalDragStartedWithItem(dragView);
4578 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004579 }
4580
Winson Chung5ffd51d2015-06-25 11:36:50 -07004581 protected void moveWorkspaceToDefaultScreen() {
4582 mWorkspace.moveToDefaultScreen(false);
4583 }
4584
Anjali Koppalf5d29132014-02-28 13:33:27 -08004585 @Override
4586 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004587 if (mLauncherCallbacks != null) {
4588 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4589 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004590 }
4591
Winson Chung80baf5a2010-08-09 16:03:15 -07004592 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004593 * Returns a FastBitmapDrawable with the icon, accurately sized.
4594 */
4595 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4596 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4597 d.setFilterBitmap(true);
4598 resizeIconDrawable(d);
4599 return d;
4600 }
4601
4602 /**
4603 * Resizes an icon drawable to the correct icon size.
4604 */
Winson5fbe0742015-09-30 15:33:00 -07004605 public Drawable resizeIconDrawable(Drawable icon) {
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004606 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
Winson5fbe0742015-09-30 15:33:00 -07004607 return icon;
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004608 }
4609
4610 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004611 * Prints out out state for debugging.
4612 */
4613 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004614 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004615 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004616 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4617 Log.d(TAG, "mRestoring=" + mRestoring);
4618 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004619 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004620 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004621 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004622
Daniel Sandler325dc232013-06-05 22:57:57 -04004623 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004624 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004625
4626 @Override
4627 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4628 super.dump(prefix, fd, writer, args);
Sunny Goyala1365452015-10-01 15:46:24 -07004629 // Dump workspace
4630 writer.println(prefix + "Workspace Items");
4631 for (int i = mWorkspace.numCustomPages(); i < mWorkspace.getPageCount(); i++) {
4632 writer.println(prefix + " Homescreen " + i);
4633
4634 ViewGroup layout = ((CellLayout) mWorkspace.getPageAt(i)).getShortcutsAndWidgets();
4635 for (int j = 0; j < layout.getChildCount(); j++) {
4636 Object tag = layout.getChildAt(j).getTag();
4637 if (tag != null) {
4638 writer.println(prefix + " " + tag.toString());
4639 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004640 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004641 }
Sunny Goyala1365452015-10-01 15:46:24 -07004642
4643 writer.println(prefix + " Hotseat");
4644 ViewGroup layout = mHotseat.getLayout().getShortcutsAndWidgets();
4645 for (int j = 0; j < layout.getChildCount(); j++) {
4646 Object tag = layout.getChildAt(j).getTag();
4647 if (tag != null) {
4648 writer.println(prefix + " " + tag.toString());
4649 }
4650 }
4651
4652 synchronized (sDumpLogs) {
4653 writer.println();
4654 writer.println(prefix + "Debug logs");
4655 for (String log : sDumpLogs) {
4656 writer.println(prefix + " " + log);
4657 }
4658 }
4659
Adam Cohen9211d422014-10-07 18:14:53 -07004660 if (mLauncherCallbacks != null) {
4661 mLauncherCallbacks.dump(prefix, fd, writer, args);
4662 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004663 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004664
Sunny Goyala1365452015-10-01 15:46:24 -07004665 public static void addDumpLog(String tag, String log) {
4666 Log.d(tag, log);
4667 synchronized(sDumpLogs) {
Winson Chungede41292013-09-19 16:27:36 -07004668 sDateStamp.setTime(System.currentTimeMillis());
Sunny Goyala1365452015-10-01 15:46:24 -07004669 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Winson Chungede41292013-09-19 16:27:36 -07004670 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004671 }
4672
Adam Cohen59400422014-03-05 18:07:04 -08004673 public static CustomAppWidget getCustomAppWidget(String name) {
4674 return sCustomAppWidgets.get(name);
4675 }
4676
4677 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4678 return sCustomAppWidgets;
4679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004680}
Michael Jurka2763be32011-02-24 11:19:57 -08004681
Dan Sandlerd5024042014-01-09 15:01:33 -05004682interface DebugIntents {
4683 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4684 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004685}