blob: 80242332c19c354ec056db771809b886e1ae4fde [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Sunny Goyal756cd262015-08-20 12:33:21 -070038import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070067import android.os.UserHandle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070089import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -070099import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal6c56c682015-07-16 14:09:05 -0700105import com.android.launcher3.config.ProviderConfig;
Vadim Tryshevfedca432015-08-19 17:55:02 -0700106import com.android.launcher3.dragndrop.DragController;
107import com.android.launcher3.dragndrop.DragLayer;
108import com.android.launcher3.dragndrop.DragView;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700109import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700110import com.android.launcher3.util.ComponentKey;
Sunny Goyale2df0622015-04-24 11:27:00 -0700111import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700112import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700114import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700115import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700116
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Vadim Tryshevfedca432015-08-19 17:55:02 -0700137 public static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700141 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700156 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
157 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
158 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
159
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700160 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
161
Mathew Inwood876a8462013-06-14 14:12:41 +0100162 /**
163 * IntentStarter uses request codes starting with this. This must be greater than all activity
164 * request codes used internally.
165 */
166 protected static final int REQUEST_LAST = 100;
167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
Sunny Goyal756cd262015-08-20 12:33:21 -0700180 // Type: Content Values / parcelable
181 private static final String RUNTIME_STATE_PENDING_ADD_ITEM = "launcher.add_item";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000184 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000186 // Type: int[]
187 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Adam Cohen432609a2014-03-13 17:03:22 -0700189 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800190 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
191
Adam Cohen3ed316a2014-07-23 18:21:20 -0700192 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
193 static final String ACTION_FIRST_LOAD_COMPLETE =
194 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
195
Adam Cohen39a06042013-07-19 14:30:12 -0700196 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700197 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700198
Sunny Goyal594d76d2014-11-06 10:12:54 -0800199 private static final String QSB_WIDGET_ID = "qsb_widget_id";
200 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
201
Winson Chunga6945242014-01-08 14:04:34 -0800202 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
203
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700205 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
206
Adam Cohen091440a2015-03-18 14:16:05 -0700207 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700208 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700210 private boolean mIsSafeModeEnabled;
211
Adam Cohenc2d6e892014-10-16 09:49:24 -0700212 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
213 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700214 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
218 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700219 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000221 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
222 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
223
Winson Chunga2413752012-04-03 14:22:34 -0700224 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700225 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
226 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700228
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800229 private final BroadcastReceiver mCloseSystemDialogsReceiver
230 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800231
Adam Cohen091440a2015-03-18 14:16:05 -0700232 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800234 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700237 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Sunny Goyalffe83f12014-08-14 17:39:34 -0700239 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700242 @Thunk final ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800243 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800244 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245
Michael Jurka0280c3b2010-09-17 15:00:07 -0700246 private int[] mTmpAddItemCellCoordinates = new int[2];
247
Sunny Goyal316490e2015-06-02 09:38:28 -0700248 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800249 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700250
Michael Jurka838a4ca2011-02-07 13:33:06 -0800251 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700252 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700253
Winson Chungc51db6a2011-10-05 11:44:49 -0700254 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700255
256 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700257 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700258
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700259 // Main container view and the model for the widget tray screen.
260 @Thunk WidgetsContainerView mWidgetsView;
261 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700262
Sunny Goyal594d76d2014-11-06 10:12:54 -0800263 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700266 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
267 // scroll issues (because the workspace may not have been measured yet) and extra work.
268 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
269 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
271 private SpannableStringBuilder mDefaultKeySsb = null;
272
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800275 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private boolean mRestoring;
277 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700278 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Michael Jurka1e2f4652013-07-08 18:03:46 -0700280 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700281 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800284 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700287 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800288 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289
Sunny Goyal639e9062015-08-19 19:17:06 -0700290 private LauncherClings mClings;
291
Sunny Goyale2df0622015-04-24 11:27:00 -0700292 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700293
Adam Cohen61f560d2013-09-30 15:58:20 -0700294 private View.OnTouchListener mHapticFeedbackTouchListener;
295
Adam Cohended9f8d2010-11-03 13:25:16 -0700296 // Related to the auto-advancing of widgets
297 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700298 private static final int ADVANCE_INTERVAL = 20000;
299 private static final int ADVANCE_STAGGER = 250;
300
301 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700304 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700308 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800309
Adam Cohen091440a2015-03-18 14:16:05 -0700310 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700311
Adam Cohen1462de32012-07-24 22:34:36 -0700312 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700313 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700314
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700315 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700316 static Date sDateStamp = new Date();
317 static DateFormat sDateFormat =
318 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
319 static long sRunStart = System.currentTimeMillis();
320 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700321
Winson Chung46353de2012-02-16 14:05:10 -0800322 // We only want to get the SharedPreferences once since it does an FS stat each time we get
323 // it from the context.
324 private SharedPreferences mSharedPrefs;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700333 private DeviceProfile mDeviceProfile;
334
Winson Chung13eb5272015-05-11 16:30:13 -0700335 // This is set to the view that launched the activity that navigated the user away from
336 // launcher. Since there is no callback for when the activity has finished launching, enable
337 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 private BubbleTextView mWaitingForResume;
339
Adam Cohen59400422014-03-05 18:07:04 -0800340 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
341 new HashMap<String, CustomAppWidget>();
342
343 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
344 static {
345 if (ENABLE_CUSTOM_WIDGET_TEST) {
346 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
347 }
348 }
349
Adam Cohen091440a2015-03-18 14:16:05 -0700350 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700352 if (mWorkspace != null) {
353 mWorkspace.buildPageHardwareLayers();
354 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 }
356 };
357
Adam Cohendb364c32014-05-20 14:23:40 -0700358 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359
Adam Cohen091440a2015-03-18 14:16:05 -0700360 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int requestCode;
362 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700363 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 int cellX;
366 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700367 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700371 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700372 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400373
374 @Thunk void setOrientation() {
375 if (mRotationEnabled) {
376 unlockScreenOrientation(true);
377 } else {
378 setRequestedOrientation(
379 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400381 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700382
Sunny Goyal7779d622015-06-11 16:18:39 -0700383 private Runnable mUpdateOrientationRunnable = new Runnable() {
384 public void run() {
385 setOrientation();
386 }
387 };
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Adam Cohen2e6da152015-05-06 11:42:25 -0700415 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700416 mDeviceProfile = getResources().getConfiguration().orientation
417 == Configuration.ORIENTATION_LANDSCAPE ?
418 app.getInvariantDeviceProfile().landscapeProfile
419 : app.getInvariantDeviceProfile().portraitProfile;
420
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700422 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700423 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800424 mModel = app.setLauncher(this);
425 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700426
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700428 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
431
Sunny Goyalffe83f12014-08-14 17:39:34 -0700432 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
435 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700437 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
438 // this also ensures that any synchronous binding below doesn't re-trigger another
439 // LauncherModel load.
440 mPaused = false;
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200443 android.os.Debug.startMethodTracing(
444 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700450 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700465 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700469 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700480 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
481 // In case we are on a device with locked rotation, we should look at preferences to check
482 // if the user has specifically allowed rotation.
483 if (!mRotationEnabled) {
Sunny Goyal21bf5312015-08-21 11:08:24 -0700484 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700485 }
486
487 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
488 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400489 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 if (mLauncherCallbacks.hasLauncherOverlay()) {
494 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700495 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
496 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
497 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700498 mWorkspace.setLauncherOverlay(mLauncherOverlay);
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700501
502 if (shouldShowIntroScreen()) {
503 showIntroScreen();
504 } else {
505 showFirstRunActivity();
506 showFirstRunClings();
507 }
Adam Cohen9211d422014-10-07 18:14:53 -0700508 }
509
Sunny Goyal7779d622015-06-11 16:18:39 -0700510 @Override
511 public void onSettingsChanged(String settings, boolean value) {
512 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
513 mRotationEnabled = value;
514 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
515 mUpdateOrientationRunnable.run();
516 }
517 }
518 }
519
Adam Cohen9211d422014-10-07 18:14:53 -0700520 private LauncherCallbacks mLauncherCallbacks;
521
522 public void onPostCreate(Bundle savedInstanceState) {
523 super.onPostCreate(savedInstanceState);
524 if (mLauncherCallbacks != null) {
525 mLauncherCallbacks.onPostCreate(savedInstanceState);
526 }
527 }
528
529 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
530 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700531 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700532 private boolean mWorkspaceImportanceStored = false;
533 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700534 private int mWorkspaceImportanceForAccessibility =
535 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
536 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
537
538 @Override
539 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700540 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700541 return;
542 }
543 // The underlying workspace and hotseat are temporarily suppressed by the search
544 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700545 if (mWorkspace != null) {
546 mWorkspaceImportanceForAccessibility =
547 mWorkspace.getImportantForAccessibility();
548 mWorkspace.setImportantForAccessibility(
549 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
550 mWorkspaceImportanceStored = true;
551 }
552 if (mHotseat != null) {
553 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
554 mHotseat.setImportantForAccessibility(
555 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
556 mHotseatImportanceStored = true;
557 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700558 }
559
560 @Override
561 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700562 if (mWorkspaceImportanceStored && mWorkspace != null) {
563 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
564 }
565 if (mHotseatImportanceStored && mHotseat != null) {
566 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
567 }
568 mWorkspaceImportanceStored = false;
569 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700570 }
571 });
Adam Cohen9211d422014-10-07 18:14:53 -0700572 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700573 }
574
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700575 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700576 public void onLauncherProviderChange() {
577 if (mLauncherCallbacks != null) {
578 mLauncherCallbacks.onLauncherProviderChange();
579 }
580 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700581
Winson Chungef7f8742015-06-04 17:18:17 -0700582 /**
583 * Updates the bounds of all the overlays to match the new fixed bounds.
584 */
585 public void updateOverlayBounds(Rect newBounds) {
586 mAppsView.setSearchBarBounds(newBounds);
587 mWidgetsView.setSearchBarBounds(newBounds);
588 }
589
Adam Cohen9211d422014-10-07 18:14:53 -0700590 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700591 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700592 if (mLauncherCallbacks != null) {
593 return mLauncherCallbacks.hasCustomContentToLeft();
594 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700595 return false;
596 }
597
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500599 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600600 * {@link #addToCustomContentPage}. This will only be invoked if
601 * {@link #hasCustomContentToLeft()} is {@code true}.
602 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500603 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700604 if (mLauncherCallbacks != null) {
605 mLauncherCallbacks.populateCustomContentContainer();
606 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600607 }
608
609 /**
610 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
611 * ensure the custom content page is added or removed if necessary.
612 */
613 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600614 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600615 // Not bound yet, wait for bindScreens to be called.
616 return;
617 }
618
619 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
620 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500621 mWorkspace.createCustomContentContainer();
622 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600623 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
624 mWorkspace.removeCustomContentPage();
625 }
626 }
627
Anton Hanssona2f665f2013-09-26 12:18:32 +0100628 public Stats getStats() {
629 return mStats;
630 }
631
Hyunyoung Song3f471442015-04-08 19:01:34 -0700632 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700633 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
634 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700635 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700636 }
637
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800638 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000639 public static int generateViewId() {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700640 if (Utilities.ATLEAST_JB_MR1) {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100641 return View.generateViewId();
642 } else {
643 // View.generateViewId() is not available. The following fallback logic is a copy
644 // of its implementation.
645 for (;;) {
646 final int result = sNextGeneratedId.get();
647 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
648 int newValue = result + 1;
649 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
650 if (sNextGeneratedId.compareAndSet(result, newValue)) {
651 return result;
652 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000653 }
654 }
655 }
656
657 public int getViewIdForItem(ItemInfo info) {
658 // This cast is safe given the > 2B range for int.
Sunny Goyalebfae6e2015-08-27 15:30:25 -0700659 int itemId = (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000660 if (mItemIdToViewId.containsKey(itemId)) {
661 return mItemIdToViewId.get(itemId);
662 }
663 int viewId = generateViewId();
664 mItemIdToViewId.put(itemId, viewId);
665 return viewId;
666 }
667
668 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
670 * a configuration step, this allows the proper animations to run after other transitions.
671 */
Adam Cohendb364c32014-05-20 14:23:40 -0700672 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700673 long screenId = args.screenId;
674 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
675 // When the screen id represents an actual screen (as opposed to a rank) we make sure
676 // that the drop page actually exists.
677 screenId = ensurePendingDropLayoutExists(args.screenId);
678 }
Adam Cohendb364c32014-05-20 14:23:40 -0700679
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700682 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700684 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700686 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700687 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700688 case REQUEST_RECONFIGURE_APPWIDGET:
689 completeRestoreAppWidget(args.appWidgetId);
690 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800691 }
Michael Jurka27614d22012-04-02 06:40:22 -0700692 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
693 // if you turned the screen off and then back while in All Apps, Launcher would not
694 // return to the workspace. Clearing mAddInfo.container here fixes this issue
695 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700696 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
698
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800699 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700700 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700701 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700702 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700703 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800704 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700705
Adam Cohenad4e15c2013-10-17 16:21:35 -0700706 Runnable exitSpringLoaded = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
710 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
711 }
712 };
713
Michael Jurka8b805b12012-04-18 14:23:14 -0700714 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700715 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700716 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
718 if (resultCode == RESULT_CANCELED) {
719 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700720 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800723 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700724 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700725
726 // When the user has granted permission to bind widgets, we should check to see if
727 // we can inflate the default search bar widget.
728 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700729 }
730 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200731 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
732 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700733 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200734 }
735 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700736 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200737
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700739 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700740
Adam Cohendb364c32014-05-20 14:23:40 -0700741 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 // We have special handling for widgets
743 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800744 final int appWidgetId;
745 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
746 : -1;
747 if (widgetId < 0) {
748 appWidgetId = pendingAddWidgetId;
749 } else {
750 appWidgetId = widgetId;
751 }
752
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800754 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700755 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
756 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 result = RESULT_CANCELED;
758 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700759 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed(false, 0, null);
763 }
764 };
Adam Cohendb364c32014-05-20 14:23:40 -0700765 if (workspaceLocked) {
766 // No need to remove the empty screen if we're mid-binding, as the
767 // the bind will not add the empty screen.
768 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
769 } else {
770 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
771 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
772 }
Winson Chung5aaab772012-04-27 15:24:02 -0700773 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700774 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700775 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
776 // When the screen id represents an actual screen (as opposed to a rank)
777 // we make sure that the drop page actually exists.
778 mPendingAddInfo.screenId =
779 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
780 }
Adam Cohendb364c32014-05-20 14:23:40 -0700781 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
782
783 dropLayout.setDropPending(true);
784 final Runnable onComplete = new Runnable() {
785 @Override
786 public void run() {
787 completeTwoStageWidgetDrop(resultCode, appWidgetId);
788 dropLayout.setDropPending(false);
789 }
790 };
791 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
793 } else {
794 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
795 mPendingAddInfo);
796 sPendingAddItem = args;
797 }
Winson Chung5aaab772012-04-27 15:24:02 -0700798 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 return;
800 }
801
Sunny Goyalff572272014-07-23 13:58:07 -0700802 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
803 if (resultCode == RESULT_OK) {
804 // Update the widget view.
805 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
806 pendingAddWidgetId, mPendingAddInfo);
807 if (workspaceLocked) {
808 sPendingAddItem = args;
809 } else {
810 completeAdd(args);
811 }
812 }
813 // Leave the widget in the pending state if the user canceled the configure.
814 return;
815 }
816
Sunny Goyalaad90582015-07-09 14:22:50 -0700817 if (requestCode == REQUEST_CREATE_SHORTCUT) {
818 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
819 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
820 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
821 mPendingAddInfo);
822 if (isWorkspaceLocked()) {
823 sPendingAddItem = args;
824 } else {
825 completeAdd(args);
826 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
828 }
829 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700830 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800835
836 }
837
838 @Override
839 protected void onActivityResult(
840 final int requestCode, final int resultCode, final Intent data) {
841 handleActivityResult(requestCode, resultCode, data);
842 if (mLauncherCallbacks != null) {
843 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 }
846
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600847 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700848 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600849 int[] grantResults) {
850 if (mLauncherCallbacks != null) {
851 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
852 grantResults);
853 }
854 }
855
Adam Cohendb364c32014-05-20 14:23:40 -0700856 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
857 appWidgetId, ItemInfo info) {
858 PendingAddArguments args = new PendingAddArguments();
859 args.requestCode = requestCode;
860 args.intent = data;
861 args.container = info.container;
862 args.screenId = info.screenId;
863 args.cellX = info.cellX;
864 args.cellY = info.cellY;
865 args.appWidgetId = appWidgetId;
866 return args;
867 }
868
869 /**
870 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
871 *
872 * @param screenId the screen id to check
873 * @return the new screen, or screenId if it exists
874 */
875 private long ensurePendingDropLayoutExists(long screenId) {
876 CellLayout dropLayout =
877 (CellLayout) mWorkspace.getScreenWithId(screenId);
878 if (dropLayout == null) {
879 // it's possible that the add screen was removed because it was
880 // empty and a re-bind occurred
881 mWorkspace.addExtraEmptyScreen();
882 return mWorkspace.commitExtraEmptyScreen();
883 } else {
884 return screenId;
885 }
886 }
887
Adam Cohen091440a2015-03-18 14:16:05 -0700888 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700889 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 Runnable onCompleteRunnable = null;
892 int animationType = 0;
893
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 if (resultCode == RESULT_OK) {
896 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
897 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 mPendingAddWidgetInfo);
899 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 onCompleteRunnable = new Runnable() {
901 @Override
902 public void run() {
903 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700905 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
906 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 }
908 };
909 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800910 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 if (mDragLayer.getAnimatedView() != null) {
914 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
915 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
916 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700917 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700918 // The animated view may be null in the case of a rotation during widget configuration
919 onCompleteRunnable.run();
920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
922
923 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700924 protected void onStop() {
925 super.onStop();
926 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700927
928 if (mLauncherCallbacks != null) {
929 mLauncherCallbacks.onStop();
930 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700931 }
932
933 @Override
934 protected void onStart() {
935 super.onStart();
936 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700937
938 if (mLauncherCallbacks != null) {
939 mLauncherCallbacks.onStart();
940 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700941 }
942
943 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200945 long startTime = 0;
946 if (DEBUG_RESUME_TIME) {
947 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400948 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200949 }
Adam Cohen9211d422014-10-07 18:14:53 -0700950
951 if (mLauncherCallbacks != null) {
952 mLauncherCallbacks.preOnResume();
953 }
954
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700956
Winson Chung4a2afa32012-07-19 14:53:05 -0700957 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700958 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800960 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700961 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700962 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700963 // view after launching an app, as they may be depending on the UI to be static to
964 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700965 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700966 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800967 } else if (mOnResumeState == State.WIDGETS) {
968 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700969 }
970 mOnResumeState = State.NONE;
971
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700972 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700973 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
974 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700975
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800976 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700977 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700978 setWorkspaceLoading(true);
Sunny Goyal22aa3812015-08-19 16:24:27 -0700979
980 // If we're starting binding all over again, clear any bind calls we'd postponed in
981 // the past (see waitUntilResume) -- we don't need them since we're starting binding
982 // from scratch again
983 mBindOnResumeCallbacks.clear();
984
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700985 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700987 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 // We might have postponed some bind calls until onResume (see waitUntilResume) --
991 // execute them here
992 long startTimeCallbacks = 0;
993 if (DEBUG_RESUME_TIME) {
994 startTimeCallbacks = System.currentTimeMillis();
995 }
996
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
998 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001000 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001001 if (DEBUG_RESUME_TIME) {
1002 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1003 (System.currentTimeMillis() - startTimeCallbacks));
1004 }
Michael Jurka447bf842013-05-15 14:52:15 +02001005 }
Michael Jurka54554252013-08-01 12:52:23 +02001006 if (mOnResumeCallbacks.size() > 0) {
1007 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1008 mOnResumeCallbacks.get(i).run();
1009 }
1010 mOnResumeCallbacks.clear();
1011 }
Winson Chunge4e50662012-01-23 14:45:13 -08001012
1013 // Reset the pressed state of icons that were locked in the press state while activities
1014 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001015 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001016 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001017 mWaitingForResume.setStayPressed(false);
1018 }
Winson Chung82963d52013-10-09 11:20:57 -07001019
Adam Cohen06dff352012-06-01 17:17:08 -07001020 // It is possible that widgets can receive updates while launcher is not in the foreground.
1021 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1022 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1023 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001024 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001025 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001026
Michael Jurka447bf842013-05-15 14:52:15 +02001027 if (DEBUG_RESUME_TIME) {
1028 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1029 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001030
1031 if (mWorkspace.getCustomContentCallbacks() != null) {
1032 // If we are resuming and the custom content is the current page, we call onShow().
1033 // It is also poassible that onShow will instead be called slightly after first layout
1034 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1035 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001036 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001037 }
1038 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001039 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001040 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001041
Sunny Goyal756adbc2015-04-16 15:20:51 -07001042 if (!isWorkspaceLoading()) {
1043 // Process any items that were added while Launcher was away.
1044 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1045 }
Adam Cohen9211d422014-10-07 18:14:53 -07001046
1047 if (mLauncherCallbacks != null) {
1048 mLauncherCallbacks.onResume();
1049 }
Adam Cohen06dff352012-06-01 17:17:08 -07001050 }
1051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001054 // Ensure that items added to Launcher are queued until Launcher returns
1055 InstallShortcutReceiver.enableInstallQueue();
1056
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001058 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001059 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001060 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001061
1062 // We call onHide() aggressively. The custom content callbacks should be able to
1063 // debounce excess onHide calls.
1064 if (mWorkspace.getCustomContentCallbacks() != null) {
1065 mWorkspace.getCustomContentCallbacks().onHide();
1066 }
Romain Guycbb89e42009-06-08 15:52:54 -07001067
Adam Cohen9211d422014-10-07 18:14:53 -07001068 if (mLauncherCallbacks != null) {
1069 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 }
1072
1073 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001074 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1075 // by a onResume or by scrolling otherwise.
1076 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001077
1078 // Custom content is completely hidden
1079 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001080
1081 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1082 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001083
1084 // Indicates whether the user is allowed to scroll away from the custom content.
1085 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
1087
Adam Cohenc2d6e892014-10-16 09:49:24 -07001088 public interface LauncherOverlay {
1089
1090 /**
1091 * Touch interaction leading to overscroll has begun
1092 */
1093 public void onScrollInteractionBegin();
1094
1095 /**
1096 * Touch interaction related to overscroll has ended
1097 */
1098 public void onScrollInteractionEnd();
1099
1100 /**
1101 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1102 * screen (or in the case of RTL, the rightmost screen).
1103 */
1104 public void onScrollChange(int progress, boolean rtl);
1105
1106 /**
1107 * Screen has stopped scrolling
1108 */
1109 public void onScrollSettled();
1110
1111 /**
1112 * This method can be called by the Launcher in order to force the LauncherOverlay
1113 * to exit fully immersive mode.
1114 */
1115 public void forceExitFullImmersion();
1116 }
1117
Jun Mukai8af0cd82015-05-12 12:32:12 -07001118 public interface LauncherSearchCallbacks {
1119 /**
1120 * Called when the search overlay is shown.
1121 */
1122 public void onSearchOverlayOpened();
1123
1124 /**
1125 * Called when the search overlay is dismissed.
1126 */
1127 public void onSearchOverlayClosed();
1128 }
1129
Adam Cohenc2d6e892014-10-16 09:49:24 -07001130 public interface LauncherOverlayCallbacks {
1131 /**
1132 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1133 * however it doesn't modify any state within the launcher.
1134 */
1135 public boolean canEnterFullImmersion();
1136
1137 /**
1138 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1139 * eg. by occupying the full screen and handling all touch events.
1140 *
1141 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1142 * case, Launcher will modify any necessary state and assumes the overlay is
1143 * handling all interaction. If false, the LauncherOverlay should cancel any
1144 *
1145 */
1146 public boolean enterFullImmersion();
1147
1148 /**
1149 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1150 * full control over UI and state.
1151 */
1152 public void exitFullImmersion();
1153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
1157 @Override
1158 public boolean canEnterFullImmersion() {
1159 return mState == State.WORKSPACE;
1160 }
1161
1162 @Override
1163 public boolean enterFullImmersion() {
1164 if (mState == State.WORKSPACE) {
1165 // When fully immersed, disregard any touches which fall through.
1166 mDragLayer.setBlockTouch(true);
1167 return true;
1168 }
1169 return false;
1170 }
1171
1172 @Override
1173 public void exitFullImmersion() {
1174 mDragLayer.setBlockTouch(false);
1175 }
1176 }
1177
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001179 if (mLauncherCallbacks != null) {
1180 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001181 } else {
1182 // On devices with a locked orientation, we will at least have the allow rotation
1183 // setting.
1184 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001185 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001186 }
1187
Adam Cohen9211d422014-10-07 18:14:53 -07001188 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001189 CustomContentCallbacks callbacks, String description) {
1190 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001191 }
1192
Adam Cohenedb40762013-07-18 16:45:45 -07001193 // The custom content needs to offset its content to account for the QSB
1194 public int getTopOffsetForCustomContent() {
1195 return mWorkspace.getPaddingTop();
1196 }
1197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001198 @Override
1199 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001201 if (mModel.isCurrentCallbacks(this)) {
1202 mModel.stopLoader();
1203 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001204 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1205
Romain Guy13c2e7b2010-03-10 19:45:00 -08001206 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001207 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 @Override
1211 public void onWindowFocusChanged(boolean hasFocus) {
1212 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001213 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001214
1215 if (mLauncherCallbacks != null) {
1216 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1217 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 private boolean acceptFilter() {
1221 final InputMethodManager inputManager = (InputMethodManager)
1222 getSystemService(Context.INPUT_METHOD_SERVICE);
1223 return !inputManager.isFullscreenMode();
1224 }
1225
1226 @Override
1227 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001228 final int uniChar = event.getUnicodeChar();
1229 final boolean handled = super.onKeyDown(keyCode, event);
1230 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1231 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1233 keyCode, event);
1234 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001236 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // showSearchDialog()
1238 // If there are multiple keystrokes before the search dialog takes focus,
1239 // onSearchRequested() will be called for every keystroke,
1240 // but it is idempotent, so it's fine.
1241 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
1244
Joe Onorato8a9625e2010-01-28 15:55:35 -08001245 // Eat the long press event so the keyboard doesn't come up.
1246 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1247 return true;
1248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 return handled;
1251 }
1252
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 private String getTypedText() {
1254 return mDefaultKeySsb.toString();
1255 }
1256
1257 private void clearTypedText() {
1258 mDefaultKeySsb.clear();
1259 mDefaultKeySsb.clearSpans();
1260 Selection.setSelection(mDefaultKeySsb, 0);
1261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001264 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1265 * State
1266 */
1267 private static State intToState(int stateOrdinal) {
1268 State state = State.WORKSPACE;
1269 final State[] stateValues = State.values();
1270 for (int i = 0; i < stateValues.length; i++) {
1271 if (stateValues[i].ordinal() == stateOrdinal) {
1272 state = stateValues[i];
1273 break;
1274 }
1275 }
1276 return state;
1277 }
1278
1279 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 * Restores the previous state, if it exists.
1281 *
1282 * @param savedState The previous state.
1283 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001284 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 private void restoreState(Bundle savedState) {
1286 if (savedState == null) {
1287 return;
1288 }
1289
Michael Jurka883f55b2010-10-21 15:47:14 -07001290 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001291 if (state == State.APPS || state == State.WIDGETS) {
1292 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001293 }
1294
Adam Cohen21cd0022013-10-09 18:57:02 -07001295 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1296 PagedView.INVALID_RESTORE_PAGE);
1297 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001298 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
Sunny Goyal756cd262015-08-20 12:33:21 -07001301 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1302 if (itemValues != null) {
1303 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001304 AppWidgetProviderInfo info = savedState.getParcelable(
1305 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001306 mPendingAddWidgetInfo = info == null ?
1307 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1308
Adam Cohen4637b5a2013-11-04 18:21:24 -08001309 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001310 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 mRestoring = true;
1312 }
1313
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001314 mItemIdToViewId = (HashMap<Integer, Integer>)
1315 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
1318 /**
1319 * Finds all the views we need and configure them properly.
1320 */
1321 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001322 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001323
Craig Mautner360310b2012-10-26 15:13:08 -07001324 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001325 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001326 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1327 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001328 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001329 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001330
John Spurlock77e1f472013-09-11 10:09:51 -04001331 mLauncherView.setSystemUiVisibility(
1332 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001333 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
Winson Chung4c98d922011-05-31 16:50:48 -07001335 // Setup the drag layer
1336 mDragLayer.setup(this, dragController);
1337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 // Setup the hotseat
1339 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1340 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001341 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 }
1343
Jorim Jaggid017f882014-01-14 17:08:48 -08001344 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001345 mWidgetsButton = findViewById(R.id.widget_button);
1346 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001347 @Override
1348 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001349 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001350 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001351 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001352 }
1353 });
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001354 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001355
1356 View wallpaperButton = findViewById(R.id.wallpaper_button);
1357 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001358 @Override
1359 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001360 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001361 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001362 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001363 }
1364 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001365 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1366
1367 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001368 if (hasSettings()) {
1369 settingsButton.setOnClickListener(new OnClickListener() {
1370 @Override
1371 public void onClick(View arg0) {
1372 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001373 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001374 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001375 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001376 });
1377 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1378 } else {
1379 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001380 }
1381
Adam Cohendbdff6b2013-09-18 19:09:15 -07001382 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001383
Winson Chung4c98d922011-05-31 16:50:48 -07001384 // Setup the workspace
1385 mWorkspace.setHapticFeedbackEnabled(false);
1386 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001387 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001388 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001389
Winson Chungf0ea4d32011-06-06 14:27:16 -07001390 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001391 mSearchDropTargetBar = (SearchDropTargetBar)
1392 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001393
Winson Chungef7f8742015-06-04 17:18:17 -07001394 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001395 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001396 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001397 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1398 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1399 } else {
1400 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1401 }
Craig Mautner360310b2012-10-26 15:13:08 -07001402
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001404 dragController.setDragScoller(mWorkspace);
1405 dragController.setScrollView(mDragLayer);
1406 dragController.setMoveTarget(mWorkspace);
1407 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001408 if (mSearchDropTargetBar != null) {
1409 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001410 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001411 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001412
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001413 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001414 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001415 mWeightWatcher = new WeightWatcher(this);
1416 mWeightWatcher.setAlpha(0.5f);
1417 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001418 new FrameLayout.LayoutParams(
1419 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001420 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001421 Gravity.BOTTOM)
1422 );
Adam Cohen39a06042013-07-19 14:30:12 -07001423
1424 boolean show = shouldShowWeightWatcher();
1425 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428
1429 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001430 * Sets the all apps button. This method is called from {@link Hotseat}.
1431 */
1432 public void setAllAppsButton(View allAppsButton) {
1433 mAllAppsButton = allAppsButton;
1434 }
1435
1436 public View getAllAppsButton() {
1437 return mAllAppsButton;
1438 }
1439
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001440 public View getWidgetsButton() {
1441 return mWidgetsButton;
1442 }
1443
Anjali Koppal5ad44842014-03-10 20:34:39 -07001444 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 * Creates a view representing a shortcut.
1446 *
1447 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001449 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001450 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
1453 /**
1454 * Creates a view representing a shortcut inflated from the specified resource.
1455 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 * @param parent The group the shortcut belongs to.
1457 * @param info The data structure describing the shortcut.
1458 *
1459 * @return A View inflated from layoutResId.
1460 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001461 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001462 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001463 parent, false);
1464 favorite.applyFromShortcutInfo(info, mIconCache);
1465 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001467 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 return favorite;
1469 }
1470
1471 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 * Add a shortcut to the workspace.
1473 *
1474 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001477 int cellY) {
1478 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001479 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Sunny Goyal5c97f512015-05-19 16:03:28 -07001481 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001482 if (info == null) {
1483 return;
1484 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001485 final View view = createShortcut(info);
1486
Sunny Goyal5c97f512015-05-19 16:03:28 -07001487 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 // First we check if we already know the exact location where we want to add this item.
1489 if (cellX >= 0 && cellY >= 0) {
1490 cellXY[0] = cellX;
1491 cellXY[1] = cellY;
1492 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001493
1494 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001495 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001496 true, null,null)) {
1497 return;
1498 }
1499 DragObject dragObject = new DragObject();
1500 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001501 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1502 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001503 return;
1504 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001505 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001507 }
1508
1509 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001510 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 return;
1512 }
1513
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001514 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
1516 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001518 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520 }
1521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001525 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 */
Adam Cohen091440a2015-03-18 14:16:05 -07001527 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001528 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1529
1530 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001531 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001532 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1533 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Adam Cohen59400422014-03-05 18:07:04 -08001536 if (appWidgetInfo.isCustomWidget) {
1537 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001538 }
1539
Adam Cohen59400422014-03-05 18:07:04 -08001540 LauncherAppWidgetInfo launcherInfo;
1541 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1542 launcherInfo.spanX = info.spanX;
1543 launcherInfo.spanY = info.spanY;
1544 launcherInfo.minSpanX = info.minSpanX;
1545 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001546 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001549 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550
1551 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 if (hostView == null) {
1553 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001554 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1555 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001556 } else {
1557 // The AppWidgetHostView has already been inflated and instantiated
1558 launcherInfo.hostView = hostView;
1559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001562 launcherInfo.notifyWidgetSizeChanged(this);
1563
Adam Cohen59400422014-03-05 18:07:04 -08001564 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1565 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001566
1567 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001569 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1573 @Override
1574 public void onReceive(Context context, Intent intent) {
1575 final String action = intent.getAction();
1576 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1577 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001578 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001579 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001580
Winson Chungc100e8e2011-08-09 16:02:43 -07001581 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001582 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001583 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001584 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001585 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001586 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1588 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001589 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001590 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1591 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001592 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001593 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1594 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1595 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001596 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001597 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1598 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 }
1600 }
1601 };
1602
1603 @Override
1604 public void onAttachedToWindow() {
1605 super.onAttachedToWindow();
1606
1607 // Listen for broadcasts related to user-presence
1608 final IntentFilter filter = new IntentFilter();
1609 filter.addAction(Intent.ACTION_SCREEN_OFF);
1610 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001611 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001612 if (ENABLE_DEBUG_INTENTS) {
1613 filter.addAction(DebugIntents.DELETE_DATABASE);
1614 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1615 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001617 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001618 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001619 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001620 mVisible = true;
1621 }
1622
Adam Cohen0b395352014-06-09 22:54:36 +00001623 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001624 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001625 * This method is a no-op for other platform versions.
1626 */
Sunny Goyald0091012015-01-20 15:55:34 -08001627 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001628 private void setupTransparentSystemBarsForLollipop() {
1629 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001630 Window window = getWindow();
1631 window.getAttributes().systemUiVisibility |=
1632 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1633 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1634 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1635 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1636 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1637 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1638 window.setStatusBarColor(Color.TRANSPARENT);
1639 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001640 }
1641 }
1642
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 @Override
1644 public void onDetachedFromWindow() {
1645 super.onDetachedFromWindow();
1646 mVisible = false;
1647
Adam Cohend113e0c2010-11-11 10:48:05 -08001648 if (mAttached) {
1649 unregisterReceiver(mReceiver);
1650 mAttached = false;
1651 }
Winson Chungb745afb2015-03-02 11:51:23 -08001652 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 }
1654
1655 public void onWindowVisibilityChanged(int visibility) {
1656 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001657 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001658 // The following code used to be in onResume, but it turns out onResume is called when
1659 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1660 // is a more appropriate event to handle
1661 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001662 if (!mWorkspaceLoading) {
1663 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001664 // We want to let Launcher draw itself at least once before we force it to build
1665 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001666 // apps is nice and speedy.
1667 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001668 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001669 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 if (mStarted) return;
1671 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001672 // We delay the layer building a bit in order to give
1673 // other message processing a time to run. In particular
1674 // this avoids a delay in hiding the IME if it was
1675 // currently shown, because doing that may involve
1676 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001677 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 final ViewTreeObserver.OnDrawListener listener = this;
1679 mWorkspace.post(new Runnable() {
1680 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001681 if (mWorkspace != null &&
1682 mWorkspace.getViewTreeObserver() != null) {
1683 mWorkspace.getViewTreeObserver().
1684 removeOnDrawListener(listener);
1685 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001686 }
1687 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001689 }
1690 });
1691 }
1692 clearTypedText();
1693 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 }
1695
Adam Cohen091440a2015-03-18 14:16:05 -07001696 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 mHandler.removeMessages(ADVANCE_MSG);
1698 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1699 mHandler.sendMessageDelayed(msg, delay);
1700 mAutoAdvanceSentTime = System.currentTimeMillis();
1701 }
1702
Adam Cohen091440a2015-03-18 14:16:05 -07001703 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1705 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1706 mAutoAdvanceRunning = autoAdvanceRunning;
1707 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001708 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 sendAdvanceMessage(delay);
1710 } else {
1711 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001712 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1714 }
1715 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001716 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 }
1718 }
1719 }
1720
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001721 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1722
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001724 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 if (msg.what == ADVANCE_MSG) {
1726 int i = 0;
1727 for (View key: mWidgetsToAdvance.keySet()) {
1728 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001729 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001731 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 public void run() {
1733 ((Advanceable) v).advance();
1734 }
1735 }, delay);
1736 }
1737 i++;
1738 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001739 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001741 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001743 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001744
1745 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001746 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1748 if (v instanceof Advanceable) {
1749 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001750 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001751 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
1753 }
1754
1755 void removeWidgetToAutoAdvance(View hostView) {
1756 if (mWidgetsToAdvance.containsKey(hostView)) {
1757 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001758 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001760 }
1761
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001764 launcherInfo.hostView = null;
1765 }
1766
Hyunyoung Song3f471442015-04-08 19:01:34 -07001767 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001768 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1769 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
1771
Winson Chunga6945242014-01-08 14:04:34 -08001772 public DragLayer getDragLayer() {
1773 return mDragLayer;
1774 }
1775
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001776 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001777 return mAppsView;
1778 }
1779
Hyunyoung Song3f471442015-04-08 19:01:34 -07001780 public WidgetsContainerView getWidgetsView() {
1781 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001782 }
1783
Winson Chunga6945242014-01-08 14:04:34 -08001784 public Workspace getWorkspace() {
1785 return mWorkspace;
1786 }
1787
1788 public Hotseat getHotseat() {
1789 return mHotseat;
1790 }
1791
Jorim Jaggid017f882014-01-14 17:08:48 -08001792 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001793 return mOverviewPanel;
1794 }
1795
Winson Chung006ee262015-08-03 14:40:11 -07001796 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001797 return mSearchDropTargetBar;
1798 }
1799
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001800 public LauncherAppWidgetHost getAppWidgetHost() {
1801 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Winson Chunga9abd0e2010-10-27 17:18:37 -07001804 public LauncherModel getModel() {
1805 return mModel;
1806 }
1807
Winson Chunga6945242014-01-08 14:04:34 -08001808 protected SharedPreferences getSharedPrefs() {
1809 return mSharedPrefs;
1810 }
1811
Adam Cohen2e6da152015-05-06 11:42:25 -07001812 public DeviceProfile getDeviceProfile() {
1813 return mDeviceProfile;
1814 }
1815
Bjorn Bringertc459e522013-06-07 19:36:01 +01001816 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001817 getWindow().closeAllPanels();
1818
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001819 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001820 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001821 }
1822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 @Override
1824 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001825 long startTime = 0;
1826 if (DEBUG_RESUME_TIME) {
1827 startTime = System.currentTimeMillis();
1828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 super.onNewIntent(intent);
1830
1831 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001832 Folder openFolder = mWorkspace.getOpenFolder();
1833 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1834 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1835 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1836 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1837 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001838 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001840
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 if (mWorkspace == null) {
1842 // Can be cases where mWorkspace is null, this prevents a NPE
1843 return;
1844 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001845 // In all these cases, only animate if we're already on home
1846 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001847
Adam Cohen6fecd412013-10-02 17:41:50 -07001848 closeFolder();
1849 exitSpringLoadedDragMode();
1850
1851 // If we are already on home, then just animate back to the workspace,
1852 // otherwise, just wait until onResume to set the state back to Workspace
1853 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001854 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001855 } else {
1856 mOnResumeState = State.WORKSPACE;
1857 }
1858
1859 final View v = getWindow().peekDecorView();
1860 if (v != null && v.getWindowToken() != null) {
1861 InputMethodManager imm = (InputMethodManager)getSystemService(
1862 INPUT_METHOD_SERVICE);
1863 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1864 }
1865
Winson Chungb745afb2015-03-02 11:51:23 -08001866 // Reset the apps view
1867 if (!alreadyOnHome && mAppsView != null) {
1868 mAppsView.scrollToTop();
1869 }
1870
Hyunyoung Song3f471442015-04-08 19:01:34 -07001871 // Reset the widgets view
1872 if (!alreadyOnHome && mWidgetsView != null) {
1873 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001875
Adam Cohen9211d422014-10-07 18:14:53 -07001876 if (mLauncherCallbacks != null) {
1877 mLauncherCallbacks.onHomeIntent();
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
Adam Cohened307df2013-10-02 09:37:31 -07001880
Adam Cohen9211d422014-10-07 18:14:53 -07001881 if (mLauncherCallbacks != null) {
1882 mLauncherCallbacks.onNewIntent(intent);
1883 }
Winson15f8b172015-08-19 11:02:39 -07001884
1885 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1886 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1887 // animation.
1888 if (isActionMain) {
1889 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1890 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
1891 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1892 openFolder == null && moveToDefaultScreen) {
1893 mWorkspace.post(new Runnable() {
1894 @Override
1895 public void run() {
1896 mWorkspace.moveToDefaultScreen(true);
1897 }
1898 });
1899 }
1900 }
1901
1902 if (DEBUG_RESUME_TIME) {
1903 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1904 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001908 public void onRestoreInstanceState(Bundle state) {
1909 super.onRestoreInstanceState(state);
1910 for (int page: mSynchronouslyBoundPages) {
1911 mWorkspace.restoreInstanceStateForChild(page);
1912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
1915 @Override
1916 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001917 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001918 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1919 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001920 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001921 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922
Michael Jurka883f55b2010-10-21 15:47:14 -07001923 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001924 // We close any open folder since it will not be re-opened, and we need to make sure
1925 // this state is reflected.
1926 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001930 ContentValues itemValues = new ContentValues();
1931 mPendingAddInfo.writeToValues(itemValues);
1932 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001933 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001934 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
Hyunyoung Song3f471442015-04-08 19:01:34 -07001937 // Save the current widgets tray?
1938 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001939 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001940
1941 if (mLauncherCallbacks != null) {
1942 mLauncherCallbacks.onSaveInstanceState(outState);
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
1946 @Override
1947 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Winson Chunge7a03942011-08-05 15:05:12 -07001950 // Remove all pending runnables
1951 mHandler.removeMessages(ADVANCE_MSG);
1952 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001953 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001954
Winson Chungcd2b0142011-06-08 16:02:26 -07001955 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001956 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001957
1958 // It's possible to receive onDestroy after a new Launcher activity has
1959 // been created. In this case, don't interfere with the new Launcher.
1960 if (mModel.isCurrentCallbacks(this)) {
1961 mModel.stopLoader();
1962 app.setLauncher(null);
1963 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001966 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001968 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001970 mAppWidgetHost = null;
1971
1972 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973
1974 TextKeyListener.getInstance().release();
1975
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001976 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001977
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001978 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001979 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001980 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001981 mWorkspace = null;
1982 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001983
Michael Jurka2ecf9952012-06-18 12:52:28 -07001984 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001985
1986 if (mLauncherCallbacks != null) {
1987 mLauncherCallbacks.onDestroy();
1988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 public DragController getDragController() {
1992 return mDragController;
1993 }
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 @Override
1996 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001997 onStartForResult(requestCode);
1998 super.startActivityForResult(intent, requestCode);
1999 }
2000
2001 @Override
2002 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2003 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2004 onStartForResult(requestCode);
2005 try {
2006 super.startIntentSenderForResult(intent, requestCode,
2007 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2008 } catch (IntentSender.SendIntentException e) {
2009 throw new ActivityNotFoundException();
2010 }
2011 }
2012
2013 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002014 if (requestCode >= 0) {
2015 setWaitingForResult(true);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
Winson Chung70d72102011-08-12 11:24:35 -07002019 /**
2020 * Indicates that we want global search for this activity by setting the globalSearch
2021 * argument for {@link #startSearch} to true.
2022 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002024 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002026
Karl Rosaen138a0412009-04-23 19:00:21 -07002027 if (initialQuery == null) {
2028 // Use any text typed in the launcher as the initial query
2029 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 if (appSearchData == null) {
2032 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002033 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Winson Chungadf0c182012-08-23 14:59:07 -07002035 Rect sourceBounds = new Rect();
2036 if (mSearchDropTargetBar != null) {
2037 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2038 }
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Ian Parkinson047036e2014-09-01 15:40:53 +01002040 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002041 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002042 if (clearTextImmediately) {
2043 clearTypedText();
2044 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002045
2046 // We need to show the workspace after starting the search
2047 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002048 }
2049
Ian Parkinson047036e2014-09-01 15:40:53 +01002050 /**
2051 * Start a text search.
2052 *
2053 * @return {@code true} if the search will start immediately, so any further keypresses
2054 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2055 * to buffer keypresses.
2056 */
2057 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002059 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2060 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2061 sourceBounds);
2062 }
2063
Michael Jurkaa33411c2012-06-14 16:18:21 -07002064 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002065 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002066 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002067 }
2068
2069 /**
2070 * Starts the global search activity. This code is a copied from SearchManager
2071 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002072 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002075 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2076 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2077 if (globalSearchActivity == null) {
2078 Log.w(TAG, "No global search activity found.");
2079 return;
2080 }
2081 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2082 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2083 intent.setComponent(globalSearchActivity);
2084 // Make sure that we have a Bundle to put source in
2085 if (appSearchData == null) {
2086 appSearchData = new Bundle();
2087 } else {
2088 appSearchData = new Bundle(appSearchData);
2089 }
Adam Cohen9211d422014-10-07 18:14:53 -07002090 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 if (!appSearchData.containsKey("source")) {
2092 appSearchData.putString("source", getPackageName());
2093 }
2094 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2095 if (!TextUtils.isEmpty(initialQuery)) {
2096 intent.putExtra(SearchManager.QUERY, initialQuery);
2097 }
2098 if (selectInitialQuery) {
2099 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2100 }
2101 intent.setSourceBounds(sourceBounds);
2102 try {
2103 startActivity(intent);
2104 } catch (ActivityNotFoundException ex) {
2105 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108
Winson Chungbedf9232015-07-10 12:38:30 -07002109 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2110 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2111 return;
2112 }
2113
2114 // If not handled, then just start the provided search intent
2115 startActivitySafely(v, searchIntent, null);
2116 }
2117
Yura4f93ec62014-02-04 14:15:21 +00002118 public boolean isOnCustomContent() {
2119 return mWorkspace.isOnOrMovingToCustomContent();
2120 }
2121
Winson Chung70d72102011-08-12 11:24:35 -07002122 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002123 public boolean onPrepareOptionsMenu(Menu menu) {
2124 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002125 if (!isOnCustomContent()) {
2126 // Close any open folders
2127 closeFolder();
2128 // Stop resizing any widgets
2129 mWorkspace.exitWidgetResizeMode();
2130 if (!mWorkspace.isInOverviewMode()) {
2131 // Show the overview mode
2132 showOverviewMode(true);
2133 } else {
2134 showWorkspace(true);
2135 }
Winson Chunge0298742014-01-17 12:03:00 -08002136 }
Adam Cohen9211d422014-10-07 18:14:53 -07002137 if (mLauncherCallbacks != null) {
2138 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2139 }
2140
Michael Jurkab94f3f82013-09-11 18:08:54 +02002141 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002144 @Override
2145 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002146 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002147 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002149 }
2150
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 public boolean isWorkspaceLocked() {
2152 return mWorkspaceLoading || mWaitingForResult;
2153 }
2154
Adam Cohen517a7f52014-03-01 12:12:59 -08002155 public boolean isWorkspaceLoading() {
2156 return mWorkspaceLoading;
2157 }
2158
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002159 private void setWorkspaceLoading(boolean value) {
2160 boolean isLocked = isWorkspaceLocked();
2161 mWorkspaceLoading = value;
2162 if (isLocked != isWorkspaceLocked()) {
2163 onWorkspaceLockedChanged();
2164 }
2165 }
2166
2167 private void setWaitingForResult(boolean value) {
2168 boolean isLocked = isWorkspaceLocked();
2169 mWaitingForResult = value;
2170 if (isLocked != isWorkspaceLocked()) {
2171 onWorkspaceLockedChanged();
2172 }
2173 }
2174
Adam Cohen9211d422014-10-07 18:14:53 -07002175 protected void onWorkspaceLockedChanged() {
2176 if (mLauncherCallbacks != null) {
2177 mLauncherCallbacks.onWorkspaceLockedChanged();
2178 }
2179 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002180
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002183 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002184 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2185 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002186 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002188
Sunny Goyale6b63a32015-01-16 16:14:29 -08002189 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002190 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002191 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2192 }
2193
Sunny Goyale6b63a32015-01-16 16:14:29 -08002194 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002195 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2196 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002197 if (appWidgetInfo.configure != null) {
2198 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002199 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002200
Bjorn Bringert7984c942009-12-09 15:38:25 +00002201 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002202 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2203 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002206 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002207 Runnable onComplete = new Runnable() {
2208 @Override
2209 public void run() {
2210 // Exit spring loaded mode if necessary after adding the widget
2211 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2212 null);
2213 }
2214 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002216 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002217 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219 }
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002221 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002222 // Close any folders that may be open.
2223 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002224 mWorkspace.moveToCustomContentScreen(animate);
2225 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002226
2227 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2228 int[] cell, int spanX, int spanY) {
2229 switch (info.itemType) {
2230 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2231 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2232 int span[] = new int[2];
2233 span[0] = spanX;
2234 span[1] = spanY;
2235 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2236 container, screenId, cell, span);
2237 break;
2238 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2239 processShortcutFromDrop(info.componentName, container, screenId, cell);
2240 break;
2241 default:
2242 throw new IllegalStateException("Unknown item type: " + info.itemType);
2243 }
2244 }
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 /**
2247 * Process a shortcut drop.
2248 *
2249 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002253 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2254 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002258
2259 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.cellX = cell[0];
2261 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263
2264 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2265 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002266 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 }
2268
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 /**
2270 * Process a widget drop.
2271 *
2272 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002276 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2277 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 mPendingAddInfo.minSpanX = info.minSpanX;
2282 mPendingAddInfo.minSpanY = info.minSpanY;
2283
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.cellX = cell[0];
2286 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002288 if (span != null) {
2289 mPendingAddInfo.spanX = span[0];
2290 mPendingAddInfo.spanY = span[1];
2291 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 AppWidgetHostView hostView = info.boundWidget;
2294 int appWidgetId;
2295 if (hostView != null) {
2296 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002298
2299 // Clear the boundWidget so that it doesn't get destroyed.
2300 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002301 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002302 // In this case, we either need to start an activity to get permission to bind
2303 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002304 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002305 Bundle options = info.bindOptions;
2306
Sunny Goyalffe83f12014-08-14 17:39:34 -07002307 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2308 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002309 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002310 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002311 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002312 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2314 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2315 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002316 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2317 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002318 // TODO: we need to make sure that this accounts for the options bundle.
2319 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002320 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2321 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002322 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 }
2324
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002327 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 folderInfo.title = getText(R.string.folder_name);
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002331 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2332 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002333 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334
2335 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 FolderIcon newFolder =
2337 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002340 // Force measure the new folder icon
2341 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2342 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002343 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 }
Romain Guycbb89e42009-06-08 15:52:54 -07002345
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002347 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002348 }
2349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 @Override
2351 public boolean dispatchKeyEvent(KeyEvent event) {
2352 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2353 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002355 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002356 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002357 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002358 dumpState();
2359 return true;
2360 }
2361 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002362 }
2363 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2364 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002365 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 return true;
2367 }
2368 }
2369
2370 return super.dispatchKeyEvent(event);
2371 }
2372
Joe Onorato88ec0992009-11-19 13:16:06 -08002373 @Override
2374 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002375 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2376 return;
2377 }
2378
Sunny Goyal45478022015-06-08 16:52:41 -07002379 if (mDragController.isDragging()) {
2380 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002381 return;
2382 }
2383
Winson Chungb745afb2015-03-02 11:51:23 -08002384 if (isAppsViewVisible()) {
2385 showWorkspace(true);
2386 } else if (isWidgetsViewVisible()) {
2387 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002388 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002389 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002390 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002391 Folder openFolder = mWorkspace.getOpenFolder();
2392 if (openFolder.isEditingName()) {
2393 openFolder.dismissEditingName();
2394 } else {
2395 closeFolder();
2396 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002397 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002398 mWorkspace.exitWidgetResizeMode();
2399
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002400 // Back button is a no-op here, but give at least some feedback for the button press
2401 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002402 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002403 }
2404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002406 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002407 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002408 @Override
2409 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002410 if (mAppWidgetHost != null) {
2411 mAppWidgetHost.startListening();
2412 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002413 }
2414
2415 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 * Launches the intent referred by the clicked shortcut.
2417 *
2418 * @param v The view representing the clicked shortcut.
2419 */
2420 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002421 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2422 // view has detached (it's possible for this to happen if the view is removed mid touch).
2423 if (v.getWindowToken() == null) {
2424 return;
2425 }
2426
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002427 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002428 return;
2429 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002430
Adam Cohen1697b792013-09-17 19:08:21 -07002431 if (v instanceof Workspace) {
2432 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002433 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 }
2435 return;
2436 }
2437
2438 if (v instanceof CellLayout) {
2439 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002440 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2441 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002442 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002443 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002444 }
2445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002447 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002448 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002450 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002451 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002452 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002453 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002455 } else if (tag instanceof AppInfo) {
2456 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002457 } else if (tag instanceof LauncherAppWidgetInfo) {
2458 if (v instanceof PendingAppWidgetHostView) {
2459 onClickPendingWidget((PendingAppWidgetHostView) v);
2460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 }
2462 }
2463
Sunny Goyal70660032015-05-14 00:07:08 -07002464 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002465 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002466 return false;
2467 }
2468
Michael Jurkaaf442092010-06-10 17:01:57 -07002469 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002470 * Event handler for the app widget view which has not fully restored.
2471 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002472 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002473 if (mIsSafeModeEnabled) {
2474 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2475 return;
2476 }
2477
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002478 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002479 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002480 int widgetId = info.appWidgetId;
2481 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2482 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002483 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2484 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002485 mPendingAddInfo.copyFrom(info);
2486 mPendingAddWidgetId = widgetId;
2487
Sunny Goyalffe83f12014-08-14 17:39:34 -07002488 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2489 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002490 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002491 } else if (info.installProgress < 0) {
2492 // The install has not been queued
2493 final String packageName = info.providerName.getPackageName();
2494 showBrokenAppInstallDialog(packageName,
2495 new DialogInterface.OnClickListener() {
2496 public void onClick(DialogInterface dialog, int id) {
2497 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2498 }
2499 });
2500 } else {
2501 // Download has started.
2502 final String packageName = info.providerName.getPackageName();
2503 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002504 }
2505 }
2506
2507 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002508 * Event handler for the "grid" button that appears on the home screen, which
2509 * enters all apps mode.
2510 *
2511 * @param v The view that was clicked.
2512 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 protected void onClickAllAppsButton(View v) {
2514 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002515 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002516 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002517 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002518
2519 if (mLauncherCallbacks != null) {
2520 mLauncherCallbacks.onClickAllAppsButton(v);
2521 }
Winson Chung76648c52015-07-10 14:33:23 -07002522 }
2523 }
2524
2525 protected void onLongClickAllAppsButton(View v) {
2526 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2527 if (!isAppsViewVisible()) {
2528 showAppsView(true /* animated */, false /* resetListToTop */,
2529 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 }
2531 }
2532
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002533 private void showBrokenAppInstallDialog(final String packageName,
2534 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002535 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002536 .setTitle(R.string.abandoned_promises_title)
2537 .setMessage(R.string.abandoned_promise_explanation)
2538 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2539 .setNeutralButton(R.string.abandoned_clean_this,
2540 new DialogInterface.OnClickListener() {
2541 public void onClick(DialogInterface dialog, int id) {
2542 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2543 mWorkspace.removeAbandonedPromise(packageName, user);
2544 }
2545 })
2546 .create().show();
2547 return;
2548 }
2549
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 /**
2551 * Event handler for an app shortcut click.
2552 *
2553 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2554 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002555 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2557 Object tag = v.getTag();
2558 if (!(tag instanceof ShortcutInfo)) {
2559 throw new IllegalArgumentException("Input must be a Shortcut");
2560 }
2561
2562 // Open shortcut
2563 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002564
2565 if (shortcut.isDisabled != 0) {
2566 int error = R.string.activity_not_available;
2567 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2568 error = R.string.safemode_shortcut_error;
2569 }
2570 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2571 return;
2572 }
2573
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002574 final Intent intent = shortcut.intent;
2575
2576 // Check for special shortcuts
2577 if (intent.getComponent() != null) {
2578 final String shortcutClass = intent.getComponent().getClassName();
2579
2580 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2581 MemoryDumpActivity.startDump(this);
2582 return;
2583 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2584 toggleShowWeightWatcher();
2585 return;
2586 }
2587 }
2588
Chris Wren40c5ed32014-06-24 18:24:23 -04002589 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002590 if ((v instanceof BubbleTextView)
2591 && shortcut.isPromise()
2592 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002593 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002594 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002595 new DialogInterface.OnClickListener() {
2596 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002597 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002598 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002599 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002600 return;
2601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002604 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002605
2606 if (mLauncherCallbacks != null) {
2607 mLauncherCallbacks.onClickAppShortcut(v);
2608 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002609 }
2610
Adam Cohen091440a2015-03-18 14:16:05 -07002611 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002612 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002613 final ShortcutInfo shortcut;
2614 final Intent intent;
2615 if (tag instanceof ShortcutInfo) {
2616 shortcut = (ShortcutInfo) tag;
2617 intent = shortcut.intent;
2618 int[] pos = new int[2];
2619 v.getLocationOnScreen(pos);
2620 intent.setSourceBounds(new Rect(pos[0], pos[1],
2621 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002622
Sunny Goyal508da152014-08-14 10:53:27 -07002623 } else if (tag instanceof AppInfo) {
2624 shortcut = null;
2625 intent = ((AppInfo) tag).intent;
2626 } else {
2627 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2628 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629
2630 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002631 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632
2633 if (success && v instanceof BubbleTextView) {
2634 mWaitingForResume = (BubbleTextView) v;
2635 mWaitingForResume.setStayPressed(true);
2636 }
2637 }
2638
2639 /**
2640 * Event handler for a folder icon click.
2641 *
2642 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2643 */
2644 protected void onClickFolderIcon(View v) {
2645 if (LOGD) Log.d(TAG, "onClickFolder");
2646 if (!(v instanceof FolderIcon)){
2647 throw new IllegalArgumentException("Input must be a FolderIcon");
2648 }
2649
Sunny Goyal317698b2015-07-29 11:45:41 -07002650 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 FolderIcon folderIcon = (FolderIcon) v;
2652 final FolderInfo info = folderIcon.getFolderInfo();
2653 Folder openFolder = mWorkspace.getFolderForTag(info);
2654
2655 // If the folder info reports that the associated folder is open, then verify that
2656 // it is actually opened. There have been a few instances where this gets out of sync.
2657 if (info.opened && openFolder == null) {
2658 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2659 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2660 info.opened = false;
2661 }
2662
2663 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2664 // Close any open folder
2665 closeFolder();
2666 // Open the requested folder
2667 openFolder(folderIcon);
2668 } else {
2669 // Find the open folder...
2670 int folderScreen;
2671 if (openFolder != null) {
2672 folderScreen = mWorkspace.getPageForView(openFolder);
2673 // .. and close it
2674 closeFolder(openFolder);
2675 if (folderScreen != mWorkspace.getCurrentPage()) {
2676 // Close any folder open on the current screen
2677 closeFolder();
2678 // Pull the folder onto this screen
2679 openFolder(folderIcon);
2680 }
2681 }
2682 }
Adam Cohen9211d422014-10-07 18:14:53 -07002683
2684 if (mLauncherCallbacks != null) {
2685 mLauncherCallbacks.onClickFolderIcon(v);
2686 }
Michael Jurka5130e402011-10-13 04:55:35 -07002687 }
2688
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002689 /**
2690 * Event handler for the (Add) Widgets button that appears after a long press
2691 * on the home screen.
2692 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002693 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002695 if (mIsSafeModeEnabled) {
2696 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2697 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002698 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002699 if (mLauncherCallbacks != null) {
2700 mLauncherCallbacks.onClickAddWidgetButton(view);
2701 }
Adam Cohen9211d422014-10-07 18:14:53 -07002702 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002703 }
2704
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 /**
2706 * Event handler for the wallpaper picker button that appears after a long press
2707 * on the home screen.
2708 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002709 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002710 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002711 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2712 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002713
2714 if (mLauncherCallbacks != null) {
2715 mLauncherCallbacks.onClickWallpaperPicker(v);
2716 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 }
2718
2719 /**
2720 * Event handler for a click on the settings button that appears after a long press
2721 * on the home screen.
2722 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002723 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002725 if (mLauncherCallbacks != null) {
2726 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002727 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002728 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002729 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 }
2731
Adam Cohen61f560d2013-09-30 15:58:20 -07002732 public View.OnTouchListener getHapticFeedbackTouchListener() {
2733 if (mHapticFeedbackTouchListener == null) {
2734 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002735 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002736 @Override
2737 public boolean onTouch(View v, MotionEvent event) {
2738 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2739 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2740 }
2741 return false;
2742 }
2743 };
2744 }
2745 return mHapticFeedbackTouchListener;
2746 }
2747
Adam Cohen9211d422014-10-07 18:14:53 -07002748 public void onDragStarted(View view) {
2749 if (isOnCustomContent()) {
2750 // Custom content screen doesn't participate in drag and drop. If on custom
2751 // content screen, move to default.
2752 moveWorkspaceToDefaultScreen();
2753 }
2754
2755 if (mLauncherCallbacks != null) {
2756 mLauncherCallbacks.onDragStarted(view);
2757 }
2758 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002759
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002760 /**
2761 * Called when the user stops interacting with the launcher.
2762 * This implies that the user is now on the homescreen and is not doing housekeeping.
2763 */
Adam Cohen9211d422014-10-07 18:14:53 -07002764 protected void onInteractionEnd() {
2765 if (mLauncherCallbacks != null) {
2766 mLauncherCallbacks.onInteractionEnd();
2767 }
2768 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002769
2770 /**
2771 * Called when the user starts interacting with the launcher.
2772 * The possible interactions are:
2773 * - open all apps
2774 * - reorder an app shortcut, or a widget
2775 * - open the overview mode.
2776 * This is a good time to stop doing things that only make sense
2777 * when the user is on the homescreen and not doing housekeeping.
2778 */
Adam Cohen9211d422014-10-07 18:14:53 -07002779 protected void onInteractionBegin() {
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onInteractionBegin();
2782 }
2783 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002784
Winson Chungcd99cd32015-04-29 11:03:24 -07002785 /** Updates the interaction state. */
2786 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002787 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002788 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002789 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2790 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002791 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002792 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002793 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002794 onInteractionEnd();
2795 }
2796 }
2797
Kenny Guyf07af7b2014-07-31 11:39:16 +01002798 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002799 try {
2800 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002801 launcherApps.showAppDetailsForProfile(componentName, user);
2802 } catch (SecurityException e) {
2803 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2804 Log.e(TAG, "Launcher does not have permission to launch settings");
2805 } catch (ActivityNotFoundException e) {
2806 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2807 Log.e(TAG, "Unable to launch settings");
2808 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002809 }
2810
Michael Jurka1e2f4652013-07-08 18:03:46 -07002811 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002812 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2813 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002814 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002815 // System applications cannot be installed. For now, show a toast explaining that.
2816 // We may give them the option of disabling apps this way.
2817 int messageId = R.string.uninstall_system_app_text;
2818 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002819 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002820 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002821 String packageName = componentName.getPackageName();
2822 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002823 Intent intent = new Intent(
2824 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002825 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2826 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002827 if (user != null) {
2828 user.addToIntent(intent, Intent.EXTRA_USER);
2829 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002830 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002831 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002832 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002833 }
2834
Winson Chung8f1eff72015-05-28 17:33:40 -07002835 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002836 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002838 // Only launch using the new animation if the shortcut has not opted out (this is a
2839 // private contract between launcher and may be ignored in the future).
2840 boolean useLaunchAnimation = (v != null) &&
2841 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002842 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2843 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002844
Kenny Guy1317e2d2014-05-08 18:52:50 +01002845 UserHandleCompat user = null;
2846 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2847 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2848 user = userManager.getUserForSerialNumber(serialNumber);
2849 }
2850
2851 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002852 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002853 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002854 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002855 int left = 0, top = 0;
2856 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2857 if (v instanceof TextView) {
2858 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002859 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2860 if (icon != null) {
2861 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002862 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002863 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002864 width = bounds.width();
2865 height = bounds.height();
2866 }
2867 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002868 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2869 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002870 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002871 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002872 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002873 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002874 // On L devices, we use the device default slide-up transition.
2875 // On L MR1 devices, we a custom version of the slide-up transition which
2876 // doesn't have the delay present in the device default.
2877 opts = ActivityOptions.makeCustomAnimation(this,
2878 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002879 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002880 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002881 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002882
2883 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2884 // Could be launching some bookkeeping activity
2885 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002886 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002887 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002888 launcherApps.startActivityForProfile(intent.getComponent(), user,
2889 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002890 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002891 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 } catch (SecurityException e) {
2893 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002894 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002896 "or use the exported attribute for this activity. "
2897 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002899 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002901
Winson Chungbedf9232015-07-10 12:38:30 -07002902 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002903 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002904 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2905 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2906 return false;
2907 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002908 try {
2909 success = startActivity(v, intent, tag);
2910 } catch (ActivityNotFoundException e) {
2911 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2912 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2913 }
2914 return success;
2915 }
2916
Adam Cohen268c4752012-06-06 17:47:33 -07002917 /**
2918 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2919 * in the DragLayer in the exact absolute location of the original FolderIcon.
2920 */
2921 private void copyFolderIconToImage(FolderIcon fi) {
2922 final int width = fi.getMeasuredWidth();
2923 final int height = fi.getMeasuredHeight();
2924
2925 // Lazy load ImageView, Bitmap and Canvas
2926 if (mFolderIconImageView == null) {
2927 mFolderIconImageView = new ImageView(this);
2928 }
2929 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2930 mFolderIconBitmap.getHeight() != height) {
2931 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2932 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2933 }
2934
2935 DragLayer.LayoutParams lp;
2936 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2937 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2938 } else {
2939 lp = new DragLayer.LayoutParams(width, height);
2940 }
2941
Adam Cohen307fe232012-08-16 17:55:58 -07002942 // The layout from which the folder is being opened may be scaled, adjust the starting
2943 // view size by this scale factor.
2944 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002945 lp.customPosition = true;
2946 lp.x = mRectForFolderAnimation.left;
2947 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002948 lp.width = (int) (scale * width);
2949 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002950
2951 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2952 fi.draw(mFolderIconCanvas);
2953 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002954 if (fi.getFolder() != null) {
2955 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2956 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002957 }
Adam Cohen268c4752012-06-06 17:47:33 -07002958 // Just in case this image view is still in the drag layer from a previous animation,
2959 // we remove it and re-add it.
2960 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2961 mDragLayer.removeView(mFolderIconImageView);
2962 }
2963 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002964 if (fi.getFolder() != null) {
2965 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002966 }
Adam Cohen268c4752012-06-06 17:47:33 -07002967 }
2968
Adam Cohen2801caf2011-05-13 20:57:39 -07002969 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002970 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002971 FolderInfo info = (FolderInfo) fi.getTag();
2972 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2973 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002974 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2975 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002976 }
2977
Adam Cohen268c4752012-06-06 17:47:33 -07002978 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2979 copyFolderIconToImage(fi);
2980 fi.setVisibility(View.INVISIBLE);
2981
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002982 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2983 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002984 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002985 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2986 }
2987 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002988 oa.start();
2989 }
2990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002992 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002993 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002994
Adam Cohen268c4752012-06-06 17:47:33 -07002995 // We remove and re-draw the FolderIcon in-case it has changed
2996 mDragLayer.removeView(mFolderIconImageView);
2997 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002998 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002999 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 oa.addListener(new AnimatorListenerAdapter() {
3001 @Override
3002 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003003 if (cl != null) {
3004 cl.clearFolderLeaveBehind();
3005 // Remove the ImageView copy of the FolderIcon and make the original visible.
3006 mDragLayer.removeView(mFolderIconImageView);
3007 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003008 }
3009 }
3010 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 oa.start();
3012 }
3013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003015 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 * is animated relative to the specified View. If the View is null, no animation
3017 * is played.
3018 *
3019 * @param folderInfo The FolderInfo describing the folder to open.
3020 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003021 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003023 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3024 if (openFolder != null && openFolder != folder) {
3025 // Close any open folder before opening a folder.
3026 closeFolder();
3027 }
3028
Adam Cohena9cf38f2011-05-02 15:36:58 -07003029 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030
Adam Cohena9cf38f2011-05-02 15:36:58 -07003031 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032
Sunny Goyalf4066152015-04-15 09:42:19 -07003033 // While the folder is open, the position of the icon cannot change.
3034 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3035
Adam Cohen4554ee12011-08-03 16:13:21 -07003036 // Just verify that the folder hasn't already been added to the DragLayer.
3037 // There was a one-off crash where the folder had a parent already.
3038 if (folder.getParent() == null) {
3039 mDragLayer.addView(folder);
3040 mDragController.addDropTarget((DropTarget) folder);
3041 } else {
3042 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3043 folder.getParent() + ").");
3044 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003045 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003046 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003047
3048 // Notify the accessibility manager that this folder "window" has appeared and occluded
3049 // the workspace items
3050 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3051 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003052 }
3053
3054 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003055 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003057 if (folder.isEditingName()) {
3058 folder.dismissEditingName();
3059 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003060 closeFolder(folder);
3061 }
3062 }
3063
Sunny Goyal83a8f042015-05-19 12:52:12 -07003064 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003065 folder.getInfo().opened = false;
3066
3067 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3068 if (parent != null) {
3069 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3070 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003071 if (fi != null) {
3072 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3073 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003074 }
3075 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003076
3077 // Notify the accessibility manager that this folder "window" has disappeard and no
3078 // longer occludeds the workspace items
3079 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 }
3081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003083 if (!isDraggingEnabled()) return false;
3084 if (isWorkspaceLocked()) return false;
3085 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086
Winson Chung76648c52015-07-10 14:33:23 -07003087 if (v == mAllAppsButton) {
3088 onLongClickAllAppsButton(v);
3089 return true;
3090 }
3091
Adam Cohen1697b792013-09-17 19:08:21 -07003092 if (v instanceof Workspace) {
3093 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003094 if (!mWorkspace.isTouchActive()) {
3095 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003096 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3097 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3098 return true;
3099 } else {
3100 return false;
3101 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003102 } else {
3103 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003104 }
Adam Cohen1697b792013-09-17 19:08:21 -07003105 }
3106
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003107 CellLayout.CellInfo longClickCellInfo = null;
3108 View itemUnderLongClick = null;
3109 if (v.getTag() instanceof ItemInfo) {
3110 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003111 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003112 itemUnderLongClick = longClickCellInfo.cell;
3113 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115
Winson Chung3d503fb2011-07-13 17:25:49 -07003116 // The hotseat touch handling does not go through Workspace, and we always allow long press
3117 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003118 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003119 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003120 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003121 // User long pressed on empty space
3122 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3123 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003124 if (mWorkspace.isInOverviewMode()) {
3125 mWorkspace.startReordering(v);
3126 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003127 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003130 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3131 mHotseat.getOrderInHotseat(
3132 longClickCellInfo.cellX,
3133 longClickCellInfo.cellY));
3134 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003136 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138 }
3139 }
3140 return true;
3141 }
3142
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 boolean isHotseatLayout(View layout) {
3144 return mHotseat != null && layout != null &&
3145 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3146 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003147
Winson Chung3d503fb2011-07-13 17:25:49 -07003148 /**
3149 * Returns the CellLayout of the specified container at the specified screen.
3150 */
Sunny Goyal92820592015-03-02 11:31:35 -08003151 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3153 if (mHotseat != null) {
3154 return mHotseat.getLayout();
3155 } else {
3156 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003157 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003159 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003160 }
3161 }
3162
Winson Chungb745afb2015-03-02 11:51:23 -08003163 /**
3164 * For overridden classes.
3165 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003166 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003167 return isAppsViewVisible();
3168 }
3169
3170 public boolean isAppsViewVisible() {
3171 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3172 }
3173
3174 public boolean isWidgetsViewVisible() {
3175 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003176 }
3177
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003178 private void setWorkspaceBackground(int background) {
3179 switch (background) {
3180 case WORKSPACE_BACKGROUND_TRANSPARENT:
3181 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3182 break;
3183 case WORKSPACE_BACKGROUND_BLACK:
3184 getWindow().setBackgroundDrawable(null);
3185 break;
3186 default:
3187 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3188 }
Craig Mautner360310b2012-10-26 15:13:08 -07003189 }
3190
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003191 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003192 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3193 int curflags = getWindow().getAttributes().flags
3194 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3195 if (wpflags != curflags) {
3196 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3197 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003198 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003199 }
3200
Michael Jurkae326f182011-11-21 14:05:46 -08003201 @Override
3202 public void onTrimMemory(int level) {
3203 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003204 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3205 // The widget preview db can result in holding onto over
3206 // 3MB of memory for caching which isn't necessary.
3207 SQLiteDatabase.releaseMemory();
3208
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003209 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003210 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003211 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003212 if (mLauncherCallbacks != null) {
3213 mLauncherCallbacks.onTrimMemory(level);
3214 }
Michael Jurkae326f182011-11-21 14:05:46 -08003215 }
3216
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003217 public void showWorkspace(boolean animated) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003218 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003219 }
3220
Sunny Goyal83a8f042015-05-19 12:52:12 -07003221 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003222 boolean changed = mState != State.WORKSPACE ||
3223 mWorkspace.getState() != Workspace.State.NORMAL;
3224 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003225 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003226 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003227 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003228
Michael Jurkab3e22d92011-10-31 15:58:33 -07003229 // Set focus to the AppsCustomize button
3230 if (mAllAppsButton != null) {
3231 mAllAppsButton.requestFocus();
3232 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003233 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003234
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003235 // Change the state *after* we've called all the transition code
3236 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003237
Winson Chung5fb63472011-02-02 17:03:37 -08003238 // Resume the auto-advance of widgets
3239 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003240 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003241
Winson Chung0f785722015-04-08 10:27:49 -07003242 if (changed) {
3243 // Send an accessibility event to announce the context change
3244 getWindow().getDecorView()
3245 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003246 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003247 }
3248
Adam Cohened307df2013-10-02 09:37:31 -07003249 void showOverviewMode(boolean animated) {
3250 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003251 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003252 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003253 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003254 }
3255
Winson Chungb745afb2015-03-02 11:51:23 -08003256 /**
3257 * Shows the apps view.
3258 */
Winson Chung76648c52015-07-10 14:33:23 -07003259 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3260 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003261 if (resetListToTop) {
3262 mAppsView.scrollToTop();
3263 }
Winson Chung4ac30062015-05-08 17:34:17 -07003264 if (updatePredictedApps) {
3265 tryAndUpdatePredictedApps();
3266 }
Winson Chung76648c52015-07-10 14:33:23 -07003267 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003268 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003269
Winson Chungb745afb2015-03-02 11:51:23 -08003270 /**
3271 * Shows the widgets view.
3272 */
3273 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003274 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003275 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003276 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003277 }
Winson Chung76648c52015-07-10 14:33:23 -07003278 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003279
3280 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003281 @Override
3282 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003283 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003284 }
3285 });
Winson Chungb745afb2015-03-02 11:51:23 -08003286 }
3287
3288 /**
3289 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003290 *
3291 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003292 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003293 // TODO: calling method should use the return value so that when {@code false} is returned
3294 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003295 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003296 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3297 mState != State.WIDGETS_SPRING_LOADED) {
3298 return false;
3299 }
3300 if (toState != State.APPS && toState != State.WIDGETS) {
3301 return false;
3302 }
Winson Chungb745afb2015-03-02 11:51:23 -08003303
3304 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003305 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3306 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003307 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003308 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003309 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003310
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003312 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313
3314 // Pause the auto-advance of widgets until we are out of AllApps
3315 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003316 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 closeFolder();
3318
3319 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003320 getWindow().getDecorView()
3321 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003322 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003323 }
3324
Winson Chungcd99cd32015-04-29 11:03:24 -07003325 /**
3326 * Updates the workspace and interaction state on state change, and return the animation to this
3327 * new state.
3328 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003329 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003330 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003331 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003332 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003333 updateInteraction(fromState, toState);
3334 return anim;
3335 }
3336
Jun Mukaif0033da2015-08-04 18:34:30 -07003337 public void onLauncherClingShown() {
3338 // When a launcher cling appears, it should cover the underlying layers, so their focus
3339 // should be blocked.
3340 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3341 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3342 }
3343 }
3344
3345 public void onLauncherClingDismissed() {
3346 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3347 }
3348
Hyunyoung Song3f471442015-04-08 19:01:34 -07003349 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003350 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003351 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3352 mState == State.WIDGETS_SPRING_LOADED) {
3353 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003354 }
Winson Chungb745afb2015-03-02 11:51:23 -08003355
Winson Chung006ee262015-08-03 14:40:11 -07003356 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003357 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003358 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003359 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003360 }
Adam Cohen7777d962011-08-18 18:58:38 -07003361
Hyunyoung Song3f471442015-04-08 19:01:34 -07003362 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003363 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003364 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003365
Winson Chunge7a03942011-08-05 15:05:12 -07003366 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003367 @Override
3368 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003369 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003370 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3371 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003372 // Before we show workspace, hide all apps again because
3373 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3374 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003376 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003377 } else {
3378 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003379 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003380 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003381 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003382 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383
Michael Jurkad3ef3062010-11-23 16:23:58 -08003384 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003385 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003386 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003387 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003388 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003389 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003390 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391 }
3392
Winson Chung4ac30062015-05-08 17:34:17 -07003393 /**
3394 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3395 * resumed.
3396 */
3397 private void tryAndUpdatePredictedApps() {
3398 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003399 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003400 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003401 mAppsView.setPredictedApps(apps);
3402 }
3403 }
3404 }
3405
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 void lockAllApps() {
3407 // TODO
3408 }
3409
3410 void unlockAllApps() {
3411 // TODO
3412 }
3413
Sunny Goyal594d76d2014-11-06 10:12:54 -08003414 protected void disableVoiceButtonProxy(boolean disable) {
3415 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003416 }
3417
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003418 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003419 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3420 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003421 }
3422
Adam Cohen24ce0b32014-01-14 16:18:14 -08003423 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003424 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3425 if (searchProvider == null) {
3426 return null;
3427 }
3428
3429 Bundle opts = new Bundle();
3430 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003431 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003432
3433 SharedPreferences sp = getSharedPreferences(
3434 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3435 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003436 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003437 if (!searchProvider.provider.flattenToString().equals(
3438 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003439 || (widgetInfo == null)
3440 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003441 // A valid widget is not already bound.
3442 if (widgetId > -1) {
3443 mAppWidgetHost.deleteAppWidgetId(widgetId);
3444 widgetId = -1;
3445 }
3446
3447 // Try to bind a new widget
3448 widgetId = mAppWidgetHost.allocateAppWidgetId();
3449
3450 if (!AppWidgetManagerCompat.getInstance(this)
3451 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3452 mAppWidgetHost.deleteAppWidgetId(widgetId);
3453 widgetId = -1;
3454 }
3455
3456 sp.edit()
3457 .putInt(QSB_WIDGET_ID, widgetId)
3458 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3459 .commit();
3460 }
3461
Sunny Goyal8d595092015-07-06 12:22:14 -07003462 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003463 if (widgetId != -1) {
3464 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003465 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003466 mQsb.updateAppWidgetOptions(opts);
3467 mQsb.setPadding(0, 0, 0, 0);
3468 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003469 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003470 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003471 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003472 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003473 }
3474
Sunny Goyal22235bc2015-04-03 09:23:43 -07003475 private void reinflateQSBIfNecessary() {
3476 if (mQsb instanceof LauncherAppWidgetHostView &&
3477 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3478 mSearchDropTargetBar.removeView(mQsb);
3479 mQsb = null;
3480 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3481 }
3482 }
3483
Michael Jurkad7c28052012-04-27 15:43:36 -07003484 @Override
3485 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003486 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003487 final List<CharSequence> text = event.getText();
3488 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003489 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003490 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003491 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003492 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003493 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003494 } else if (mWorkspace != null) {
3495 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003496 } else {
3497 text.add(getString(R.string.all_apps_home_button_label));
3498 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003499 return result;
3500 }
3501
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003502 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003503 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003504 */
Adam Cohen091440a2015-03-18 14:16:05 -07003505 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003506 @Override
3507 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003508 closeSystemDialogs();
3509 }
3510 }
3511
3512 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003513 * If the activity is currently paused, signal that we need to run the passed Runnable
3514 * in onResume.
3515 *
3516 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003517 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3518 * wrong when we're not running, and if the activity comes back to what the configuration was
3519 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003520 *
3521 * Implementation of the method from LauncherModel.Callbacks.
3522 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003523 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003524 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003525 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003526 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003527 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003528 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003529 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003530 }
3531 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003532 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003533 return true;
3534 } else {
3535 return false;
3536 }
3537 }
3538
Michael Jurkac402cd92013-05-20 15:49:32 +02003539 private boolean waitUntilResume(Runnable run) {
3540 return waitUntilResume(run, false);
3541 }
3542
Michael Jurka1e2f4652013-07-08 18:03:46 -07003543 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003544 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003545 }
3546
Michael Jurka7607c2f2013-04-03 14:33:19 -07003547 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003548 * If the activity is currently paused, signal that we need to re-run the loader
3549 * in onResume.
3550 *
3551 * This needs to be called from incoming places where resources might have been loaded
3552 * while we are paused. That is becaues the Configuration might be wrong
3553 * when we're not running, and if it comes back to what it was when we
3554 * were paused, we are not restarted.
3555 *
3556 * Implementation of the method from LauncherModel.Callbacks.
3557 *
3558 * @return true if we are currently paused. The caller might be able to
3559 * skip some work in that case since we will come back again.
3560 */
3561 public boolean setLoadOnResume() {
3562 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003563 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003564 mOnResumeNeedsLoad = true;
3565 return true;
3566 } else {
3567 return false;
3568 }
3569 }
3570
3571 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003573 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003575 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003576 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003577 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003578 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003581
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 /**
3583 * Refreshes the shortcuts shown on the workspace.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
3587 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003588 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003589
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 // If we're starting binding all over again, clear any bind calls we'd postponed in
3591 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3592 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003593 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003594
Winson Chungd64d1762013-08-20 14:37:16 -07003595 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003596 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003597 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003598
Michael Jurka05bf644e2011-11-30 20:28:53 -08003599 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003600 if (mHotseat != null) {
3601 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003602 }
3603 }
3604
Adam Cohendcd297f2013-06-18 13:13:40 -07003605 @Override
3606 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003607 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003608
Adam Cohen5084cba2013-09-03 12:01:16 -07003609 // If there are no screens, we need to have an empty screen
3610 if (orderedScreenIds.size() == 0) {
3611 mWorkspace.addExtraEmptyScreen();
3612 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003613
3614 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003615 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003616 if (hasCustomContentToLeft()) {
3617 mWorkspace.createCustomContentContainer();
3618 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003619 }
Winson Chung64359a52013-07-08 17:17:08 -07003620 }
3621
3622 @Override
3623 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003624 int count = orderedScreenIds.size();
3625 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003626 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003627 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003628 }
3629
Adam Cohen39a06042013-07-19 14:30:12 -07003630 private boolean shouldShowWeightWatcher() {
3631 String spKey = LauncherAppState.getSharedPreferencesKey();
3632 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003633 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003634
3635 return show;
3636 }
3637
3638 private void toggleShowWeightWatcher() {
3639 String spKey = LauncherAppState.getSharedPreferencesKey();
3640 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3641 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3642
3643 show = !show;
3644
3645 SharedPreferences.Editor editor = sp.edit();
3646 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3647 editor.commit();
3648
3649 if (mWeightWatcher != null) {
3650 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3651 }
3652 }
3653
Winson Chungd64d1762013-08-20 14:37:16 -07003654 public void bindAppsAdded(final ArrayList<Long> newScreens,
3655 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003656 final ArrayList<ItemInfo> addAnimated,
3657 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003658 Runnable r = new Runnable() {
3659 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003660 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003661 }
3662 };
3663 if (waitUntilResume(r)) {
3664 return;
3665 }
3666
Winson Chungd64d1762013-08-20 14:37:16 -07003667 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003668 if (newScreens != null) {
3669 bindAddScreens(newScreens);
3670 }
Winson Chungd64d1762013-08-20 14:37:16 -07003671
3672 // We add the items without animation on non-visible pages, and with
3673 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003674 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003675 bindItems(addNotAnimated, 0,
3676 addNotAnimated.size(), false);
3677 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003678 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003679 bindItems(addAnimated, 0,
3680 addAnimated.size(), true);
3681 }
Winson Chungc58497e2013-09-03 17:48:37 -07003682
Winson Chung87412982013-10-03 18:34:14 -07003683 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003684 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003685
Winson Chungb745afb2015-03-02 11:51:23 -08003686 if (addedApps != null && mAppsView != null) {
3687 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003688 }
Winson Chungd64d1762013-08-20 14:37:16 -07003689 }
3690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003691 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 * Bind the items start-end from the list.
3693 *
3694 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003695 */
Winson Chung64359a52013-07-08 17:17:08 -07003696 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3697 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003698 Runnable r = new Runnable() {
3699 public void run() {
3700 bindItems(shortcuts, start, end, forceAnimateIcons);
3701 }
3702 };
3703 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003704 return;
3705 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003706
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003708 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3709 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003710 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003712 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003713 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003714 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003715
3716 // Short circuit if we are loading dock items for a configuration which has no dock
3717 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3718 mHotseat == null) {
3719 continue;
3720 }
3721
Sunny Goyal639e9062015-08-19 19:17:06 -07003722 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 switch (item.itemType) {
3724 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3725 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003726 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003727 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003728
Winson Chung64359a52013-07-08 17:17:08 -07003729 /*
3730 * TODO: FIX collision case
3731 */
Winson Chungce376632013-07-11 16:12:41 -07003732 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3733 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3734 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003735 View v = cl.getChildAt(item.cellX, item.cellY);
3736 Object tag = v.getTag();
3737 String desc = "Collision while binding workspace item: " + item
3738 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003739 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003740 throw (new RuntimeException(desc));
3741 } else {
3742 Log.d(TAG, desc);
3743 }
Winson Chungce376632013-07-11 16:12:41 -07003744 }
Winson Chung64359a52013-07-08 17:17:08 -07003745 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003747 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003748 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003749 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003750 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003752 default:
3753 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003755
3756 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3757 item.cellY, 1, 1);
3758 if (animateIcons) {
3759 // Animate all the applications up now
3760 view.setAlpha(0f);
3761 view.setScaleX(0f);
3762 view.setScaleY(0f);
3763 bounceAnims.add(createNewAppBounceAnimation(view, i));
3764 newShortcutsScreenId = item.screenId;
3765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003766 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003767
Winson Chung997a9232013-07-24 15:33:46 -07003768 if (animateIcons) {
3769 // Animate to the correct page
3770 if (newShortcutsScreenId > -1) {
3771 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003772 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003773 final Runnable startBounceAnimRunnable = new Runnable() {
3774 public void run() {
3775 anim.playTogether(bounceAnims);
3776 anim.start();
3777 }
3778 };
Winson Chung997a9232013-07-24 15:33:46 -07003779 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003780 // We post the animation slightly delayed to prevent slowdowns
3781 // when we are loading right after we return to launcher.
3782 mWorkspace.postDelayed(new Runnable() {
3783 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003784 if (mWorkspace != null) {
3785 mWorkspace.snapToPage(newScreenIndex);
3786 mWorkspace.postDelayed(startBounceAnimRunnable,
3787 NEW_APPS_ANIMATION_DELAY);
3788 }
Winson Chung94d67682013-09-25 16:29:40 -07003789 }
3790 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003791 } else {
3792 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003793 }
3794 }
Winson Chung64359a52013-07-08 17:17:08 -07003795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003797 }
3798
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 /**
3800 * Implementation of the method from LauncherModel.Callbacks.
3801 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003802 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 Runnable r = new Runnable() {
3804 public void run() {
3805 bindFolders(folders);
3806 }
3807 };
3808 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003809 return;
3810 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003811 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813
3814 /**
3815 * Add the views for a widget to the workspace.
3816 *
3817 * Implementation of the method from LauncherModel.Callbacks.
3818 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003819 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 Runnable r = new Runnable() {
3821 public void run() {
3822 bindAppWidget(item);
3823 }
3824 };
3825 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003826 return;
3827 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003828
Daniel Sandler843e8602010-06-07 14:59:01 -04003829 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3830 if (DEBUG_WIDGETS) {
3831 Log.d(TAG, "bindAppWidget: " + item);
3832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 final Workspace workspace = mWorkspace;
3834
Adam Cohen59400422014-03-05 18:07:04 -08003835 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003836 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003837
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003838 if (!mIsSafeModeEnabled
3839 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003840 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3841
Sunny Goyalff572272014-07-23 13:58:07 -07003842 if (appWidgetInfo == null) {
3843 if (DEBUG_WIDGETS) {
3844 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3845 + " belongs to component " + item.providerName
3846 + ", as the povider is null");
3847 }
3848 LauncherModel.deleteItemFromDatabase(this, item);
3849 return;
3850 }
Sunny Goyalff572272014-07-23 13:58:07 -07003851
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003852 // If we do not have a valid id, try to bind an id.
3853 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3854 // Note: This assumes that the id remap broadcast is received before this step.
3855 // If that is not the case, the id remap will be ignored and user may see the
3856 // click to setup view.
3857 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3858 pendingInfo.spanX = item.spanX;
3859 pendingInfo.spanY = item.spanY;
3860 pendingInfo.minSpanX = item.minSpanX;
3861 pendingInfo.minSpanY = item.minSpanY;
3862 Bundle options = null;
3863 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003864
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003865 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3866 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3867 newWidgetId, appWidgetInfo, options);
3868
3869 // TODO consider showing a permission dialog when the widget is clicked.
3870 if (!success) {
3871 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3872 if (DEBUG_WIDGETS) {
3873 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3874 + " belongs to component " + item.providerName
3875 + ", as the launcher is unable to bing a new widget id");
3876 }
3877 LauncherModel.deleteItemFromDatabase(this, item);
3878 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003879 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003880
3881 item.appWidgetId = newWidgetId;
3882
3883 // If the widget has a configure activity, it is still needs to set it up, otherwise
3884 // the widget is ready to go.
3885 item.restoreStatus = (appWidgetInfo.configure == null)
3886 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3887 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3888
3889 LauncherModel.updateItemInDatabase(this, item);
3890 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3891 && (appWidgetInfo.configure == null)) {
3892 // If the ID is already valid, verify if we need to configure or not.
3893 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3894 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003895 }
Sunny Goyalff572272014-07-23 13:58:07 -07003896 }
3897
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003898 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003899 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003900 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003901 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3902 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003903 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003904
Sunny Goyal651077b2014-06-30 14:15:31 -07003905 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003906 item.minSpanX = appWidgetInfo.minSpanX;
3907 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003908 } else {
3909 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003910 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3911 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003912 view.updateIcon(mIconCache);
3913 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003914 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003915 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003916 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003919 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920
Adam Cohendcd297f2013-06-18 13:13:40 -07003921 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003923 if (!item.isCustomWidget()) {
3924 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3925 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003926
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 workspace.requestLayout();
3928
Daniel Sandler843e8602010-06-07 14:59:01 -04003929 if (DEBUG_WIDGETS) {
3930 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3931 + (SystemClock.uptimeMillis()-start) + "ms");
3932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 }
3934
Sunny Goyalff572272014-07-23 13:58:07 -07003935 /**
3936 * Restores a pending widget.
3937 *
3938 * @param appWidgetId The app widget id
3939 * @param cellInfo The position on screen where to create the widget.
3940 */
3941 private void completeRestoreAppWidget(final int appWidgetId) {
3942 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3943 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3944 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3945 return;
3946 }
3947
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003948 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003949 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3950
3951 mWorkspace.reinflateWidgetsIfNecessary();
3952 LauncherModel.updateItemInDatabase(this, info);
3953 }
3954
Adam Cohen1462de32012-07-24 22:34:36 -07003955 public void onPageBoundSynchronously(int page) {
3956 mSynchronouslyBoundPages.add(page);
3957 }
3958
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 /**
3960 * Callback saying that there aren't any more items to bind.
3961 *
3962 * Implementation of the method from LauncherModel.Callbacks.
3963 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003964 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003965 Runnable r = new Runnable() {
3966 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003967 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003968 }
3969 };
3970 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003971 return;
3972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 if (mSavedState != null) {
3974 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003975 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 mSavedState = null;
3978 }
3979
Adam Cohen1462de32012-07-24 22:34:36 -07003980 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003982 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003983 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003984
3985 // If we received the result of any pending adds while the loader was running (e.g. the
3986 // widget configuration forced an orientation change), process them now.
3987 if (sPendingAddItem != null) {
3988 final long screenId = completeAdd(sPendingAddItem);
3989
3990 // TODO: this moves the user to the page where the pending item was added. Ideally,
3991 // the screen would be guaranteed to exist after bind, and the page would be set through
3992 // the workspace restore process.
3993 mWorkspace.post(new Runnable() {
3994 @Override
3995 public void run() {
3996 mWorkspace.snapToScreenId(screenId);
3997 }
3998 });
3999 sPendingAddItem = null;
4000 }
4001
Sunny Goyal756adbc2015-04-16 15:20:51 -07004002 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004003
4004 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004005 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004006 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004007 }
4008
Adam Cohen3ed316a2014-07-23 18:21:20 -07004009 private void sendLoadingCompleteBroadcastIfNecessary() {
4010 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4011 String permission =
4012 getResources().getString(R.string.receive_first_load_broadcast_permission);
4013 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4014 sendBroadcast(intent, permission);
4015 SharedPreferences.Editor editor = mSharedPrefs.edit();
4016 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4017 editor.apply();
4018 }
4019 }
4020
Winson Chunga0b7e862013-09-05 16:03:15 -07004021 public boolean isAllAppsButtonRank(int rank) {
4022 if (mHotseat != null) {
4023 return mHotseat.isAllAppsButtonRank(rank);
4024 }
4025 return false;
4026 }
4027
Winson Chunga2413752012-04-03 14:22:34 -07004028 private boolean canRunNewAppsAnimation() {
4029 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004030 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4031 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004032 }
4033
Winson Chungc9168342013-06-26 14:54:55 -07004034 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004035 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004036 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4037 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004038 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004039 return bounceAnim;
4040 }
4041
Adam Cohen27772732013-11-11 14:00:56 +00004042 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004043 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004044 }
4045
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004046 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004047 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004048 }
4049
Winson Chung88fa7412015-08-03 14:25:28 -07004050 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004051 if (mSearchDropTargetBar == null) {
4052 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004053 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004054 if (mQsb != null) {
4055 mSearchDropTargetBar.removeView(mQsb);
4056 mQsb = null;
4057 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004058 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004059 }
4060
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004061 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004062 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4063 * multiple calls to bind the same list.)
4064 */
4065 @Thunk ArrayList<AppInfo> mTmpAppsList;
4066 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4067 public void run() {
4068 bindAllApplications(mTmpAppsList);
4069 mTmpAppsList = null;
4070 }
4071 };
4072
4073 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 * Add the icons for all apps.
4075 *
4076 * Implementation of the method from LauncherModel.Callbacks.
4077 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004078 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004079 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4080 mTmpAppsList = apps;
4081 return;
4082 }
4083
Winson Chungb745afb2015-03-02 11:51:23 -08004084 if (mAppsView != null) {
4085 mAppsView.setApps(apps);
4086 }
Adam Cohen9211d422014-10-07 18:14:53 -07004087 if (mLauncherCallbacks != null) {
4088 mLauncherCallbacks.bindAllApplications(apps);
4089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 }
4091
4092 /**
4093 * A package was updated.
4094 *
4095 * Implementation of the method from LauncherModel.Callbacks.
4096 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004097 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004098 Runnable r = new Runnable() {
4099 public void run() {
4100 bindAppsUpdated(apps);
4101 }
4102 };
4103 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004104 return;
4105 }
4106
Winson Chungb745afb2015-03-02 11:51:23 -08004107 if (mAppsView != null) {
4108 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004109 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 }
4111
Sunny Goyal4390ace2014-10-13 11:33:11 -07004112 @Override
4113 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4114 Runnable r = new Runnable() {
4115 public void run() {
4116 bindWidgetsRestored(widgets);
4117 }
4118 };
4119 if (waitUntilResume(r)) {
4120 return;
4121 }
4122 mWorkspace.widgetsRestored(widgets);
4123 }
4124
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004126 * Some shortcuts were updated in the background.
4127 *
4128 * Implementation of the method from LauncherModel.Callbacks.
4129 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004130 @Override
4131 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4132 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004133 Runnable r = new Runnable() {
4134 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004135 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004136 }
4137 };
4138 if (waitUntilResume(r)) {
4139 return;
4140 }
4141
Sunny Goyal4390ace2014-10-13 11:33:11 -07004142 if (!updated.isEmpty()) {
4143 mWorkspace.updateShortcuts(updated);
4144 }
4145
4146 if (!removed.isEmpty()) {
4147 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4148 for (ShortcutInfo si : removed) {
4149 removedComponents.add(si.getTargetComponent());
4150 }
4151 mWorkspace.removeItemsByComponentName(removedComponents, user);
4152 // Notify the drag controller
4153 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004154 }
4155 }
4156
4157 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004158 * Update the state of a package, typically related to install state.
4159 *
4160 * Implementation of the method from LauncherModel.Callbacks.
4161 */
Sunny Goyale755d462014-07-22 13:48:29 -07004162 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004163 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4164 Runnable r = new Runnable() {
4165 public void run() {
4166 bindRestoreItemsChange(updates);
4167 }
4168 };
4169 if (waitUntilResume(r)) {
4170 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004171 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004172
Sunny Goyal756adbc2015-04-16 15:20:51 -07004173 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004174 }
4175
4176 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004177 * A package was uninstalled. We take both the super set of packageNames
4178 * in addition to specific applications to remove, the reason being that
4179 * this can be called when a package is updated as well. In that scenario,
4180 * we only remove specific components from the workspace, where as
4181 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004183 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 * Implementation of the method from LauncherModel.Callbacks.
4185 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004186 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004187 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004188 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004189 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004190 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004191 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004192 }
Winson Chungd64d1762013-08-20 14:37:16 -07004193 };
4194 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004195 return;
4196 }
4197
Sunny Goyal1a745e82014-10-02 15:58:31 -07004198 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004199 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4200 for (AppInfo info : appInfos) {
4201 removedComponents.add(info.componentName);
4202 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004203 if (!packageNames.isEmpty()) {
4204 mWorkspace.removeItemsByPackageName(packageNames, user);
4205 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004206 if (!removedComponents.isEmpty()) {
4207 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004208 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004209 // Notify the drag controller
4210 mDragController.onAppsRemoved(packageNames, removedComponents);
4211
Sunny Goyal1a745e82014-10-02 15:58:31 -07004212 } else {
4213 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004214 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004215
Winson Chungdf95eb12013-10-16 14:57:07 -07004216 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004217 if (mAppsView != null) {
4218 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 }
Joe Onoratobe386092009-11-17 17:32:16 -08004221
Michael Jurkac402cd92013-05-20 15:49:32 +02004222 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004223 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004224 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004225 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004226 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004227
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004228 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004229 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004230 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004231 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004232 return;
4233 }
4234
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004235 if (mWidgetsView != null && model != null) {
4236 mWidgetsView.addWidgets(model);
4237 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004238 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004239 }
4240
Winson Chung400438b2011-01-16 17:53:48 -08004241 private int mapConfigurationOriActivityInfoOri(int configOri) {
4242 final Display d = getWindowManager().getDefaultDisplay();
4243 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4244 switch (d.getRotation()) {
4245 case Surface.ROTATION_0:
4246 case Surface.ROTATION_180:
4247 // We are currently in the same basic orientation as the natural orientation
4248 naturalOri = configOri;
4249 break;
4250 case Surface.ROTATION_90:
4251 case Surface.ROTATION_270:
4252 // We are currently in the other basic orientation to the natural orientation
4253 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4254 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4255 break;
4256 }
4257
4258 int[] oriMap = {
4259 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4260 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4261 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4262 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4263 };
4264 // Since the map starts at portrait, we need to offset if this device's natural orientation
4265 // is landscape.
4266 int indexOffset = 0;
4267 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4268 indexOffset = 1;
4269 }
4270 return oriMap[(d.getRotation() + indexOffset) % 4];
4271 }
Adam Cohen446e9402011-09-15 18:21:21 -07004272
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004273 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004274 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004275 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004276 if (Utilities.ATLEAST_JB_MR2) {
4277 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4278 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004279 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4280 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004281 }
Winson Chung4b919f82012-05-01 10:44:08 -07004282 }
4283 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004284
Winson Chung4b919f82012-05-01 10:44:08 -07004285 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004286 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004287 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004288 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004289 } else {
4290 mHandler.postDelayed(new Runnable() {
4291 public void run() {
4292 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4293 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004294 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004295 }
Winson Chung4b919f82012-05-01 10:44:08 -07004296 }
Winson Chung400438b2011-01-16 17:53:48 -08004297 }
4298
Winson Chunge43a1e72014-01-15 10:33:02 -08004299 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004300 if (mLauncherCallbacks != null) {
4301 return mLauncherCallbacks.isLauncherPreinstalled();
4302 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004303 PackageManager pm = getPackageManager();
4304 try {
4305 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4306 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4307 return true;
4308 } else {
4309 return false;
4310 }
4311 } catch (NameNotFoundException e) {
4312 e.printStackTrace();
4313 return false;
4314 }
4315 }
4316
Adam Cohenea90f832014-05-21 15:03:34 -07004317 /**
4318 * This method indicates whether or not we should suggest default wallpaper dimensions
4319 * when our wallpaper cropper was not yet used to set a wallpaper.
4320 */
4321 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004322 if (mLauncherCallbacks != null) {
4323 return mLauncherCallbacks.overrideWallpaperDimensions();
4324 }
Adam Cohenea90f832014-05-21 15:03:34 -07004325 return true;
4326 }
4327
Adam Cohen432609a2014-03-13 17:03:22 -07004328 /**
4329 * To be overridden by subclasses to indicate that there is an activity to launch
4330 * before showing the standard launcher experience.
4331 */
4332 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004333 if (mLauncherCallbacks != null) {
4334 return mLauncherCallbacks.hasFirstRunActivity();
4335 }
Adam Cohen432609a2014-03-13 17:03:22 -07004336 return false;
4337 }
4338
4339 /**
4340 * To be overridden by subclasses to launch any first run activity
4341 */
4342 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004343 if (mLauncherCallbacks != null) {
4344 return mLauncherCallbacks.getFirstRunActivity();
4345 }
Adam Cohen432609a2014-03-13 17:03:22 -07004346 return null;
4347 }
4348
Winson Chunga6945242014-01-08 14:04:34 -08004349 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004350 return !ActivityManager.isRunningInTestHarness() &&
4351 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004352 }
4353
Adam Cohen4a9423d2014-03-28 14:22:31 -07004354 protected boolean hasRunFirstRunActivity() {
4355 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4356 }
4357
Adam Cohen432609a2014-03-13 17:03:22 -07004358 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004359 if (shouldRunFirstRunActivity() &&
4360 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004361 Intent firstRunIntent = getFirstRunActivity();
4362 if (firstRunIntent != null) {
4363 startActivity(firstRunIntent);
4364 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004365 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004366 }
4367 }
Adam Cohen432609a2014-03-13 17:03:22 -07004368 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004369 }
4370
4371 private void markFirstRunActivityShown() {
4372 SharedPreferences.Editor editor = mSharedPrefs.edit();
4373 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4374 editor.apply();
4375 }
4376
Adam Cohen432609a2014-03-13 17:03:22 -07004377 /**
4378 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4379 * screen that must be displayed and dismissed.
4380 */
4381 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004382 if (mLauncherCallbacks != null) {
4383 return mLauncherCallbacks.hasDismissableIntroScreen();
4384 }
Adam Cohen432609a2014-03-13 17:03:22 -07004385 return false;
4386 }
4387
4388 /**
4389 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4390 */
4391 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004392 if (mLauncherCallbacks != null) {
4393 return mLauncherCallbacks.getIntroScreen();
4394 }
Adam Cohen432609a2014-03-13 17:03:22 -07004395 return null;
4396 }
4397
4398 /**
4399 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4400 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4401 */
4402 private boolean shouldShowIntroScreen() {
4403 return hasDismissableIntroScreen() &&
4404 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4405 }
4406
4407 protected void showIntroScreen() {
4408 View introScreen = getIntroScreen();
4409 changeWallpaperVisiblity(false);
4410 if (introScreen != null) {
4411 mDragLayer.showOverlayView(introScreen);
4412 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004413 if (mLauncherOverlayContainer != null) {
4414 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4415 }
Adam Cohen432609a2014-03-13 17:03:22 -07004416 }
4417
4418 public void dismissIntroScreen() {
4419 markIntroScreenDismissed();
4420 if (showFirstRunActivity()) {
4421 // We delay hiding the intro view until the first run activity is showing. This
4422 // avoids a blip.
4423 mWorkspace.postDelayed(new Runnable() {
4424 @Override
4425 public void run() {
4426 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004427 if (mLauncherOverlayContainer != null) {
4428 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4429 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004430 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004431 }
4432 }, ACTIVITY_START_DELAY);
4433 } else {
4434 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004435 if (mLauncherOverlayContainer != null) {
4436 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4437 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004438 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004439 }
4440 changeWallpaperVisiblity(true);
4441 }
4442
4443 private void markIntroScreenDismissed() {
4444 SharedPreferences.Editor editor = mSharedPrefs.edit();
4445 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4446 editor.apply();
4447 }
4448
Adam Cohen091440a2015-03-18 14:16:05 -07004449 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004450 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4451 // on the device, then we always show the first run cling experience (or if there is no
4452 // launcher2). Otherwise, we prompt the user upon started for migration
4453 LauncherClings launcherClings = new LauncherClings(this);
4454 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004455 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004456 if (mModel.canMigrateFromOldLauncherDb(this)) {
4457 launcherClings.showMigrationCling();
4458 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004459 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004460 }
4461 }
4462 }
4463
Winson Chunga6945242014-01-08 14:04:34 -08004464 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004465 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4466 if (mHotseat != null) mHotseat.setAlpha(1f);
4467 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004468 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4469 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004470 }
4471
4472 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004473 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4474 if (mHotseat != null) mHotseat.setAlpha(0f);
4475 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004476 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4477 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004478 }
4479
Winson Chung5ffd51d2015-06-25 11:36:50 -07004480 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004481 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004482 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004483 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004484 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004485 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004486 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4487 if (userHandle != null) {
4488 user = UserHandleCompat.fromUser(userHandle);
4489 }
4490 }
4491 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004492 }
4493
4494 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004495 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004496 if (user == null) {
4497 user = UserHandleCompat.myUserHandle();
4498 }
4499
4500 // Called from search suggestion, add the profile extra to the intent to ensure that we
4501 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004502 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004503 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004504 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004505 return null;
4506 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004507 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004508 }
4509
Winson Chung5ffd51d2015-06-25 11:36:50 -07004510 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004511 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4512 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004513 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004514 UserHandleCompat.myUserHandle());
4515 }
4516
Winson Chung5ffd51d2015-06-25 11:36:50 -07004517 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004518 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4519 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004520 mWorkspace.onExternalDragStartedWithItem(dragView);
4521 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004522 }
4523
Winson Chung5ffd51d2015-06-25 11:36:50 -07004524 protected void moveWorkspaceToDefaultScreen() {
4525 mWorkspace.moveToDefaultScreen(false);
4526 }
4527
Anjali Koppalf5d29132014-02-28 13:33:27 -08004528 @Override
4529 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004530 if (mLauncherCallbacks != null) {
4531 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4532 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004533 }
4534
Winson Chung80baf5a2010-08-09 16:03:15 -07004535 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004536 * Returns a FastBitmapDrawable with the icon, accurately sized.
4537 */
4538 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4539 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4540 d.setFilterBitmap(true);
4541 resizeIconDrawable(d);
4542 return d;
4543 }
4544
4545 /**
4546 * Resizes an icon drawable to the correct icon size.
4547 */
4548 public void resizeIconDrawable(Drawable icon) {
4549 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4550 }
4551
4552 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004553 * Prints out out state for debugging.
4554 */
4555 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004556 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004557 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004558 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4559 Log.d(TAG, "mRestoring=" + mRestoring);
4560 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004561 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004562 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004563 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004564
Daniel Sandler325dc232013-06-05 22:57:57 -04004565 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004566 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004567
4568 @Override
4569 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4570 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004571 synchronized (sDumpLogs) {
4572 writer.println(" ");
4573 writer.println("Debug logs: ");
4574 for (int i = 0; i < sDumpLogs.size(); i++) {
4575 writer.println(" " + sDumpLogs.get(i));
4576 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004577 }
Adam Cohen9211d422014-10-07 18:14:53 -07004578 if (mLauncherCallbacks != null) {
4579 mLauncherCallbacks.dump(prefix, fd, writer, args);
4580 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004581 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004582
4583 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004584 if (DEBUG_DUMP_LOG) {
4585 synchronized (sDumpLogs) {
4586 Log.d(TAG, "");
4587 Log.d(TAG, "*********************");
4588 Log.d(TAG, "Launcher debug logs: ");
4589 for (int i = 0; i < sDumpLogs.size(); i++) {
4590 Log.d(TAG, " " + sDumpLogs.get(i));
4591 }
4592 Log.d(TAG, "*********************");
4593 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004594 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004595 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004596 }
4597
4598 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004599 addDumpLog(tag, log, null, debugLog);
4600 }
4601
4602 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004603 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004604 if (e != null) {
4605 Log.d(tag, log, e);
4606 } else {
4607 Log.d(tag, log);
4608 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004609 }
Winson Chungede41292013-09-19 16:27:36 -07004610 if (DEBUG_DUMP_LOG) {
4611 sDateStamp.setTime(System.currentTimeMillis());
4612 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004613 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4614 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 }
Winson Chungede41292013-09-19 16:27:36 -07004616 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004617 }
4618
Adam Cohen59400422014-03-05 18:07:04 -08004619 public static CustomAppWidget getCustomAppWidget(String name) {
4620 return sCustomAppWidgets.get(name);
4621 }
4622
4623 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4624 return sCustomAppWidgets;
4625 }
4626
Winson Chungede41292013-09-19 16:27:36 -07004627 public void dumpLogsToLocalData() {
4628 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004629 new AsyncTask<Void, Void, Void>() {
4630 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004631 boolean success = false;
4632 sDateStamp.setTime(sRunStart);
4633 String FILENAME = sDateStamp.getMonth() + "-"
4634 + sDateStamp.getDay() + "_"
4635 + sDateStamp.getHours() + "-"
4636 + sDateStamp.getMinutes() + "_"
4637 + sDateStamp.getSeconds() + ".txt";
4638
4639 FileOutputStream fos = null;
4640 File outFile = null;
4641 try {
4642 outFile = new File(getFilesDir(), FILENAME);
4643 outFile.createNewFile();
4644 fos = new FileOutputStream(outFile);
4645 } catch (Exception e) {
4646 e.printStackTrace();
4647 }
4648 if (fos != null) {
4649 PrintWriter writer = new PrintWriter(fos);
4650
4651 writer.println(" ");
4652 writer.println("Debug logs: ");
4653 synchronized (sDumpLogs) {
4654 for (int i = 0; i < sDumpLogs.size(); i++) {
4655 writer.println(" " + sDumpLogs.get(i));
4656 }
4657 }
4658 writer.close();
4659 }
4660 try {
4661 if (fos != null) {
4662 fos.close();
4663 success = true;
4664 }
4665 } catch (IOException e) {
4666 e.printStackTrace();
4667 }
Michael Jurka43467462013-11-14 12:03:58 +01004668 return null;
Winson Chungede41292013-09-19 16:27:36 -07004669 }
Michael Jurka43467462013-11-14 12:03:58 +01004670 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004671 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004673}
Michael Jurka2763be32011-02-24 11:19:57 -08004674
Dan Sandlerd5024042014-01-09 15:01:33 -05004675interface DebugIntents {
4676 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4677 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004678}