blob: d926cc1700b55dac44e3a0a7e28777b535d640fc [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 Sandler6053b8002013-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 Goyale2df0622015-04-24 11:27:00 -0700290 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Adam Cohen61f560d2013-09-30 15:58:20 -0700292 private View.OnTouchListener mHapticFeedbackTouchListener;
293
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 // Related to the auto-advancing of widgets
295 private final int ADVANCE_MSG = 1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700296 private static final int ADVANCE_INTERVAL = 20000;
297 private static final int ADVANCE_STAGGER = 250;
298
299 private boolean mAutoAdvanceRunning = false;
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 private long mAutoAdvanceSentTime;
301 private long mAutoAdvanceTimeLeft = -1;
Sunny Goyal756cd262015-08-20 12:33:21 -0700302 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance = new HashMap<>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700303
Winson Chung400438b2011-01-16 17:53:48 -0800304 // Determines how long to wait after a rotation before restoring the screen orientation to
305 // match the sensor state.
Sunny Goyal756cd262015-08-20 12:33:21 -0700306 private static final int RESTORE_SCREEN_ORIENTATION_DELAY = 500;
Winson Chung400438b2011-01-16 17:53:48 -0800307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700309
Adam Cohen1462de32012-07-24 22:34:36 -0700310 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700311 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700312
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700313 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700314 static Date sDateStamp = new Date();
315 static DateFormat sDateFormat =
316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
317 static long sRunStart = System.currentTimeMillis();
318 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Winson Chungf0c6ae02012-03-21 16:10:31 -0700324 // Holds the page that we need to animate to, and the icon views that we need to animate up
325 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700326 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700327 private Bitmap mFolderIconBitmap;
328 private Canvas mFolderIconCanvas;
329 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700330
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700331 private DeviceProfile mDeviceProfile;
332
Winson Chung13eb5272015-05-11 16:30:13 -0700333 // This is set to the view that launched the activity that navigated the user away from
334 // launcher. Since there is no callback for when the activity has finished launching, enable
335 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800336 private BubbleTextView mWaitingForResume;
337
Adam Cohen59400422014-03-05 18:07:04 -0800338 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
339 new HashMap<String, CustomAppWidget>();
340
341 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
342 static {
343 if (ENABLE_CUSTOM_WIDGET_TEST) {
344 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
345 }
346 }
347
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Adam Cohendb364c32014-05-20 14:23:40 -0700356 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800357
Adam Cohen091440a2015-03-18 14:16:05 -0700358 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700365 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700369 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700370 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400371
372 @Thunk void setOrientation() {
373 if (mRotationEnabled) {
374 unlockScreenOrientation(true);
375 } else {
376 setRequestedOrientation(
377 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700378 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400379 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700380
Sunny Goyal7779d622015-06-11 16:18:39 -0700381 private Runnable mUpdateOrientationRunnable = new Runnable() {
382 public void run() {
383 setOrientation();
384 }
385 };
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Adam Cohen2e6da152015-05-06 11:42:25 -0700413 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700414 mDeviceProfile = getResources().getConfiguration().orientation
415 == Configuration.ORIENTATION_LANDSCAPE ?
416 app.getInvariantDeviceProfile().landscapeProfile
417 : app.getInvariantDeviceProfile().portraitProfile;
418
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700420 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700424
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chung006ee262015-08-03 14:40:11 -0700426 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
429
Sunny Goyalffe83f12014-08-14 17:39:34 -0700430 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
433 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700435 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
436 // this also ensures that any synchronous binding below doesn't re-trigger another
437 // LauncherModel load.
438 mPaused = false;
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200441 android.os.Debug.startMethodTracing(
442 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700448 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Joe Onorato7c312c12009-08-13 21:36:53 -0700450 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mSavedState = savedInstanceState;
453 restoreState(mSavedState);
454
455 if (PROFILE_STARTUP) {
456 android.os.Debug.stopMethodTracing();
457 }
458
459 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700460 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 // If the user leaves launcher, then we should just load items asynchronously when
462 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700463 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 } else {
465 // We only load the page synchronously if the user rotates (or triggers a
466 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700467 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
476 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800477
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700478 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
479 // In case we are on a device with locked rotation, we should look at preferences to check
480 // if the user has specifically allowed rotation.
481 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400482 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700483 }
484
485 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
486 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400487 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen9211d422014-10-07 18:14:53 -0700489 if (mLauncherCallbacks != null) {
490 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700491 if (mLauncherCallbacks.hasLauncherOverlay()) {
492 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700493 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
494 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
495 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700496 mWorkspace.setLauncherOverlay(mLauncherOverlay);
497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700499
500 if (shouldShowIntroScreen()) {
501 showIntroScreen();
502 } else {
503 showFirstRunActivity();
504 showFirstRunClings();
505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
507
Sunny Goyal7779d622015-06-11 16:18:39 -0700508 @Override
509 public void onSettingsChanged(String settings, boolean value) {
510 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
511 mRotationEnabled = value;
512 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
513 mUpdateOrientationRunnable.run();
514 }
515 }
516 }
517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 private LauncherCallbacks mLauncherCallbacks;
519
520 public void onPostCreate(Bundle savedInstanceState) {
521 super.onPostCreate(savedInstanceState);
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onPostCreate(savedInstanceState);
524 }
525 }
526
527 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
528 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700529 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700530 private boolean mWorkspaceImportanceStored = false;
531 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700532 private int mWorkspaceImportanceForAccessibility =
533 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
534 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
535
536 @Override
537 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700538 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700539 return;
540 }
541 // The underlying workspace and hotseat are temporarily suppressed by the search
542 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700543 if (mWorkspace != null) {
544 mWorkspaceImportanceForAccessibility =
545 mWorkspace.getImportantForAccessibility();
546 mWorkspace.setImportantForAccessibility(
547 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
548 mWorkspaceImportanceStored = true;
549 }
550 if (mHotseat != null) {
551 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
552 mHotseat.setImportantForAccessibility(
553 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
554 mHotseatImportanceStored = true;
555 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 }
557
558 @Override
559 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700560 if (mWorkspaceImportanceStored && mWorkspace != null) {
561 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
562 }
563 if (mHotseatImportanceStored && mHotseat != null) {
564 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
565 }
566 mWorkspaceImportanceStored = false;
567 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700568 }
569 });
Adam Cohen9211d422014-10-07 18:14:53 -0700570 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700571 }
572
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700573 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700574 public void onLauncherProviderChange() {
575 if (mLauncherCallbacks != null) {
576 mLauncherCallbacks.onLauncherProviderChange();
577 }
578 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700579
Winson Chungef7f8742015-06-04 17:18:17 -0700580 /**
581 * Updates the bounds of all the overlays to match the new fixed bounds.
582 */
583 public void updateOverlayBounds(Rect newBounds) {
584 mAppsView.setSearchBarBounds(newBounds);
585 mWidgetsView.setSearchBarBounds(newBounds);
586 }
587
Adam Cohen9211d422014-10-07 18:14:53 -0700588 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700589 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700590 if (mLauncherCallbacks != null) {
591 return mLauncherCallbacks.hasCustomContentToLeft();
592 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700593 return false;
594 }
595
Dave Hawkeya8881582013-09-17 15:55:33 -0600596 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500597 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 * {@link #addToCustomContentPage}. This will only be invoked if
599 * {@link #hasCustomContentToLeft()} is {@code true}.
600 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500601 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 mLauncherCallbacks.populateCustomContentContainer();
604 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600605 }
606
607 /**
608 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
609 * ensure the custom content page is added or removed if necessary.
610 */
611 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600612 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 // Not bound yet, wait for bindScreens to be called.
614 return;
615 }
616
617 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
618 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500619 mWorkspace.createCustomContentContainer();
620 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600621 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
622 mWorkspace.removeCustomContentPage();
623 }
624 }
625
Anton Hanssona2f665f2013-09-26 12:18:32 +0100626 public Stats getStats() {
627 return mStats;
628 }
629
Hyunyoung Song3f471442015-04-08 19:01:34 -0700630 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700631 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
632 // that is subsequently removed from the workspace in startBinding().
633 return !mModel.isLoadingWorkspace();
634 }
635
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800636 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000637 public static int generateViewId() {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700638 if (Utilities.ATLEAST_JB_MR1) {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100639 return View.generateViewId();
640 } else {
641 // View.generateViewId() is not available. The following fallback logic is a copy
642 // of its implementation.
643 for (;;) {
644 final int result = sNextGeneratedId.get();
645 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
646 int newValue = result + 1;
647 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
648 if (sNextGeneratedId.compareAndSet(result, newValue)) {
649 return result;
650 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000651 }
652 }
653 }
654
655 public int getViewIdForItem(ItemInfo info) {
656 // This cast is safe given the > 2B range for int.
657 int itemId = (int) info.id;
658 if (mItemIdToViewId.containsKey(itemId)) {
659 return mItemIdToViewId.get(itemId);
660 }
661 int viewId = generateViewId();
662 mItemIdToViewId.put(itemId, viewId);
663 return viewId;
664 }
665
666 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
668 * a configuration step, this allows the proper animations to run after other transitions.
669 */
Adam Cohendb364c32014-05-20 14:23:40 -0700670 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700671 long screenId = args.screenId;
672 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
673 // When the screen id represents an actual screen (as opposed to a rank) we make sure
674 // that the drop page actually exists.
675 screenId = ensurePendingDropLayoutExists(args.screenId);
676 }
Adam Cohendb364c32014-05-20 14:23:40 -0700677
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700680 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700681 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700682 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700684 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700685 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700686 case REQUEST_RECONFIGURE_APPWIDGET:
687 completeRestoreAppWidget(args.appWidgetId);
688 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 }
Michael Jurka27614d22012-04-02 06:40:22 -0700690 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
691 // if you turned the screen off and then back while in All Apps, Launcher would not
692 // return to the workspace. Clearing mAddInfo.container here fixes this issue
693 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700694 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800695 }
696
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800697 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700698 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700699 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700700 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700701 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800702 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700703
Adam Cohenad4e15c2013-10-17 16:21:35 -0700704 Runnable exitSpringLoaded = new Runnable() {
705 @Override
706 public void run() {
707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
708 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
709 }
710 };
711
Michael Jurka8b805b12012-04-18 14:23:14 -0700712 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700713 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700714 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700715 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
716 if (resultCode == RESULT_CANCELED) {
717 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700718 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700719 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800721 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700722 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700723
724 // When the user has granted permission to bind widgets, we should check to see if
725 // we can inflate the default search bar widget.
726 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
728 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200729 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
730 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700731 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200732 }
733 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200735
Adam Cohened66b2b2012-01-23 17:28:51 -0800736 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700737 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700738
Adam Cohendb364c32014-05-20 14:23:40 -0700739 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800740 // We have special handling for widgets
741 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800742 final int appWidgetId;
743 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
744 : -1;
745 if (widgetId < 0) {
746 appWidgetId = pendingAddWidgetId;
747 } else {
748 appWidgetId = widgetId;
749 }
750
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800752 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700753 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
754 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 result = RESULT_CANCELED;
756 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed(false, 0, null);
761 }
762 };
Adam Cohendb364c32014-05-20 14:23:40 -0700763 if (workspaceLocked) {
764 // No need to remove the empty screen if we're mid-binding, as the
765 // the bind will not add the empty screen.
766 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
767 } else {
768 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
769 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
770 }
Winson Chung5aaab772012-04-27 15:24:02 -0700771 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700772 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700773 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
774 // When the screen id represents an actual screen (as opposed to a rank)
775 // we make sure that the drop page actually exists.
776 mPendingAddInfo.screenId =
777 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
778 }
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
780
781 dropLayout.setDropPending(true);
782 final Runnable onComplete = new Runnable() {
783 @Override
784 public void run() {
785 completeTwoStageWidgetDrop(resultCode, appWidgetId);
786 dropLayout.setDropPending(false);
787 }
788 };
789 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
790 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
791 } else {
792 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
793 mPendingAddInfo);
794 sPendingAddItem = args;
795 }
Winson Chung5aaab772012-04-27 15:24:02 -0700796 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800797 return;
798 }
799
Sunny Goyalff572272014-07-23 13:58:07 -0700800 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
801 if (resultCode == RESULT_OK) {
802 // Update the widget view.
803 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
804 pendingAddWidgetId, mPendingAddInfo);
805 if (workspaceLocked) {
806 sPendingAddItem = args;
807 } else {
808 completeAdd(args);
809 }
810 }
811 // Leave the widget in the pending state if the user canceled the configure.
812 return;
813 }
814
Sunny Goyalaad90582015-07-09 14:22:50 -0700815 if (requestCode == REQUEST_CREATE_SHORTCUT) {
816 // Handle custom shortcuts created using ACTION_CREATE_SHORTCUT.
817 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
818 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
819 mPendingAddInfo);
820 if (isWorkspaceLocked()) {
821 sPendingAddItem = args;
822 } else {
823 completeAdd(args);
824 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
825 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
826 }
827 } else if (resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700828 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800832 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800833
834 }
835
836 @Override
837 protected void onActivityResult(
838 final int requestCode, final int resultCode, final Intent data) {
839 handleActivityResult(requestCode, resultCode, data);
840 if (mLauncherCallbacks != null) {
841 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 }
844
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600845 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700846 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600847 int[] grantResults) {
848 if (mLauncherCallbacks != null) {
849 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
850 grantResults);
851 }
852 }
853
Adam Cohendb364c32014-05-20 14:23:40 -0700854 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
855 appWidgetId, ItemInfo info) {
856 PendingAddArguments args = new PendingAddArguments();
857 args.requestCode = requestCode;
858 args.intent = data;
859 args.container = info.container;
860 args.screenId = info.screenId;
861 args.cellX = info.cellX;
862 args.cellY = info.cellY;
863 args.appWidgetId = appWidgetId;
864 return args;
865 }
866
867 /**
868 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
869 *
870 * @param screenId the screen id to check
871 * @return the new screen, or screenId if it exists
872 */
873 private long ensurePendingDropLayoutExists(long screenId) {
874 CellLayout dropLayout =
875 (CellLayout) mWorkspace.getScreenWithId(screenId);
876 if (dropLayout == null) {
877 // it's possible that the add screen was removed because it was
878 // empty and a re-bind occurred
879 mWorkspace.addExtraEmptyScreen();
880 return mWorkspace.commitExtraEmptyScreen();
881 } else {
882 return screenId;
883 }
884 }
885
Adam Cohen091440a2015-03-18 14:16:05 -0700886 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700887 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 Runnable onCompleteRunnable = null;
890 int animationType = 0;
891
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700892 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 if (resultCode == RESULT_OK) {
894 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
895 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 mPendingAddWidgetInfo);
897 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 onCompleteRunnable = new Runnable() {
899 @Override
900 public void run() {
901 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700902 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700903 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
904 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 }
906 };
907 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800908 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700911 if (mDragLayer.getAnimatedView() != null) {
912 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
913 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
914 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700915 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 // The animated view may be null in the case of a rotation during widget configuration
917 onCompleteRunnable.run();
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
921 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700922 protected void onStop() {
923 super.onStop();
924 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700925
926 if (mLauncherCallbacks != null) {
927 mLauncherCallbacks.onStop();
928 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700929 }
930
931 @Override
932 protected void onStart() {
933 super.onStart();
934 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700935
936 if (mLauncherCallbacks != null) {
937 mLauncherCallbacks.onStart();
938 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700939 }
940
941 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200943 long startTime = 0;
944 if (DEBUG_RESUME_TIME) {
945 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400946 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200947 }
Adam Cohen9211d422014-10-07 18:14:53 -0700948
949 if (mLauncherCallbacks != null) {
950 mLauncherCallbacks.preOnResume();
951 }
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700954
Winson Chung4a2afa32012-07-19 14:53:05 -0700955 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700956 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700957 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800958 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700959 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700960 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700961 // view after launching an app, as they may be depending on the UI to be static to
962 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700963 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -0700964 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -0800965 } else if (mOnResumeState == State.WIDGETS) {
966 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700967 }
968 mOnResumeState = State.NONE;
969
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700970 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700971 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
972 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700973
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800974 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700976 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700977 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700979 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700981 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200982 // We might have postponed some bind calls until onResume (see waitUntilResume) --
983 // execute them here
984 long startTimeCallbacks = 0;
985 if (DEBUG_RESUME_TIME) {
986 startTimeCallbacks = System.currentTimeMillis();
987 }
988
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
990 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200991 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700992 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200993 if (DEBUG_RESUME_TIME) {
994 Log.d(TAG, "Time spent processing callbacks in onResume: " +
995 (System.currentTimeMillis() - startTimeCallbacks));
996 }
Michael Jurka447bf842013-05-15 14:52:15 +0200997 }
Michael Jurka54554252013-08-01 12:52:23 +0200998 if (mOnResumeCallbacks.size() > 0) {
999 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1000 mOnResumeCallbacks.get(i).run();
1001 }
1002 mOnResumeCallbacks.clear();
1003 }
Winson Chunge4e50662012-01-23 14:45:13 -08001004
1005 // Reset the pressed state of icons that were locked in the press state while activities
1006 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001007 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001008 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001009 mWaitingForResume.setStayPressed(false);
1010 }
Winson Chung82963d52013-10-09 11:20:57 -07001011
Adam Cohen06dff352012-06-01 17:17:08 -07001012 // It is possible that widgets can receive updates while launcher is not in the foreground.
1013 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1014 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1015 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001016 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001017 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001018
Michael Jurka447bf842013-05-15 14:52:15 +02001019 if (DEBUG_RESUME_TIME) {
1020 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1021 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001022
1023 if (mWorkspace.getCustomContentCallbacks() != null) {
1024 // If we are resuming and the custom content is the current page, we call onShow().
1025 // It is also poassible that onShow will instead be called slightly after first layout
1026 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1027 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001028 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001029 }
1030 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001031 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001032 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001033
Sunny Goyal756adbc2015-04-16 15:20:51 -07001034 if (!isWorkspaceLoading()) {
1035 // Process any items that were added while Launcher was away.
1036 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1037 }
Adam Cohen9211d422014-10-07 18:14:53 -07001038
1039 if (mLauncherCallbacks != null) {
1040 mLauncherCallbacks.onResume();
1041 }
Adam Cohen06dff352012-06-01 17:17:08 -07001042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001046 // Ensure that items added to Launcher are queued until Launcher returns
1047 InstallShortcutReceiver.enableInstallQueue();
1048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001050 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001051 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001052 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001053
1054 // We call onHide() aggressively. The custom content callbacks should be able to
1055 // debounce excess onHide calls.
1056 if (mWorkspace.getCustomContentCallbacks() != null) {
1057 mWorkspace.getCustomContentCallbacks().onHide();
1058 }
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Adam Cohen9211d422014-10-07 18:14:53 -07001060 if (mLauncherCallbacks != null) {
1061 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001063 }
1064
1065 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001066 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1067 // by a onResume or by scrolling otherwise.
1068 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001069
1070 // Custom content is completely hidden
1071 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001072
1073 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1074 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001075
1076 // Indicates whether the user is allowed to scroll away from the custom content.
1077 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 }
1079
Adam Cohenc2d6e892014-10-16 09:49:24 -07001080 public interface LauncherOverlay {
1081
1082 /**
1083 * Touch interaction leading to overscroll has begun
1084 */
1085 public void onScrollInteractionBegin();
1086
1087 /**
1088 * Touch interaction related to overscroll has ended
1089 */
1090 public void onScrollInteractionEnd();
1091
1092 /**
1093 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1094 * screen (or in the case of RTL, the rightmost screen).
1095 */
1096 public void onScrollChange(int progress, boolean rtl);
1097
1098 /**
1099 * Screen has stopped scrolling
1100 */
1101 public void onScrollSettled();
1102
1103 /**
1104 * This method can be called by the Launcher in order to force the LauncherOverlay
1105 * to exit fully immersive mode.
1106 */
1107 public void forceExitFullImmersion();
1108 }
1109
Jun Mukai8af0cd82015-05-12 12:32:12 -07001110 public interface LauncherSearchCallbacks {
1111 /**
1112 * Called when the search overlay is shown.
1113 */
1114 public void onSearchOverlayOpened();
1115
1116 /**
1117 * Called when the search overlay is dismissed.
1118 */
1119 public void onSearchOverlayClosed();
1120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlayCallbacks {
1123 /**
1124 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1125 * however it doesn't modify any state within the launcher.
1126 */
1127 public boolean canEnterFullImmersion();
1128
1129 /**
1130 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1131 * eg. by occupying the full screen and handling all touch events.
1132 *
1133 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1134 * case, Launcher will modify any necessary state and assumes the overlay is
1135 * handling all interaction. If false, the LauncherOverlay should cancel any
1136 *
1137 */
1138 public boolean enterFullImmersion();
1139
1140 /**
1141 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1142 * full control over UI and state.
1143 */
1144 public void exitFullImmersion();
1145 }
1146
1147 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1148
1149 @Override
1150 public boolean canEnterFullImmersion() {
1151 return mState == State.WORKSPACE;
1152 }
1153
1154 @Override
1155 public boolean enterFullImmersion() {
1156 if (mState == State.WORKSPACE) {
1157 // When fully immersed, disregard any touches which fall through.
1158 mDragLayer.setBlockTouch(true);
1159 return true;
1160 }
1161 return false;
1162 }
1163
1164 @Override
1165 public void exitFullImmersion() {
1166 mDragLayer.setBlockTouch(false);
1167 }
1168 }
1169
Jorim Jaggid017f882014-01-14 17:08:48 -08001170 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001171 if (mLauncherCallbacks != null) {
1172 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001173 } else {
1174 // On devices with a locked orientation, we will at least have the allow rotation
1175 // setting.
1176 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001177 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 }
1179
Adam Cohen9211d422014-10-07 18:14:53 -07001180 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001181 CustomContentCallbacks callbacks, String description) {
1182 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001183 }
1184
Adam Cohenedb40762013-07-18 16:45:45 -07001185 // The custom content needs to offset its content to account for the QSB
1186 public int getTopOffsetForCustomContent() {
1187 return mWorkspace.getPaddingTop();
1188 }
1189
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001190 @Override
1191 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001193 if (mModel.isCurrentCallbacks(this)) {
1194 mModel.stopLoader();
1195 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001196 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1197
Romain Guy13c2e7b2010-03-10 19:45:00 -08001198 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001201 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001202 @Override
1203 public void onWindowFocusChanged(boolean hasFocus) {
1204 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001205 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001206
1207 if (mLauncherCallbacks != null) {
1208 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1209 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 private boolean acceptFilter() {
1213 final InputMethodManager inputManager = (InputMethodManager)
1214 getSystemService(Context.INPUT_METHOD_SERVICE);
1215 return !inputManager.isFullscreenMode();
1216 }
1217
1218 @Override
1219 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001220 final int uniChar = event.getUnicodeChar();
1221 final boolean handled = super.onKeyDown(keyCode, event);
1222 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1223 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1225 keyCode, event);
1226 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001227 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001228 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 // showSearchDialog()
1230 // If there are multiple keystrokes before the search dialog takes focus,
1231 // onSearchRequested() will be called for every keystroke,
1232 // but it is idempotent, so it's fine.
1233 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235 }
1236
Joe Onorato8a9625e2010-01-28 15:55:35 -08001237 // Eat the long press event so the keyboard doesn't come up.
1238 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1239 return true;
1240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 return handled;
1243 }
1244
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 private String getTypedText() {
1246 return mDefaultKeySsb.toString();
1247 }
1248
1249 private void clearTypedText() {
1250 mDefaultKeySsb.clear();
1251 mDefaultKeySsb.clearSpans();
1252 Selection.setSelection(mDefaultKeySsb, 0);
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001256 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1257 * State
1258 */
1259 private static State intToState(int stateOrdinal) {
1260 State state = State.WORKSPACE;
1261 final State[] stateValues = State.values();
1262 for (int i = 0; i < stateValues.length; i++) {
1263 if (stateValues[i].ordinal() == stateOrdinal) {
1264 state = stateValues[i];
1265 break;
1266 }
1267 }
1268 return state;
1269 }
1270
1271 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 * Restores the previous state, if it exists.
1273 *
1274 * @param savedState The previous state.
1275 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001276 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 private void restoreState(Bundle savedState) {
1278 if (savedState == null) {
1279 return;
1280 }
1281
Michael Jurka883f55b2010-10-21 15:47:14 -07001282 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001283 if (state == State.APPS || state == State.WIDGETS) {
1284 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001285 }
1286
Adam Cohen21cd0022013-10-09 18:57:02 -07001287 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1288 PagedView.INVALID_RESTORE_PAGE);
1289 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001290 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
Sunny Goyal756cd262015-08-20 12:33:21 -07001293 ContentValues itemValues = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_ITEM);
1294 if (itemValues != null) {
1295 mPendingAddInfo.readFromValues(itemValues);
Adam Cohenb823ae42015-03-27 18:07:52 -07001296 AppWidgetProviderInfo info = savedState.getParcelable(
1297 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001298 mPendingAddWidgetInfo = info == null ?
1299 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1300
Adam Cohen4637b5a2013-11-04 18:21:24 -08001301 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001302 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 mRestoring = true;
1304 }
1305
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001306 mItemIdToViewId = (HashMap<Integer, Integer>)
1307 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309
1310 /**
1311 * Finds all the views we need and configure them properly.
1312 */
1313 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001314 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001315
Craig Mautner360310b2012-10-26 15:13:08 -07001316 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001317 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001318 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1319 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001320 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001321 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001322
John Spurlock77e1f472013-09-11 10:09:51 -04001323 mLauncherView.setSystemUiVisibility(
1324 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001325 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
Winson Chung4c98d922011-05-31 16:50:48 -07001327 // Setup the drag layer
1328 mDragLayer.setup(this, dragController);
1329
Winson Chung3d503fb2011-07-13 17:25:49 -07001330 // Setup the hotseat
1331 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1332 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001333 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001334 }
1335
Jorim Jaggid017f882014-01-14 17:08:48 -08001336 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001337 mWidgetsButton = findViewById(R.id.widget_button);
1338 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001339 @Override
1340 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001341 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001342 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001343 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 }
1345 });
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001346 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001347
1348 View wallpaperButton = findViewById(R.id.wallpaper_button);
1349 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350 @Override
1351 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001352 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001353 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001354 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001355 }
1356 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001357 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1358
1359 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001360 if (hasSettings()) {
1361 settingsButton.setOnClickListener(new OnClickListener() {
1362 @Override
1363 public void onClick(View arg0) {
1364 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001365 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001366 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001367 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001368 });
1369 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370 } else {
1371 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001372 }
1373
Adam Cohendbdff6b2013-09-18 19:09:15 -07001374 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375
Winson Chung4c98d922011-05-31 16:50:48 -07001376 // Setup the workspace
1377 mWorkspace.setHapticFeedbackEnabled(false);
1378 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001379 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001380 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001381
Winson Chungf0ea4d32011-06-06 14:27:16 -07001382 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001383 mSearchDropTargetBar = (SearchDropTargetBar)
1384 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001385
Winson Chungef7f8742015-06-04 17:18:17 -07001386 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001387 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001388 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001389 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1390 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1391 } else {
1392 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1393 }
Craig Mautner360310b2012-10-26 15:13:08 -07001394
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001396 dragController.setDragScoller(mWorkspace);
1397 dragController.setScrollView(mDragLayer);
1398 dragController.setMoveTarget(mWorkspace);
1399 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001400 if (mSearchDropTargetBar != null) {
1401 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001402 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001403 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001404
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001405 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001406 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001407 mWeightWatcher = new WeightWatcher(this);
1408 mWeightWatcher.setAlpha(0.5f);
1409 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001410 new FrameLayout.LayoutParams(
1411 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001412 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001413 Gravity.BOTTOM)
1414 );
Adam Cohen39a06042013-07-19 14:30:12 -07001415
1416 boolean show = shouldShowWeightWatcher();
1417 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420
1421 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001422 * Sets the all apps button. This method is called from {@link Hotseat}.
1423 */
1424 public void setAllAppsButton(View allAppsButton) {
1425 mAllAppsButton = allAppsButton;
1426 }
1427
1428 public View getAllAppsButton() {
1429 return mAllAppsButton;
1430 }
1431
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001432 public View getWidgetsButton() {
1433 return mWidgetsButton;
1434 }
1435
Anjali Koppal5ad44842014-03-10 20:34:39 -07001436 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 * Creates a view representing a shortcut.
1438 *
1439 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001441 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001442 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
1445 /**
1446 * Creates a view representing a shortcut inflated from the specified resource.
1447 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 * @param parent The group the shortcut belongs to.
1449 * @param info The data structure describing the shortcut.
1450 *
1451 * @return A View inflated from layoutResId.
1452 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001453 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001454 BubbleTextView favorite = (BubbleTextView) getLayoutInflater().inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001455 parent, false);
1456 favorite.applyFromShortcutInfo(info, mIconCache);
1457 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001459 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 return favorite;
1461 }
1462
1463 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 * Add a shortcut to the workspace.
1465 *
1466 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001468 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 int cellY) {
1470 int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001471 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Sunny Goyal5c97f512015-05-19 16:03:28 -07001473 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001474 if (info == null) {
1475 return;
1476 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001477 final View view = createShortcut(info);
1478
Sunny Goyal5c97f512015-05-19 16:03:28 -07001479 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 // First we check if we already know the exact location where we want to add this item.
1481 if (cellX >= 0 && cellY >= 0) {
1482 cellXY[0] = cellX;
1483 cellXY[1] = cellY;
1484 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001485
1486 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001487 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001488 true, null,null)) {
1489 return;
1490 }
1491 DragObject dragObject = new DragObject();
1492 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001493 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1494 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001495 return;
1496 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08001497 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001499 }
1500
1501 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001502 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 return;
1504 }
1505
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001506 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507
1508 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001517 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 */
Adam Cohen091440a2015-03-18 14:16:05 -07001519 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001520 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1521
1522 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001523 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001524 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1525 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Adam Cohen59400422014-03-05 18:07:04 -08001528 if (appWidgetInfo.isCustomWidget) {
1529 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001530 }
1531
Adam Cohen59400422014-03-05 18:07:04 -08001532 LauncherAppWidgetInfo launcherInfo;
1533 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1534 launcherInfo.spanX = info.spanX;
1535 launcherInfo.spanY = info.spanY;
1536 launcherInfo.minSpanX = info.minSpanX;
1537 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001538 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001541 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
1543 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001544 if (hostView == null) {
1545 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001546 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1547 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001548 } else {
1549 // The AppWidgetHostView has already been inflated and instantiated
1550 launcherInfo.hostView = hostView;
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001553 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001554 launcherInfo.notifyWidgetSizeChanged(this);
1555
Adam Cohen59400422014-03-05 18:07:04 -08001556 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1557 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001558
1559 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001561 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Adam Cohended9f8d2010-11-03 13:25:16 -07001564 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1565 @Override
1566 public void onReceive(Context context, Intent intent) {
1567 final String action = intent.getAction();
1568 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1569 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001570 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001571 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001572
Winson Chungc100e8e2011-08-09 16:02:43 -07001573 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001574 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001575 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001576 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001577 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001578 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1580 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001581 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001582 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1583 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001584 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001585 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1586 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1587 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001588 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1590 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 }
1592 }
1593 };
1594
1595 @Override
1596 public void onAttachedToWindow() {
1597 super.onAttachedToWindow();
1598
1599 // Listen for broadcasts related to user-presence
1600 final IntentFilter filter = new IntentFilter();
1601 filter.addAction(Intent.ACTION_SCREEN_OFF);
1602 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001603 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001604 if (ENABLE_DEBUG_INTENTS) {
1605 filter.addAction(DebugIntents.DELETE_DATABASE);
1606 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1607 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001609 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001610 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001611 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 mVisible = true;
1613 }
1614
Adam Cohen0b395352014-06-09 22:54:36 +00001615 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001616 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001617 * This method is a no-op for other platform versions.
1618 */
Sunny Goyald0091012015-01-20 15:55:34 -08001619 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001620 private void setupTransparentSystemBarsForLollipop() {
1621 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001622 Window window = getWindow();
1623 window.getAttributes().systemUiVisibility |=
1624 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1625 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1626 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1627 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1628 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1629 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1630 window.setStatusBarColor(Color.TRANSPARENT);
1631 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001632 }
1633 }
1634
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 @Override
1636 public void onDetachedFromWindow() {
1637 super.onDetachedFromWindow();
1638 mVisible = false;
1639
Adam Cohend113e0c2010-11-11 10:48:05 -08001640 if (mAttached) {
1641 unregisterReceiver(mReceiver);
1642 mAttached = false;
1643 }
Winson Chungb745afb2015-03-02 11:51:23 -08001644 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
1647 public void onWindowVisibilityChanged(int visibility) {
1648 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001649 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001650 // The following code used to be in onResume, but it turns out onResume is called when
1651 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1652 // is a more appropriate event to handle
1653 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001654 if (!mWorkspaceLoading) {
1655 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001656 // We want to let Launcher draw itself at least once before we force it to build
1657 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 // apps is nice and speedy.
1659 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001660 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001662 if (mStarted) return;
1663 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001664 // We delay the layer building a bit in order to give
1665 // other message processing a time to run. In particular
1666 // this avoids a delay in hiding the IME if it was
1667 // currently shown, because doing that may involve
1668 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001669 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 final ViewTreeObserver.OnDrawListener listener = this;
1671 mWorkspace.post(new Runnable() {
1672 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001673 if (mWorkspace != null &&
1674 mWorkspace.getViewTreeObserver() != null) {
1675 mWorkspace.getViewTreeObserver().
1676 removeOnDrawListener(listener);
1677 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 }
1679 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 }
1682 });
1683 }
1684 clearTypedText();
1685 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 }
1687
Adam Cohen091440a2015-03-18 14:16:05 -07001688 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 mHandler.removeMessages(ADVANCE_MSG);
1690 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1691 mHandler.sendMessageDelayed(msg, delay);
1692 mAutoAdvanceSentTime = System.currentTimeMillis();
1693 }
1694
Adam Cohen091440a2015-03-18 14:16:05 -07001695 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1697 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1698 mAutoAdvanceRunning = autoAdvanceRunning;
1699 if (autoAdvanceRunning) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001700 long delay = mAutoAdvanceTimeLeft == -1 ? ADVANCE_INTERVAL : mAutoAdvanceTimeLeft;
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 sendAdvanceMessage(delay);
1702 } else {
1703 if (!mWidgetsToAdvance.isEmpty()) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001704 mAutoAdvanceTimeLeft = Math.max(0, ADVANCE_INTERVAL -
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1706 }
1707 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001708 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001709 }
1710 }
1711 }
1712
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001713 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1714
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001716 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 if (msg.what == ADVANCE_MSG) {
1718 int i = 0;
1719 for (View key: mWidgetsToAdvance.keySet()) {
1720 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
Sunny Goyal756cd262015-08-20 12:33:21 -07001721 final int delay = ADVANCE_STAGGER * i;
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001723 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 public void run() {
1725 ((Advanceable) v).advance();
1726 }
1727 }, delay);
1728 }
1729 i++;
1730 }
Sunny Goyal756cd262015-08-20 12:33:21 -07001731 sendAdvanceMessage(ADVANCE_INTERVAL);
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001733 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001735 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001736
1737 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001738 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1740 if (v instanceof Advanceable) {
1741 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001742 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001743 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
1745 }
1746
1747 void removeWidgetToAutoAdvance(View hostView) {
1748 if (mWidgetsToAdvance.containsKey(hostView)) {
1749 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001750 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001752 }
1753
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 launcherInfo.hostView = null;
1757 }
1758
Hyunyoung Song3f471442015-04-08 19:01:34 -07001759 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001760 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1761 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
1763
Winson Chunga6945242014-01-08 14:04:34 -08001764 public DragLayer getDragLayer() {
1765 return mDragLayer;
1766 }
1767
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001768 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001769 return mAppsView;
1770 }
1771
Hyunyoung Song3f471442015-04-08 19:01:34 -07001772 public WidgetsContainerView getWidgetsView() {
1773 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001774 }
1775
Winson Chunga6945242014-01-08 14:04:34 -08001776 public Workspace getWorkspace() {
1777 return mWorkspace;
1778 }
1779
1780 public Hotseat getHotseat() {
1781 return mHotseat;
1782 }
1783
Jorim Jaggid017f882014-01-14 17:08:48 -08001784 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001785 return mOverviewPanel;
1786 }
1787
Winson Chung006ee262015-08-03 14:40:11 -07001788 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001789 return mSearchDropTargetBar;
1790 }
1791
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001792 public LauncherAppWidgetHost getAppWidgetHost() {
1793 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
Romain Guycbb89e42009-06-08 15:52:54 -07001795
Winson Chunga9abd0e2010-10-27 17:18:37 -07001796 public LauncherModel getModel() {
1797 return mModel;
1798 }
1799
Winson Chunga6945242014-01-08 14:04:34 -08001800 protected SharedPreferences getSharedPrefs() {
1801 return mSharedPrefs;
1802 }
1803
Adam Cohen2e6da152015-05-06 11:42:25 -07001804 public DeviceProfile getDeviceProfile() {
1805 return mDeviceProfile;
1806 }
1807
Bjorn Bringertc459e522013-06-07 19:36:01 +01001808 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001809 getWindow().closeAllPanels();
1810
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001811 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001812 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001813 }
1814
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 @Override
1816 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001817 long startTime = 0;
1818 if (DEBUG_RESUME_TIME) {
1819 startTime = System.currentTimeMillis();
1820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 super.onNewIntent(intent);
1822
1823 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001824 Folder openFolder = mWorkspace.getOpenFolder();
1825 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1826 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1827 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1828 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1829 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001830 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001831 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001832
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 if (mWorkspace == null) {
1834 // Can be cases where mWorkspace is null, this prevents a NPE
1835 return;
1836 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001837 // In all these cases, only animate if we're already on home
1838 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001839
Adam Cohen6fecd412013-10-02 17:41:50 -07001840 closeFolder();
1841 exitSpringLoadedDragMode();
1842
1843 // If we are already on home, then just animate back to the workspace,
1844 // otherwise, just wait until onResume to set the state back to Workspace
1845 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001846 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001847 } else {
1848 mOnResumeState = State.WORKSPACE;
1849 }
1850
1851 final View v = getWindow().peekDecorView();
1852 if (v != null && v.getWindowToken() != null) {
1853 InputMethodManager imm = (InputMethodManager)getSystemService(
1854 INPUT_METHOD_SERVICE);
1855 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1856 }
1857
Winson Chungb745afb2015-03-02 11:51:23 -08001858 // Reset the apps view
1859 if (!alreadyOnHome && mAppsView != null) {
1860 mAppsView.scrollToTop();
1861 }
1862
Hyunyoung Song3f471442015-04-08 19:01:34 -07001863 // Reset the widgets view
1864 if (!alreadyOnHome && mWidgetsView != null) {
1865 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001866 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001867
Adam Cohen9211d422014-10-07 18:14:53 -07001868 if (mLauncherCallbacks != null) {
1869 mLauncherCallbacks.onHomeIntent();
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Adam Cohened307df2013-10-02 09:37:31 -07001872
Adam Cohen9211d422014-10-07 18:14:53 -07001873 if (mLauncherCallbacks != null) {
1874 mLauncherCallbacks.onNewIntent(intent);
1875 }
Winson15f8b172015-08-19 11:02:39 -07001876
1877 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1878 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1879 // animation.
1880 if (isActionMain) {
1881 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1882 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
1883 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1884 openFolder == null && moveToDefaultScreen) {
1885 mWorkspace.post(new Runnable() {
1886 @Override
1887 public void run() {
1888 mWorkspace.moveToDefaultScreen(true);
1889 }
1890 });
1891 }
1892 }
1893
1894 if (DEBUG_RESUME_TIME) {
1895 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1896 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001900 public void onRestoreInstanceState(Bundle state) {
1901 super.onRestoreInstanceState(state);
1902 for (int page: mSynchronouslyBoundPages) {
1903 mWorkspace.restoreInstanceStateForChild(page);
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
1906
1907 @Override
1908 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001909 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001910 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1911 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001912 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001913 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914
Michael Jurka883f55b2010-10-21 15:47:14 -07001915 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001916 // We close any open folder since it will not be re-opened, and we need to make sure
1917 // this state is reflected.
1918 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mWaitingForResult) {
Sunny Goyal756cd262015-08-20 12:33:21 -07001922 ContentValues itemValues = new ContentValues();
1923 mPendingAddInfo.writeToValues(itemValues);
1924 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_ITEM, itemValues);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001925 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001926 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
1928
Hyunyoung Song3f471442015-04-08 19:01:34 -07001929 // Save the current widgets tray?
1930 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001931 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001932
1933 if (mLauncherCallbacks != null) {
1934 mLauncherCallbacks.onSaveInstanceState(outState);
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
1938 @Override
1939 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001941
Winson Chunge7a03942011-08-05 15:05:12 -07001942 // Remove all pending runnables
1943 mHandler.removeMessages(ADVANCE_MSG);
1944 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001945 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001946
Winson Chungcd2b0142011-06-08 16:02:26 -07001947 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001948 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001949
1950 // It's possible to receive onDestroy after a new Launcher activity has
1951 // been created. In this case, don't interfere with the new Launcher.
1952 if (mModel.isCurrentCallbacks(this)) {
1953 mModel.stopLoader();
1954 app.setLauncher(null);
1955 }
Winson Chungcd2b0142011-06-08 16:02:26 -07001956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001958 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001960 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001962 mAppWidgetHost = null;
1963
1964 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965
1966 TextKeyListener.getInstance().release();
1967
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001968 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001969
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001970 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001971 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001972 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001973 mWorkspace = null;
1974 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001975
Michael Jurka2ecf9952012-06-18 12:52:28 -07001976 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07001977
1978 if (mLauncherCallbacks != null) {
1979 mLauncherCallbacks.onDestroy();
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
Adam Cohena9cf38f2011-05-02 15:36:58 -07001983 public DragController getDragController() {
1984 return mDragController;
1985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 @Override
1988 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07001989 onStartForResult(requestCode);
1990 super.startActivityForResult(intent, requestCode);
1991 }
1992
1993 @Override
1994 public void startIntentSenderForResult (IntentSender intent, int requestCode,
1995 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
1996 onStartForResult(requestCode);
1997 try {
1998 super.startIntentSenderForResult(intent, requestCode,
1999 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2000 } catch (IntentSender.SendIntentException e) {
2001 throw new ActivityNotFoundException();
2002 }
2003 }
2004
2005 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002006 if (requestCode >= 0) {
2007 setWaitingForResult(true);
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Winson Chung70d72102011-08-12 11:24:35 -07002011 /**
2012 * Indicates that we want global search for this activity by setting the globalSearch
2013 * argument for {@link #startSearch} to true.
2014 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002016 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002018
Karl Rosaen138a0412009-04-23 19:00:21 -07002019 if (initialQuery == null) {
2020 // Use any text typed in the launcher as the initial query
2021 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 if (appSearchData == null) {
2024 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002025 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
Winson Chungadf0c182012-08-23 14:59:07 -07002027 Rect sourceBounds = new Rect();
2028 if (mSearchDropTargetBar != null) {
2029 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2030 }
Romain Guycbb89e42009-06-08 15:52:54 -07002031
Ian Parkinson047036e2014-09-01 15:40:53 +01002032 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002033 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002034 if (clearTextImmediately) {
2035 clearTypedText();
2036 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002037
2038 // We need to show the workspace after starting the search
2039 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002040 }
2041
Ian Parkinson047036e2014-09-01 15:40:53 +01002042 /**
2043 * Start a text search.
2044 *
2045 * @return {@code true} if the search will start immediately, so any further keypresses
2046 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2047 * to buffer keypresses.
2048 */
2049 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002050 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002051 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2052 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2053 sourceBounds);
2054 }
2055
Michael Jurkaa33411c2012-06-14 16:18:21 -07002056 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002057 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002058 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002059 }
2060
2061 /**
2062 * Starts the global search activity. This code is a copied from SearchManager
2063 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002064 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002065 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002066 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002067 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2068 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2069 if (globalSearchActivity == null) {
2070 Log.w(TAG, "No global search activity found.");
2071 return;
2072 }
2073 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2074 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2075 intent.setComponent(globalSearchActivity);
2076 // Make sure that we have a Bundle to put source in
2077 if (appSearchData == null) {
2078 appSearchData = new Bundle();
2079 } else {
2080 appSearchData = new Bundle(appSearchData);
2081 }
Adam Cohen9211d422014-10-07 18:14:53 -07002082 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002083 if (!appSearchData.containsKey("source")) {
2084 appSearchData.putString("source", getPackageName());
2085 }
2086 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2087 if (!TextUtils.isEmpty(initialQuery)) {
2088 intent.putExtra(SearchManager.QUERY, initialQuery);
2089 }
2090 if (selectInitialQuery) {
2091 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2092 }
2093 intent.setSourceBounds(sourceBounds);
2094 try {
2095 startActivity(intent);
2096 } catch (ActivityNotFoundException ex) {
2097 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100
Winson Chungbedf9232015-07-10 12:38:30 -07002101 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2102 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2103 return;
2104 }
2105
2106 // If not handled, then just start the provided search intent
2107 startActivitySafely(v, searchIntent, null);
2108 }
2109
Yura4f93ec62014-02-04 14:15:21 +00002110 public boolean isOnCustomContent() {
2111 return mWorkspace.isOnOrMovingToCustomContent();
2112 }
2113
Winson Chung70d72102011-08-12 11:24:35 -07002114 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002115 public boolean onPrepareOptionsMenu(Menu menu) {
2116 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002117 if (!isOnCustomContent()) {
2118 // Close any open folders
2119 closeFolder();
2120 // Stop resizing any widgets
2121 mWorkspace.exitWidgetResizeMode();
2122 if (!mWorkspace.isInOverviewMode()) {
2123 // Show the overview mode
2124 showOverviewMode(true);
2125 } else {
2126 showWorkspace(true);
2127 }
Winson Chunge0298742014-01-17 12:03:00 -08002128 }
Adam Cohen9211d422014-10-07 18:14:53 -07002129 if (mLauncherCallbacks != null) {
2130 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2131 }
2132
Michael Jurkab94f3f82013-09-11 18:08:54 +02002133 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002136 @Override
2137 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002138 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002139 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002140 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002141 }
2142
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 public boolean isWorkspaceLocked() {
2144 return mWorkspaceLoading || mWaitingForResult;
2145 }
2146
Adam Cohen517a7f52014-03-01 12:12:59 -08002147 public boolean isWorkspaceLoading() {
2148 return mWorkspaceLoading;
2149 }
2150
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002151 private void setWorkspaceLoading(boolean value) {
2152 boolean isLocked = isWorkspaceLocked();
2153 mWorkspaceLoading = value;
2154 if (isLocked != isWorkspaceLocked()) {
2155 onWorkspaceLockedChanged();
2156 }
2157 }
2158
2159 private void setWaitingForResult(boolean value) {
2160 boolean isLocked = isWorkspaceLocked();
2161 mWaitingForResult = value;
2162 if (isLocked != isWorkspaceLocked()) {
2163 onWorkspaceLockedChanged();
2164 }
2165 }
2166
Adam Cohen9211d422014-10-07 18:14:53 -07002167 protected void onWorkspaceLockedChanged() {
2168 if (mLauncherCallbacks != null) {
2169 mLauncherCallbacks.onWorkspaceLockedChanged();
2170 }
2171 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002172
Michael Jurka0280c3b2010-09-17 15:00:07 -07002173 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002174 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002175 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002176 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2177 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002178 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002179 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002180
Sunny Goyale6b63a32015-01-16 16:14:29 -08002181 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002182 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002183 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2184 }
2185
Sunny Goyale6b63a32015-01-16 16:14:29 -08002186 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002187 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2188 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002189 if (appWidgetInfo.configure != null) {
2190 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002191 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002192
Bjorn Bringert7984c942009-12-09 15:38:25 +00002193 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002194 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2195 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002198 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002199 Runnable onComplete = new Runnable() {
2200 @Override
2201 public void run() {
2202 // Exit spring loaded mode if necessary after adding the widget
2203 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2204 null);
2205 }
2206 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002207 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002208 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002209 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211 }
Romain Guycbb89e42009-06-08 15:52:54 -07002212
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002213 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002214 // Close any folders that may be open.
2215 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002216 mWorkspace.moveToCustomContentScreen(animate);
2217 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002218
2219 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2220 int[] cell, int spanX, int spanY) {
2221 switch (info.itemType) {
2222 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2223 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2224 int span[] = new int[2];
2225 span[0] = spanX;
2226 span[1] = spanY;
2227 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2228 container, screenId, cell, span);
2229 break;
2230 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2231 processShortcutFromDrop(info.componentName, container, screenId, cell);
2232 break;
2233 default:
2234 throw new IllegalStateException("Unknown item type: " + info.itemType);
2235 }
2236 }
2237
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 /**
2239 * Process a shortcut drop.
2240 *
2241 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002242 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002245 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2246 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 mPendingAddInfo.screenId = screenId;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250
2251 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.cellX = cell[0];
2253 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255
2256 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2257 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002258 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259 }
2260
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 /**
2262 * Process a widget drop.
2263 *
2264 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002268 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2269 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 mPendingAddInfo.screenId = info.screenId = screenId;
Adam Cohend41fbf52012-02-16 23:53:59 -08002273 mPendingAddInfo.minSpanX = info.minSpanX;
2274 mPendingAddInfo.minSpanY = info.minSpanY;
2275
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.cellX = cell[0];
2278 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002280 if (span != null) {
2281 mPendingAddInfo.spanX = span[0];
2282 mPendingAddInfo.spanY = span[1];
2283 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284
Adam Cohened66b2b2012-01-23 17:28:51 -08002285 AppWidgetHostView hostView = info.boundWidget;
2286 int appWidgetId;
2287 if (hostView != null) {
2288 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002289 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002290
2291 // Clear the boundWidget so that it doesn't get destroyed.
2292 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002294 // In this case, we either need to start an activity to get permission to bind
2295 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002297 Bundle options = info.bindOptions;
2298
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2300 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002302 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002304 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002305 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002308 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2309 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002310 // TODO: we need to make sure that this accounts for the options bundle.
2311 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002312 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2313 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002314 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 }
2316
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002319 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 folderInfo.title = getText(R.string.folder_name);
2321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002323 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2324 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002325 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326
2327 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 FolderIcon newFolder =
2329 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002332 // Force measure the new folder icon
2333 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2334 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002335 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
Romain Guycbb89e42009-06-08 15:52:54 -07002337
Joe Onorato9c1289c2009-08-17 11:03:03 -04002338 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002339 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002340 }
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 @Override
2343 public boolean dispatchKeyEvent(KeyEvent event) {
2344 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2345 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002347 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002348 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002349 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002350 dumpState();
2351 return true;
2352 }
2353 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002354 }
2355 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2356 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002357 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 return true;
2359 }
2360 }
2361
2362 return super.dispatchKeyEvent(event);
2363 }
2364
Joe Onorato88ec0992009-11-19 13:16:06 -08002365 @Override
2366 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002367 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2368 return;
2369 }
2370
Sunny Goyal45478022015-06-08 16:52:41 -07002371 if (mDragController.isDragging()) {
2372 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002373 return;
2374 }
2375
Winson Chungb745afb2015-03-02 11:51:23 -08002376 if (isAppsViewVisible()) {
2377 showWorkspace(true);
2378 } else if (isWidgetsViewVisible()) {
2379 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002380 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002381 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002382 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002383 Folder openFolder = mWorkspace.getOpenFolder();
2384 if (openFolder.isEditingName()) {
2385 openFolder.dismissEditingName();
2386 } else {
2387 closeFolder();
2388 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002389 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002390 mWorkspace.exitWidgetResizeMode();
2391
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002392 // Back button is a no-op here, but give at least some feedback for the button press
2393 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002394 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002395 }
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002398 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002399 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002400 @Override
2401 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002402 if (mAppWidgetHost != null) {
2403 mAppWidgetHost.startListening();
2404 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002405 }
2406
2407 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 * Launches the intent referred by the clicked shortcut.
2409 *
2410 * @param v The view representing the clicked shortcut.
2411 */
2412 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002413 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2414 // view has detached (it's possible for this to happen if the view is removed mid touch).
2415 if (v.getWindowToken() == null) {
2416 return;
2417 }
2418
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002419 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002420 return;
2421 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002422
Adam Cohen1697b792013-09-17 19:08:21 -07002423 if (v instanceof Workspace) {
2424 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002425 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002426 }
2427 return;
2428 }
2429
2430 if (v instanceof CellLayout) {
2431 if (mWorkspace.isInOverviewMode()) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002432 mWorkspace.snapToPageFromOverView(mWorkspace.indexOfChild(v));
2433 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002434 }
Sunny Goyalce5a7e52015-07-08 15:44:27 -07002435 return;
Adam Cohenf358a4b2013-07-23 16:47:31 -07002436 }
2437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002439 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002440 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002442 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002443 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002444 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002445 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002446 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002447 } else if (tag instanceof AppInfo) {
2448 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002449 } else if (tag instanceof LauncherAppWidgetInfo) {
2450 if (v instanceof PendingAppWidgetHostView) {
2451 onClickPendingWidget((PendingAppWidgetHostView) v);
2452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 }
2454 }
2455
Sunny Goyal70660032015-05-14 00:07:08 -07002456 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002457 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002458 return false;
2459 }
2460
Michael Jurkaaf442092010-06-10 17:01:57 -07002461 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002462 * Event handler for the app widget view which has not fully restored.
2463 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002464 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002465 if (mIsSafeModeEnabled) {
2466 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2467 return;
2468 }
2469
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002470 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002471 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002472 int widgetId = info.appWidgetId;
2473 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2474 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002475 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2476 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002477 mPendingAddInfo.copyFrom(info);
2478 mPendingAddWidgetId = widgetId;
2479
Sunny Goyalffe83f12014-08-14 17:39:34 -07002480 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2481 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002482 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002483 } else if (info.installProgress < 0) {
2484 // The install has not been queued
2485 final String packageName = info.providerName.getPackageName();
2486 showBrokenAppInstallDialog(packageName,
2487 new DialogInterface.OnClickListener() {
2488 public void onClick(DialogInterface dialog, int id) {
2489 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2490 }
2491 });
2492 } else {
2493 // Download has started.
2494 final String packageName = info.providerName.getPackageName();
2495 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002496 }
2497 }
2498
2499 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002500 * Event handler for the "grid" button that appears on the home screen, which
2501 * enters all apps mode.
2502 *
2503 * @param v The view that was clicked.
2504 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 protected void onClickAllAppsButton(View v) {
2506 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002507 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002508 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002509 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002510
2511 if (mLauncherCallbacks != null) {
2512 mLauncherCallbacks.onClickAllAppsButton(v);
2513 }
Winson Chung76648c52015-07-10 14:33:23 -07002514 }
2515 }
2516
2517 protected void onLongClickAllAppsButton(View v) {
2518 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2519 if (!isAppsViewVisible()) {
2520 showAppsView(true /* animated */, false /* resetListToTop */,
2521 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 }
2523 }
2524
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002525 private void showBrokenAppInstallDialog(final String packageName,
2526 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002527 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002528 .setTitle(R.string.abandoned_promises_title)
2529 .setMessage(R.string.abandoned_promise_explanation)
2530 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2531 .setNeutralButton(R.string.abandoned_clean_this,
2532 new DialogInterface.OnClickListener() {
2533 public void onClick(DialogInterface dialog, int id) {
2534 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2535 mWorkspace.removeAbandonedPromise(packageName, user);
2536 }
2537 })
2538 .create().show();
2539 return;
2540 }
2541
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002542 /**
2543 * Event handler for an app shortcut click.
2544 *
2545 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2546 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002547 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2549 Object tag = v.getTag();
2550 if (!(tag instanceof ShortcutInfo)) {
2551 throw new IllegalArgumentException("Input must be a Shortcut");
2552 }
2553
2554 // Open shortcut
2555 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002556
2557 if (shortcut.isDisabled != 0) {
2558 int error = R.string.activity_not_available;
2559 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2560 error = R.string.safemode_shortcut_error;
2561 }
2562 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2563 return;
2564 }
2565
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002566 final Intent intent = shortcut.intent;
2567
2568 // Check for special shortcuts
2569 if (intent.getComponent() != null) {
2570 final String shortcutClass = intent.getComponent().getClassName();
2571
2572 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2573 MemoryDumpActivity.startDump(this);
2574 return;
2575 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2576 toggleShowWeightWatcher();
2577 return;
2578 }
2579 }
2580
Chris Wren40c5ed32014-06-24 18:24:23 -04002581 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002582 if ((v instanceof BubbleTextView)
2583 && shortcut.isPromise()
2584 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002585 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002586 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002587 new DialogInterface.OnClickListener() {
2588 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002589 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002590 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002591 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002592 return;
2593 }
2594
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002596 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002597
2598 if (mLauncherCallbacks != null) {
2599 mLauncherCallbacks.onClickAppShortcut(v);
2600 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002601 }
2602
Adam Cohen091440a2015-03-18 14:16:05 -07002603 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002604 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002605 final ShortcutInfo shortcut;
2606 final Intent intent;
2607 if (tag instanceof ShortcutInfo) {
2608 shortcut = (ShortcutInfo) tag;
2609 intent = shortcut.intent;
2610 int[] pos = new int[2];
2611 v.getLocationOnScreen(pos);
2612 intent.setSourceBounds(new Rect(pos[0], pos[1],
2613 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002614
Sunny Goyal508da152014-08-14 10:53:27 -07002615 } else if (tag instanceof AppInfo) {
2616 shortcut = null;
2617 intent = ((AppInfo) tag).intent;
2618 } else {
2619 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2620 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621
2622 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002623 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624
2625 if (success && v instanceof BubbleTextView) {
2626 mWaitingForResume = (BubbleTextView) v;
2627 mWaitingForResume.setStayPressed(true);
2628 }
2629 }
2630
2631 /**
2632 * Event handler for a folder icon click.
2633 *
2634 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2635 */
2636 protected void onClickFolderIcon(View v) {
2637 if (LOGD) Log.d(TAG, "onClickFolder");
2638 if (!(v instanceof FolderIcon)){
2639 throw new IllegalArgumentException("Input must be a FolderIcon");
2640 }
2641
Sunny Goyal317698b2015-07-29 11:45:41 -07002642 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 FolderIcon folderIcon = (FolderIcon) v;
2644 final FolderInfo info = folderIcon.getFolderInfo();
2645 Folder openFolder = mWorkspace.getFolderForTag(info);
2646
2647 // If the folder info reports that the associated folder is open, then verify that
2648 // it is actually opened. There have been a few instances where this gets out of sync.
2649 if (info.opened && openFolder == null) {
2650 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2651 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2652 info.opened = false;
2653 }
2654
2655 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2656 // Close any open folder
2657 closeFolder();
2658 // Open the requested folder
2659 openFolder(folderIcon);
2660 } else {
2661 // Find the open folder...
2662 int folderScreen;
2663 if (openFolder != null) {
2664 folderScreen = mWorkspace.getPageForView(openFolder);
2665 // .. and close it
2666 closeFolder(openFolder);
2667 if (folderScreen != mWorkspace.getCurrentPage()) {
2668 // Close any folder open on the current screen
2669 closeFolder();
2670 // Pull the folder onto this screen
2671 openFolder(folderIcon);
2672 }
2673 }
2674 }
Adam Cohen9211d422014-10-07 18:14:53 -07002675
2676 if (mLauncherCallbacks != null) {
2677 mLauncherCallbacks.onClickFolderIcon(v);
2678 }
Michael Jurka5130e402011-10-13 04:55:35 -07002679 }
2680
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002681 /**
2682 * Event handler for the (Add) Widgets button that appears after a long press
2683 * on the home screen.
2684 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002685 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002687 if (mIsSafeModeEnabled) {
2688 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2689 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002690 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002691 if (mLauncherCallbacks != null) {
2692 mLauncherCallbacks.onClickAddWidgetButton(view);
2693 }
Adam Cohen9211d422014-10-07 18:14:53 -07002694 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002695 }
2696
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 /**
2698 * Event handler for the wallpaper picker button that appears after a long press
2699 * on the home screen.
2700 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002701 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002702 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002703 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2704 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002705
2706 if (mLauncherCallbacks != null) {
2707 mLauncherCallbacks.onClickWallpaperPicker(v);
2708 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 }
2710
2711 /**
2712 * Event handler for a click on the settings button that appears after a long press
2713 * on the home screen.
2714 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002715 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002717 if (mLauncherCallbacks != null) {
2718 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002719 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002720 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002721 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722 }
2723
Adam Cohen61f560d2013-09-30 15:58:20 -07002724 public View.OnTouchListener getHapticFeedbackTouchListener() {
2725 if (mHapticFeedbackTouchListener == null) {
2726 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002727 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002728 @Override
2729 public boolean onTouch(View v, MotionEvent event) {
2730 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2731 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2732 }
2733 return false;
2734 }
2735 };
2736 }
2737 return mHapticFeedbackTouchListener;
2738 }
2739
Adam Cohen9211d422014-10-07 18:14:53 -07002740 public void onDragStarted(View view) {
2741 if (isOnCustomContent()) {
2742 // Custom content screen doesn't participate in drag and drop. If on custom
2743 // content screen, move to default.
2744 moveWorkspaceToDefaultScreen();
2745 }
2746
2747 if (mLauncherCallbacks != null) {
2748 mLauncherCallbacks.onDragStarted(view);
2749 }
2750 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002751
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002752 /**
2753 * Called when the user stops interacting with the launcher.
2754 * This implies that the user is now on the homescreen and is not doing housekeeping.
2755 */
Adam Cohen9211d422014-10-07 18:14:53 -07002756 protected void onInteractionEnd() {
2757 if (mLauncherCallbacks != null) {
2758 mLauncherCallbacks.onInteractionEnd();
2759 }
2760 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002761
2762 /**
2763 * Called when the user starts interacting with the launcher.
2764 * The possible interactions are:
2765 * - open all apps
2766 * - reorder an app shortcut, or a widget
2767 * - open the overview mode.
2768 * This is a good time to stop doing things that only make sense
2769 * when the user is on the homescreen and not doing housekeeping.
2770 */
Adam Cohen9211d422014-10-07 18:14:53 -07002771 protected void onInteractionBegin() {
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onInteractionBegin();
2774 }
2775 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002776
Winson Chungcd99cd32015-04-29 11:03:24 -07002777 /** Updates the interaction state. */
2778 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002779 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002780 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002781 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2782 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002783 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002784 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002785 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002786 onInteractionEnd();
2787 }
2788 }
2789
Kenny Guyf07af7b2014-07-31 11:39:16 +01002790 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002791 try {
2792 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002793 launcherApps.showAppDetailsForProfile(componentName, user);
2794 } catch (SecurityException e) {
2795 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2796 Log.e(TAG, "Launcher does not have permission to launch settings");
2797 } catch (ActivityNotFoundException e) {
2798 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2799 Log.e(TAG, "Unable to launch settings");
2800 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002801 }
2802
Michael Jurka1e2f4652013-07-08 18:03:46 -07002803 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002804 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2805 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002806 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002807 // System applications cannot be installed. For now, show a toast explaining that.
2808 // We may give them the option of disabling apps this way.
2809 int messageId = R.string.uninstall_system_app_text;
2810 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002811 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002812 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002813 String packageName = componentName.getPackageName();
2814 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002815 Intent intent = new Intent(
2816 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002817 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2818 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002819 if (user != null) {
2820 user.addToIntent(intent, Intent.EXTRA_USER);
2821 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002822 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002823 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002824 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002825 }
2826
Winson Chung8f1eff72015-05-28 17:33:40 -07002827 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002828 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002830 // Only launch using the new animation if the shortcut has not opted out (this is a
2831 // private contract between launcher and may be ignored in the future).
2832 boolean useLaunchAnimation = (v != null) &&
2833 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002834 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2835 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002836
Kenny Guy1317e2d2014-05-08 18:52:50 +01002837 UserHandleCompat user = null;
2838 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2839 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2840 user = userManager.getUserForSerialNumber(serialNumber);
2841 }
2842
2843 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002844 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002845 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002846 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002847 int left = 0, top = 0;
2848 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2849 if (v instanceof TextView) {
2850 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002851 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2852 if (icon != null) {
2853 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002854 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002855 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002856 width = bounds.width();
2857 height = bounds.height();
2858 }
2859 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002860 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2861 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002862 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002863 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002864 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002865 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002866 // On L devices, we use the device default slide-up transition.
2867 // On L MR1 devices, we a custom version of the slide-up transition which
2868 // doesn't have the delay present in the device default.
2869 opts = ActivityOptions.makeCustomAnimation(this,
2870 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002871 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002872 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002873 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002874
2875 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2876 // Could be launching some bookkeeping activity
2877 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002878 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002879 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002880 launcherApps.startActivityForProfile(intent.getComponent(), user,
2881 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002882 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002883 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 } catch (SecurityException e) {
2885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002886 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002888 "or use the exported attribute for this activity. "
2889 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002891 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002893
Winson Chungbedf9232015-07-10 12:38:30 -07002894 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002895 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002896 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2897 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2898 return false;
2899 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002900 try {
2901 success = startActivity(v, intent, tag);
2902 } catch (ActivityNotFoundException e) {
2903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2904 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2905 }
2906 return success;
2907 }
2908
Adam Cohen268c4752012-06-06 17:47:33 -07002909 /**
2910 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2911 * in the DragLayer in the exact absolute location of the original FolderIcon.
2912 */
2913 private void copyFolderIconToImage(FolderIcon fi) {
2914 final int width = fi.getMeasuredWidth();
2915 final int height = fi.getMeasuredHeight();
2916
2917 // Lazy load ImageView, Bitmap and Canvas
2918 if (mFolderIconImageView == null) {
2919 mFolderIconImageView = new ImageView(this);
2920 }
2921 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2922 mFolderIconBitmap.getHeight() != height) {
2923 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2924 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2925 }
2926
2927 DragLayer.LayoutParams lp;
2928 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2929 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2930 } else {
2931 lp = new DragLayer.LayoutParams(width, height);
2932 }
2933
Adam Cohen307fe232012-08-16 17:55:58 -07002934 // The layout from which the folder is being opened may be scaled, adjust the starting
2935 // view size by this scale factor.
2936 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002937 lp.customPosition = true;
2938 lp.x = mRectForFolderAnimation.left;
2939 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002940 lp.width = (int) (scale * width);
2941 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002942
2943 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2944 fi.draw(mFolderIconCanvas);
2945 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002946 if (fi.getFolder() != null) {
2947 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2948 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002949 }
Adam Cohen268c4752012-06-06 17:47:33 -07002950 // Just in case this image view is still in the drag layer from a previous animation,
2951 // we remove it and re-add it.
2952 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2953 mDragLayer.removeView(mFolderIconImageView);
2954 }
2955 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002956 if (fi.getFolder() != null) {
2957 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002958 }
Adam Cohen268c4752012-06-06 17:47:33 -07002959 }
2960
Adam Cohen2801caf2011-05-13 20:57:39 -07002961 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002962 if (fi == null) return;
Adam Cohenc51934b2011-07-26 21:07:43 -07002963 FolderInfo info = (FolderInfo) fi.getTag();
2964 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2965 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002966 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2967 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002968 }
2969
Adam Cohen268c4752012-06-06 17:47:33 -07002970 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2971 copyFolderIconToImage(fi);
2972 fi.setVisibility(View.INVISIBLE);
2973
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002974 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(
2975 mFolderIconImageView, 0, 1.5f, 1.5f);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002976 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002977 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2978 }
2979 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002980 oa.start();
2981 }
2982
Adam Cohen268c4752012-06-06 17:47:33 -07002983 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002984 if (fi == null) return;
Adam Cohen268c4752012-06-06 17:47:33 -07002985 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002986
Adam Cohen268c4752012-06-06 17:47:33 -07002987 // We remove and re-draw the FolderIcon in-case it has changed
2988 mDragLayer.removeView(mFolderIconImageView);
2989 copyFolderIconToImage(fi);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002990 ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(mFolderIconImageView, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002991 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002992 oa.addListener(new AnimatorListenerAdapter() {
2993 @Override
2994 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002995 if (cl != null) {
2996 cl.clearFolderLeaveBehind();
2997 // Remove the ImageView copy of the FolderIcon and make the original visible.
2998 mDragLayer.removeView(mFolderIconImageView);
2999 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 }
3001 }
3002 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003003 oa.start();
3004 }
3005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003007 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008 * is animated relative to the specified View. If the View is null, no animation
3009 * is played.
3010 *
3011 * @param folderInfo The FolderInfo describing the folder to open.
3012 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003013 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003014 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003015 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3016 if (openFolder != null && openFolder != folder) {
3017 // Close any open folder before opening a folder.
3018 closeFolder();
3019 }
3020
Adam Cohena9cf38f2011-05-02 15:36:58 -07003021 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022
Adam Cohena9cf38f2011-05-02 15:36:58 -07003023 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024
Sunny Goyalf4066152015-04-15 09:42:19 -07003025 // While the folder is open, the position of the icon cannot change.
3026 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3027
Adam Cohen4554ee12011-08-03 16:13:21 -07003028 // Just verify that the folder hasn't already been added to the DragLayer.
3029 // There was a one-off crash where the folder had a parent already.
3030 if (folder.getParent() == null) {
3031 mDragLayer.addView(folder);
3032 mDragController.addDropTarget((DropTarget) folder);
3033 } else {
3034 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3035 folder.getParent() + ").");
3036 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003037 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003038 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003039
3040 // Notify the accessibility manager that this folder "window" has appeared and occluded
3041 // the workspace items
3042 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3043 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003044 }
3045
3046 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003047 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003048 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003049 if (folder.isEditingName()) {
3050 folder.dismissEditingName();
3051 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003052 closeFolder(folder);
3053 }
3054 }
3055
Sunny Goyal83a8f042015-05-19 12:52:12 -07003056 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003057 folder.getInfo().opened = false;
3058
3059 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3060 if (parent != null) {
3061 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3062 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003063 if (fi != null) {
3064 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3065 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003066 }
3067 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003068
3069 // Notify the accessibility manager that this folder "window" has disappeard and no
3070 // longer occludeds the workspace items
3071 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 }
3073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003075 if (!isDraggingEnabled()) return false;
3076 if (isWorkspaceLocked()) return false;
3077 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078
Winson Chung76648c52015-07-10 14:33:23 -07003079 if (v == mAllAppsButton) {
3080 onLongClickAllAppsButton(v);
3081 return true;
3082 }
3083
Adam Cohen1697b792013-09-17 19:08:21 -07003084 if (v instanceof Workspace) {
3085 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003086 if (!mWorkspace.isTouchActive()) {
3087 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003088 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3089 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3090 return true;
3091 } else {
3092 return false;
3093 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003094 } else {
3095 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003096 }
Adam Cohen1697b792013-09-17 19:08:21 -07003097 }
3098
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003099 CellLayout.CellInfo longClickCellInfo = null;
3100 View itemUnderLongClick = null;
3101 if (v.getTag() instanceof ItemInfo) {
3102 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003103 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003104 itemUnderLongClick = longClickCellInfo.cell;
3105 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
3107
Winson Chung3d503fb2011-07-13 17:25:49 -07003108 // The hotseat touch handling does not go through Workspace, and we always allow long press
3109 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003110 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003111 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003112 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003113 // User long pressed on empty space
3114 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3115 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003116 if (mWorkspace.isInOverviewMode()) {
3117 mWorkspace.startReordering(v);
3118 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003119 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003122 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3123 mHotseat.getOrderInHotseat(
3124 longClickCellInfo.cellX,
3125 longClickCellInfo.cellY));
3126 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003128 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 }
3130 }
3131 }
3132 return true;
3133 }
3134
Winson Chung3d503fb2011-07-13 17:25:49 -07003135 boolean isHotseatLayout(View layout) {
3136 return mHotseat != null && layout != null &&
3137 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3138 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003139
Winson Chung3d503fb2011-07-13 17:25:49 -07003140 /**
3141 * Returns the CellLayout of the specified container at the specified screen.
3142 */
Sunny Goyal92820592015-03-02 11:31:35 -08003143 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3145 if (mHotseat != null) {
3146 return mHotseat.getLayout();
3147 } else {
3148 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003149 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003151 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003152 }
3153 }
3154
Winson Chungb745afb2015-03-02 11:51:23 -08003155 /**
3156 * For overridden classes.
3157 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003158 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003159 return isAppsViewVisible();
3160 }
3161
3162 public boolean isAppsViewVisible() {
3163 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3164 }
3165
3166 public boolean isWidgetsViewVisible() {
3167 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003168 }
3169
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003170 private void setWorkspaceBackground(int background) {
3171 switch (background) {
3172 case WORKSPACE_BACKGROUND_TRANSPARENT:
3173 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3174 break;
3175 case WORKSPACE_BACKGROUND_BLACK:
3176 getWindow().setBackgroundDrawable(null);
3177 break;
3178 default:
3179 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3180 }
Craig Mautner360310b2012-10-26 15:13:08 -07003181 }
3182
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003183 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003184 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3185 int curflags = getWindow().getAttributes().flags
3186 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3187 if (wpflags != curflags) {
3188 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3189 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003190 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003191 }
3192
Michael Jurkae326f182011-11-21 14:05:46 -08003193 @Override
3194 public void onTrimMemory(int level) {
3195 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003196 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3197 // The widget preview db can result in holding onto over
3198 // 3MB of memory for caching which isn't necessary.
3199 SQLiteDatabase.releaseMemory();
3200
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003201 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003202 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003203 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003204 if (mLauncherCallbacks != null) {
3205 mLauncherCallbacks.onTrimMemory(level);
3206 }
Michael Jurkae326f182011-11-21 14:05:46 -08003207 }
3208
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003209 public void showWorkspace(boolean animated) {
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003210 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003211 }
3212
Sunny Goyal83a8f042015-05-19 12:52:12 -07003213 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003214 boolean changed = mState != State.WORKSPACE ||
3215 mWorkspace.getState() != Workspace.State.NORMAL;
3216 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003217 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003218 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003219 Workspace.State.NORMAL, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003220
Michael Jurkab3e22d92011-10-31 15:58:33 -07003221 // Set focus to the AppsCustomize button
3222 if (mAllAppsButton != null) {
3223 mAllAppsButton.requestFocus();
3224 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003225 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003226
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003227 // Change the state *after* we've called all the transition code
3228 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003229
Winson Chung5fb63472011-02-02 17:03:37 -08003230 // Resume the auto-advance of widgets
3231 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003232 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003233
Winson Chung0f785722015-04-08 10:27:49 -07003234 if (changed) {
3235 // Send an accessibility event to announce the context change
3236 getWindow().getDecorView()
3237 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003238 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003239 }
3240
Adam Cohened307df2013-10-02 09:37:31 -07003241 void showOverviewMode(boolean animated) {
3242 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003243 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003244 Workspace.State.OVERVIEW, animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003245 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003246 }
3247
Winson Chungb745afb2015-03-02 11:51:23 -08003248 /**
3249 * Shows the apps view.
3250 */
Winson Chung76648c52015-07-10 14:33:23 -07003251 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3252 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003253 if (resetListToTop) {
3254 mAppsView.scrollToTop();
3255 }
Winson Chung4ac30062015-05-08 17:34:17 -07003256 if (updatePredictedApps) {
3257 tryAndUpdatePredictedApps();
3258 }
Winson Chung76648c52015-07-10 14:33:23 -07003259 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003260 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261
Winson Chungb745afb2015-03-02 11:51:23 -08003262 /**
3263 * Shows the widgets view.
3264 */
3265 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003266 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003267 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003268 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003269 }
Winson Chung76648c52015-07-10 14:33:23 -07003270 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003271
3272 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003273 @Override
3274 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003275 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003276 }
3277 });
Winson Chungb745afb2015-03-02 11:51:23 -08003278 }
3279
3280 /**
3281 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003282 *
3283 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003284 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003285 // TODO: calling method should use the return value so that when {@code false} is returned
3286 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003287 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003288 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3289 mState != State.WIDGETS_SPRING_LOADED) {
3290 return false;
3291 }
3292 if (toState != State.APPS && toState != State.WIDGETS) {
3293 return false;
3294 }
Winson Chungb745afb2015-03-02 11:51:23 -08003295
3296 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003297 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3298 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003299 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003300 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003301 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003302
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003304 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305
3306 // Pause the auto-advance of widgets until we are out of AllApps
3307 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003308 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003309 closeFolder();
3310
3311 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003312 getWindow().getDecorView()
3313 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003314 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003315 }
3316
Winson Chungcd99cd32015-04-29 11:03:24 -07003317 /**
3318 * Updates the workspace and interaction state on state change, and return the animation to this
3319 * new state.
3320 */
Sunny Goyalce5a7e52015-07-08 15:44:27 -07003321 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState,
Winson Chung006ee262015-08-03 14:40:11 -07003322 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003323 Workspace.State fromState = mWorkspace.getState();
Winson2a87fe82015-08-07 10:37:21 -07003324 Animator anim = mWorkspace.setStateWithAnimation(toState, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003325 updateInteraction(fromState, toState);
3326 return anim;
3327 }
3328
Jun Mukaif0033da2015-08-04 18:34:30 -07003329 public void onLauncherClingShown() {
3330 // When a launcher cling appears, it should cover the underlying layers, so their focus
3331 // should be blocked.
3332 if (mDragLayer.getDescendantFocusability() != ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3333 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
3334 }
3335 }
3336
3337 public void onLauncherClingDismissed() {
3338 mDragLayer.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3339 }
3340
Hyunyoung Song3f471442015-04-08 19:01:34 -07003341 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003342 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003343 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3344 mState == State.WIDGETS_SPRING_LOADED) {
3345 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003346 }
Winson Chungb745afb2015-03-02 11:51:23 -08003347
Winson Chung006ee262015-08-03 14:40:11 -07003348 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
Winson2a87fe82015-08-07 10:37:21 -07003349 Workspace.State.SPRING_LOADED, true /* animated */,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003350 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003351 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003352 }
Adam Cohen7777d962011-08-18 18:58:38 -07003353
Hyunyoung Song3f471442015-04-08 19:01:34 -07003354 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003355 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003356 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003357
Winson Chunge7a03942011-08-05 15:05:12 -07003358 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003359 @Override
3360 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003361 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003362 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3363 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003364 // Before we show workspace, hide all apps again because
3365 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3366 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003367 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003368 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003369 } else {
3370 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003371 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003372 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003373 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003374 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375
Michael Jurkad3ef3062010-11-23 16:23:58 -08003376 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003377 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003378 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003379 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003380 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003381 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003382 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383 }
3384
Winson Chung4ac30062015-05-08 17:34:17 -07003385 /**
3386 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3387 * resumed.
3388 */
3389 private void tryAndUpdatePredictedApps() {
3390 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003391 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003392 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003393 mAppsView.setPredictedApps(apps);
3394 }
3395 }
3396 }
3397
Michael Jurkab3e22d92011-10-31 15:58:33 -07003398 void lockAllApps() {
3399 // TODO
3400 }
3401
3402 void unlockAllApps() {
3403 // TODO
3404 }
3405
Sunny Goyal594d76d2014-11-06 10:12:54 -08003406 protected void disableVoiceButtonProxy(boolean disable) {
3407 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003408 }
3409
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003410 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003411 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3412 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003413 }
3414
Adam Cohen24ce0b32014-01-14 16:18:14 -08003415 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003416 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3417 if (searchProvider == null) {
3418 return null;
3419 }
3420
3421 Bundle opts = new Bundle();
3422 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003423 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003424
3425 SharedPreferences sp = getSharedPreferences(
3426 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3427 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003428 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003429 if (!searchProvider.provider.flattenToString().equals(
3430 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003431 || (widgetInfo == null)
3432 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003433 // A valid widget is not already bound.
3434 if (widgetId > -1) {
3435 mAppWidgetHost.deleteAppWidgetId(widgetId);
3436 widgetId = -1;
3437 }
3438
3439 // Try to bind a new widget
3440 widgetId = mAppWidgetHost.allocateAppWidgetId();
3441
3442 if (!AppWidgetManagerCompat.getInstance(this)
3443 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3444 mAppWidgetHost.deleteAppWidgetId(widgetId);
3445 widgetId = -1;
3446 }
3447
3448 sp.edit()
3449 .putInt(QSB_WIDGET_ID, widgetId)
3450 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3451 .commit();
3452 }
3453
Sunny Goyal8d595092015-07-06 12:22:14 -07003454 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003455 if (widgetId != -1) {
3456 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3457 mQsb.updateAppWidgetOptions(opts);
3458 mQsb.setPadding(0, 0, 0, 0);
3459 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003460 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003461 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003462 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003463 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003464 }
3465
Sunny Goyal22235bc2015-04-03 09:23:43 -07003466 private void reinflateQSBIfNecessary() {
3467 if (mQsb instanceof LauncherAppWidgetHostView &&
3468 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3469 mSearchDropTargetBar.removeView(mQsb);
3470 mQsb = null;
3471 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3472 }
3473 }
3474
Michael Jurkad7c28052012-04-27 15:43:36 -07003475 @Override
3476 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003477 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003478 final List<CharSequence> text = event.getText();
3479 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003480 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003481 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003482 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003483 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003484 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003485 } else if (mWorkspace != null) {
3486 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003487 } else {
3488 text.add(getString(R.string.all_apps_home_button_label));
3489 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003490 return result;
3491 }
3492
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003493 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003494 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003495 */
Adam Cohen091440a2015-03-18 14:16:05 -07003496 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003497 @Override
3498 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003499 closeSystemDialogs();
3500 }
3501 }
3502
3503 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 * If the activity is currently paused, signal that we need to run the passed Runnable
3505 * in onResume.
3506 *
3507 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003508 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3509 * wrong when we're not running, and if the activity comes back to what the configuration was
3510 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003511 *
3512 * Implementation of the method from LauncherModel.Callbacks.
3513 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003514 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003516 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003517 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003518 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003519 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003520 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003521 }
3522 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003523 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003524 return true;
3525 } else {
3526 return false;
3527 }
3528 }
3529
Michael Jurkac402cd92013-05-20 15:49:32 +02003530 private boolean waitUntilResume(Runnable run) {
3531 return waitUntilResume(run, false);
3532 }
3533
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003535 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003536 }
3537
Michael Jurka7607c2f2013-04-03 14:33:19 -07003538 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 * If the activity is currently paused, signal that we need to re-run the loader
3540 * in onResume.
3541 *
3542 * This needs to be called from incoming places where resources might have been loaded
3543 * while we are paused. That is becaues the Configuration might be wrong
3544 * when we're not running, and if it comes back to what it was when we
3545 * were paused, we are not restarted.
3546 *
3547 * Implementation of the method from LauncherModel.Callbacks.
3548 *
3549 * @return true if we are currently paused. The caller might be able to
3550 * skip some work in that case since we will come back again.
3551 */
3552 public boolean setLoadOnResume() {
3553 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003554 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555 mOnResumeNeedsLoad = true;
3556 return true;
3557 } else {
3558 return false;
3559 }
3560 }
3561
3562 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003564 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003566 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003567 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003568 } else {
Winson Chung882a52e2015-07-08 14:32:26 -07003569 return 0;
Joe Onoratod0afc872010-06-11 00:03:15 -07003570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 /**
3574 * Refreshes the shortcuts shown on the workspace.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
3578 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003579 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003580
Michael Jurka7607c2f2013-04-03 14:33:19 -07003581 // If we're starting binding all over again, clear any bind calls we'd postponed in
3582 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3583 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003584 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003585
Winson Chungd64d1762013-08-20 14:37:16 -07003586 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003587 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003588 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003589
Michael Jurka05bf644e2011-11-30 20:28:53 -08003590 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003591 if (mHotseat != null) {
3592 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003593 }
3594 }
3595
Adam Cohendcd297f2013-06-18 13:13:40 -07003596 @Override
3597 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003598 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003599
Adam Cohen5084cba2013-09-03 12:01:16 -07003600 // If there are no screens, we need to have an empty screen
3601 if (orderedScreenIds.size() == 0) {
3602 mWorkspace.addExtraEmptyScreen();
3603 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003604
3605 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003606 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003607 if (hasCustomContentToLeft()) {
3608 mWorkspace.createCustomContentContainer();
3609 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003610 }
Winson Chung64359a52013-07-08 17:17:08 -07003611 }
3612
3613 @Override
3614 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 int count = orderedScreenIds.size();
3616 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003617 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003618 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003619 }
3620
Adam Cohen39a06042013-07-19 14:30:12 -07003621 private boolean shouldShowWeightWatcher() {
3622 String spKey = LauncherAppState.getSharedPreferencesKey();
3623 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003624 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003625
3626 return show;
3627 }
3628
3629 private void toggleShowWeightWatcher() {
3630 String spKey = LauncherAppState.getSharedPreferencesKey();
3631 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3632 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3633
3634 show = !show;
3635
3636 SharedPreferences.Editor editor = sp.edit();
3637 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3638 editor.commit();
3639
3640 if (mWeightWatcher != null) {
3641 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3642 }
3643 }
3644
Winson Chungd64d1762013-08-20 14:37:16 -07003645 public void bindAppsAdded(final ArrayList<Long> newScreens,
3646 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003647 final ArrayList<ItemInfo> addAnimated,
3648 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003649 Runnable r = new Runnable() {
3650 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003651 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003652 }
3653 };
3654 if (waitUntilResume(r)) {
3655 return;
3656 }
3657
Winson Chungd64d1762013-08-20 14:37:16 -07003658 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003659 if (newScreens != null) {
3660 bindAddScreens(newScreens);
3661 }
Winson Chungd64d1762013-08-20 14:37:16 -07003662
3663 // We add the items without animation on non-visible pages, and with
3664 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003665 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003666 bindItems(addNotAnimated, 0,
3667 addNotAnimated.size(), false);
3668 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003669 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003670 bindItems(addAnimated, 0,
3671 addAnimated.size(), true);
3672 }
Winson Chungc58497e2013-09-03 17:48:37 -07003673
Winson Chung87412982013-10-03 18:34:14 -07003674 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003675 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003676
Winson Chungb745afb2015-03-02 11:51:23 -08003677 if (addedApps != null && mAppsView != null) {
3678 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003679 }
Winson Chungd64d1762013-08-20 14:37:16 -07003680 }
3681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003682 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 * Bind the items start-end from the list.
3684 *
3685 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686 */
Winson Chung64359a52013-07-08 17:17:08 -07003687 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3688 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003689 Runnable r = new Runnable() {
3690 public void run() {
3691 bindItems(shortcuts, start, end, forceAnimateIcons);
3692 }
3693 };
3694 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003695 return;
3696 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003697
Winson Chungf0c6ae02012-03-21 16:10:31 -07003698 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003699 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3700 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003701 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003702 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003703 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003706
3707 // Short circuit if we are loading dock items for a configuration which has no dock
3708 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3709 mHotseat == null) {
3710 continue;
3711 }
3712
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 switch (item.itemType) {
3714 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3715 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003717 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003718
Winson Chung64359a52013-07-08 17:17:08 -07003719 /*
3720 * TODO: FIX collision case
3721 */
Winson Chungce376632013-07-11 16:12:41 -07003722 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3723 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3724 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003725 View v = cl.getChildAt(item.cellX, item.cellY);
3726 Object tag = v.getTag();
3727 String desc = "Collision while binding workspace item: " + item
3728 + ". Collides with " + tag;
Sunny Goyal6c56c682015-07-16 14:09:05 -07003729 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003730 throw (new RuntimeException(desc));
3731 } else {
3732 Log.d(TAG, desc);
3733 }
Winson Chungce376632013-07-11 16:12:41 -07003734 }
Winson Chung64359a52013-07-08 17:17:08 -07003735 }
3736
Adam Cohendcd297f2013-06-18 13:13:40 -07003737 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3738 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003739 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003740 // Animate all the applications up now
3741 shortcut.setAlpha(0f);
3742 shortcut.setScaleX(0f);
3743 shortcut.setScaleY(0f);
3744 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003745 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003748 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003749 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003750 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003751 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003752 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3753 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003755 default:
3756 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003759
Winson Chung997a9232013-07-24 15:33:46 -07003760 if (animateIcons) {
3761 // Animate to the correct page
3762 if (newShortcutsScreenId > -1) {
3763 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003764 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003765 final Runnable startBounceAnimRunnable = new Runnable() {
3766 public void run() {
3767 anim.playTogether(bounceAnims);
3768 anim.start();
3769 }
3770 };
Winson Chung997a9232013-07-24 15:33:46 -07003771 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003772 // We post the animation slightly delayed to prevent slowdowns
3773 // when we are loading right after we return to launcher.
3774 mWorkspace.postDelayed(new Runnable() {
3775 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003776 if (mWorkspace != null) {
3777 mWorkspace.snapToPage(newScreenIndex);
3778 mWorkspace.postDelayed(startBounceAnimRunnable,
3779 NEW_APPS_ANIMATION_DELAY);
3780 }
Winson Chung94d67682013-09-25 16:29:40 -07003781 }
3782 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003783 } else {
3784 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003785 }
3786 }
Winson Chung64359a52013-07-08 17:17:08 -07003787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003789 }
3790
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 /**
3792 * Implementation of the method from LauncherModel.Callbacks.
3793 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003794 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003795 Runnable r = new Runnable() {
3796 public void run() {
3797 bindFolders(folders);
3798 }
3799 };
3800 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003801 return;
3802 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003803 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805
3806 /**
3807 * Add the views for a widget to the workspace.
3808 *
3809 * Implementation of the method from LauncherModel.Callbacks.
3810 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003811 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003812 Runnable r = new Runnable() {
3813 public void run() {
3814 bindAppWidget(item);
3815 }
3816 };
3817 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003818 return;
3819 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003820
Daniel Sandler843e8602010-06-07 14:59:01 -04003821 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3822 if (DEBUG_WIDGETS) {
3823 Log.d(TAG, "bindAppWidget: " + item);
3824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 final Workspace workspace = mWorkspace;
3826
Adam Cohen59400422014-03-05 18:07:04 -08003827 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003828 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003829
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003830 if (!mIsSafeModeEnabled
3831 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003832 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3833
Sunny Goyalff572272014-07-23 13:58:07 -07003834 if (appWidgetInfo == null) {
3835 if (DEBUG_WIDGETS) {
3836 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3837 + " belongs to component " + item.providerName
3838 + ", as the povider is null");
3839 }
3840 LauncherModel.deleteItemFromDatabase(this, item);
3841 return;
3842 }
Sunny Goyalff572272014-07-23 13:58:07 -07003843
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003844 // If we do not have a valid id, try to bind an id.
3845 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3846 // Note: This assumes that the id remap broadcast is received before this step.
3847 // If that is not the case, the id remap will be ignored and user may see the
3848 // click to setup view.
3849 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3850 pendingInfo.spanX = item.spanX;
3851 pendingInfo.spanY = item.spanY;
3852 pendingInfo.minSpanX = item.minSpanX;
3853 pendingInfo.minSpanY = item.minSpanY;
3854 Bundle options = null;
3855 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003856
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003857 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3858 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3859 newWidgetId, appWidgetInfo, options);
3860
3861 // TODO consider showing a permission dialog when the widget is clicked.
3862 if (!success) {
3863 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3864 if (DEBUG_WIDGETS) {
3865 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3866 + " belongs to component " + item.providerName
3867 + ", as the launcher is unable to bing a new widget id");
3868 }
3869 LauncherModel.deleteItemFromDatabase(this, item);
3870 return;
Sunny Goyalff572272014-07-23 13:58:07 -07003871 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003872
3873 item.appWidgetId = newWidgetId;
3874
3875 // If the widget has a configure activity, it is still needs to set it up, otherwise
3876 // the widget is ready to go.
3877 item.restoreStatus = (appWidgetInfo.configure == null)
3878 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3879 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3880
3881 LauncherModel.updateItemInDatabase(this, item);
3882 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
3883 && (appWidgetInfo.configure == null)) {
3884 // If the ID is already valid, verify if we need to configure or not.
3885 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3886 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07003887 }
Sunny Goyalff572272014-07-23 13:58:07 -07003888 }
3889
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003890 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003891 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003892 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003893 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3894 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003895 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003896
Sunny Goyal651077b2014-06-30 14:15:31 -07003897 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07003898 item.minSpanX = appWidgetInfo.minSpanX;
3899 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07003900 } else {
3901 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003902 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3903 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003904 view.updateIcon(mIconCache);
3905 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003906 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003907 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003911 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912
Adam Cohendcd297f2013-06-18 13:13:40 -07003913 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003915 if (!item.isCustomWidget()) {
3916 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3917 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003918
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 workspace.requestLayout();
3920
Daniel Sandler843e8602010-06-07 14:59:01 -04003921 if (DEBUG_WIDGETS) {
3922 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3923 + (SystemClock.uptimeMillis()-start) + "ms");
3924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 }
3926
Sunny Goyalff572272014-07-23 13:58:07 -07003927 /**
3928 * Restores a pending widget.
3929 *
3930 * @param appWidgetId The app widget id
3931 * @param cellInfo The position on screen where to create the widget.
3932 */
3933 private void completeRestoreAppWidget(final int appWidgetId) {
3934 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3935 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3936 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3937 return;
3938 }
3939
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003940 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003941 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3942
3943 mWorkspace.reinflateWidgetsIfNecessary();
3944 LauncherModel.updateItemInDatabase(this, info);
3945 }
3946
Adam Cohen1462de32012-07-24 22:34:36 -07003947 public void onPageBoundSynchronously(int page) {
3948 mSynchronouslyBoundPages.add(page);
3949 }
3950
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 /**
3952 * Callback saying that there aren't any more items to bind.
3953 *
3954 * Implementation of the method from LauncherModel.Callbacks.
3955 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003956 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003957 Runnable r = new Runnable() {
3958 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003959 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003960 }
3961 };
3962 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003963 return;
3964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 if (mSavedState != null) {
3966 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003967 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 mSavedState = null;
3970 }
3971
Adam Cohen1462de32012-07-24 22:34:36 -07003972 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003974 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07003975 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07003976
3977 // If we received the result of any pending adds while the loader was running (e.g. the
3978 // widget configuration forced an orientation change), process them now.
3979 if (sPendingAddItem != null) {
3980 final long screenId = completeAdd(sPendingAddItem);
3981
3982 // TODO: this moves the user to the page where the pending item was added. Ideally,
3983 // the screen would be guaranteed to exist after bind, and the page would be set through
3984 // the workspace restore process.
3985 mWorkspace.post(new Runnable() {
3986 @Override
3987 public void run() {
3988 mWorkspace.snapToScreenId(screenId);
3989 }
3990 });
3991 sPendingAddItem = null;
3992 }
3993
Sunny Goyal756adbc2015-04-16 15:20:51 -07003994 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07003995
3996 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003997 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07003998 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003999 }
4000
Adam Cohen3ed316a2014-07-23 18:21:20 -07004001 private void sendLoadingCompleteBroadcastIfNecessary() {
4002 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4003 String permission =
4004 getResources().getString(R.string.receive_first_load_broadcast_permission);
4005 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4006 sendBroadcast(intent, permission);
4007 SharedPreferences.Editor editor = mSharedPrefs.edit();
4008 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4009 editor.apply();
4010 }
4011 }
4012
Winson Chunga0b7e862013-09-05 16:03:15 -07004013 public boolean isAllAppsButtonRank(int rank) {
4014 if (mHotseat != null) {
4015 return mHotseat.isAllAppsButtonRank(rank);
4016 }
4017 return false;
4018 }
4019
Winson Chunga2413752012-04-03 14:22:34 -07004020 private boolean canRunNewAppsAnimation() {
4021 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4022 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4023 }
4024
Winson Chungc9168342013-06-26 14:54:55 -07004025 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07004026 ValueAnimator bounceAnim = LauncherAnimUtils.ofViewAlphaAndScale(v, 1, 1, 1);
Winson Chungc9168342013-06-26 14:54:55 -07004027 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4028 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004029 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004030 return bounceAnim;
4031 }
4032
Adam Cohen27772732013-11-11 14:00:56 +00004033 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004034 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004035 }
4036
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004037 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004038 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004039 }
4040
Winson Chung88fa7412015-08-03 14:25:28 -07004041 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004042 if (mSearchDropTargetBar == null) {
4043 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004044 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004045 if (mQsb != null) {
4046 mSearchDropTargetBar.removeView(mQsb);
4047 mQsb = null;
4048 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004049 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004050 }
4051
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004052 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004053 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4054 * multiple calls to bind the same list.)
4055 */
4056 @Thunk ArrayList<AppInfo> mTmpAppsList;
4057 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4058 public void run() {
4059 bindAllApplications(mTmpAppsList);
4060 mTmpAppsList = null;
4061 }
4062 };
4063
4064 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 * Add the icons for all apps.
4066 *
4067 * Implementation of the method from LauncherModel.Callbacks.
4068 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004069 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004070 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4071 mTmpAppsList = apps;
4072 return;
4073 }
4074
Winson Chungb745afb2015-03-02 11:51:23 -08004075 if (mAppsView != null) {
4076 mAppsView.setApps(apps);
4077 }
Adam Cohen9211d422014-10-07 18:14:53 -07004078 if (mLauncherCallbacks != null) {
4079 mLauncherCallbacks.bindAllApplications(apps);
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
4083 /**
4084 * A package was updated.
4085 *
4086 * Implementation of the method from LauncherModel.Callbacks.
4087 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004088 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004089 Runnable r = new Runnable() {
4090 public void run() {
4091 bindAppsUpdated(apps);
4092 }
4093 };
4094 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004095 return;
4096 }
4097
Winson Chungb745afb2015-03-02 11:51:23 -08004098 if (mAppsView != null) {
4099 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004100 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 }
4102
Sunny Goyal4390ace2014-10-13 11:33:11 -07004103 @Override
4104 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4105 Runnable r = new Runnable() {
4106 public void run() {
4107 bindWidgetsRestored(widgets);
4108 }
4109 };
4110 if (waitUntilResume(r)) {
4111 return;
4112 }
4113 mWorkspace.widgetsRestored(widgets);
4114 }
4115
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004117 * Some shortcuts were updated in the background.
4118 *
4119 * Implementation of the method from LauncherModel.Callbacks.
4120 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004121 @Override
4122 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4123 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004124 Runnable r = new Runnable() {
4125 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004126 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004127 }
4128 };
4129 if (waitUntilResume(r)) {
4130 return;
4131 }
4132
Sunny Goyal4390ace2014-10-13 11:33:11 -07004133 if (!updated.isEmpty()) {
4134 mWorkspace.updateShortcuts(updated);
4135 }
4136
4137 if (!removed.isEmpty()) {
4138 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4139 for (ShortcutInfo si : removed) {
4140 removedComponents.add(si.getTargetComponent());
4141 }
4142 mWorkspace.removeItemsByComponentName(removedComponents, user);
4143 // Notify the drag controller
4144 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004145 }
4146 }
4147
4148 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004149 * Update the state of a package, typically related to install state.
4150 *
4151 * Implementation of the method from LauncherModel.Callbacks.
4152 */
Sunny Goyale755d462014-07-22 13:48:29 -07004153 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004154 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4155 Runnable r = new Runnable() {
4156 public void run() {
4157 bindRestoreItemsChange(updates);
4158 }
4159 };
4160 if (waitUntilResume(r)) {
4161 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004162 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004163
Sunny Goyal756adbc2015-04-16 15:20:51 -07004164 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004165 }
4166
4167 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004168 * A package was uninstalled. We take both the super set of packageNames
4169 * in addition to specific applications to remove, the reason being that
4170 * this can be called when a package is updated as well. In that scenario,
4171 * we only remove specific components from the workspace, where as
4172 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004173 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004174 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 * Implementation of the method from LauncherModel.Callbacks.
4176 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004177 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004178 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004179 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004180 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004181 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004182 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004183 }
Winson Chungd64d1762013-08-20 14:37:16 -07004184 };
4185 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004186 return;
4187 }
4188
Sunny Goyal1a745e82014-10-02 15:58:31 -07004189 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004190 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4191 for (AppInfo info : appInfos) {
4192 removedComponents.add(info.componentName);
4193 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004194 if (!packageNames.isEmpty()) {
4195 mWorkspace.removeItemsByPackageName(packageNames, user);
4196 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004197 if (!removedComponents.isEmpty()) {
4198 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004199 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004200 // Notify the drag controller
4201 mDragController.onAppsRemoved(packageNames, removedComponents);
4202
Sunny Goyal1a745e82014-10-02 15:58:31 -07004203 } else {
4204 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004205 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004206
Winson Chungdf95eb12013-10-16 14:57:07 -07004207 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004208 if (mAppsView != null) {
4209 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004211 }
Joe Onoratobe386092009-11-17 17:32:16 -08004212
Michael Jurkac402cd92013-05-20 15:49:32 +02004213 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004214 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004215 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004216 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004217 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004218
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004219 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004220 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004221 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004222 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004223 return;
4224 }
4225
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004226 if (mWidgetsView != null && model != null) {
4227 mWidgetsView.addWidgets(model);
4228 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004229 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004230 }
4231
Winson Chung400438b2011-01-16 17:53:48 -08004232 private int mapConfigurationOriActivityInfoOri(int configOri) {
4233 final Display d = getWindowManager().getDefaultDisplay();
4234 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4235 switch (d.getRotation()) {
4236 case Surface.ROTATION_0:
4237 case Surface.ROTATION_180:
4238 // We are currently in the same basic orientation as the natural orientation
4239 naturalOri = configOri;
4240 break;
4241 case Surface.ROTATION_90:
4242 case Surface.ROTATION_270:
4243 // We are currently in the other basic orientation to the natural orientation
4244 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4245 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4246 break;
4247 }
4248
4249 int[] oriMap = {
4250 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4251 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4252 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4253 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4254 };
4255 // Since the map starts at portrait, we need to offset if this device's natural orientation
4256 // is landscape.
4257 int indexOffset = 0;
4258 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4259 indexOffset = 1;
4260 }
4261 return oriMap[(d.getRotation() + indexOffset) % 4];
4262 }
Adam Cohen446e9402011-09-15 18:21:21 -07004263
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004264 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004265 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004266 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004267 if (Utilities.ATLEAST_JB_MR2) {
4268 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4269 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004270 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4271 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004272 }
Winson Chung4b919f82012-05-01 10:44:08 -07004273 }
4274 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004275
Winson Chung4b919f82012-05-01 10:44:08 -07004276 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004277 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004278 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004279 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004280 } else {
4281 mHandler.postDelayed(new Runnable() {
4282 public void run() {
4283 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4284 }
Sunny Goyal756cd262015-08-20 12:33:21 -07004285 }, RESTORE_SCREEN_ORIENTATION_DELAY);
Winson Chung641d71d2012-04-26 15:58:01 -07004286 }
Winson Chung4b919f82012-05-01 10:44:08 -07004287 }
Winson Chung400438b2011-01-16 17:53:48 -08004288 }
4289
Winson Chunge43a1e72014-01-15 10:33:02 -08004290 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004291 if (mLauncherCallbacks != null) {
4292 return mLauncherCallbacks.isLauncherPreinstalled();
4293 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004294 PackageManager pm = getPackageManager();
4295 try {
4296 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4297 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4298 return true;
4299 } else {
4300 return false;
4301 }
4302 } catch (NameNotFoundException e) {
4303 e.printStackTrace();
4304 return false;
4305 }
4306 }
4307
Adam Cohenea90f832014-05-21 15:03:34 -07004308 /**
4309 * This method indicates whether or not we should suggest default wallpaper dimensions
4310 * when our wallpaper cropper was not yet used to set a wallpaper.
4311 */
4312 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004313 if (mLauncherCallbacks != null) {
4314 return mLauncherCallbacks.overrideWallpaperDimensions();
4315 }
Adam Cohenea90f832014-05-21 15:03:34 -07004316 return true;
4317 }
4318
Adam Cohen432609a2014-03-13 17:03:22 -07004319 /**
4320 * To be overridden by subclasses to indicate that there is an activity to launch
4321 * before showing the standard launcher experience.
4322 */
4323 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004324 if (mLauncherCallbacks != null) {
4325 return mLauncherCallbacks.hasFirstRunActivity();
4326 }
Adam Cohen432609a2014-03-13 17:03:22 -07004327 return false;
4328 }
4329
4330 /**
4331 * To be overridden by subclasses to launch any first run activity
4332 */
4333 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004334 if (mLauncherCallbacks != null) {
4335 return mLauncherCallbacks.getFirstRunActivity();
4336 }
Adam Cohen432609a2014-03-13 17:03:22 -07004337 return null;
4338 }
4339
Winson Chunga6945242014-01-08 14:04:34 -08004340 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004341 return !ActivityManager.isRunningInTestHarness() &&
4342 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004343 }
4344
Adam Cohen4a9423d2014-03-28 14:22:31 -07004345 protected boolean hasRunFirstRunActivity() {
4346 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4347 }
4348
Adam Cohen432609a2014-03-13 17:03:22 -07004349 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004350 if (shouldRunFirstRunActivity() &&
4351 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004352 Intent firstRunIntent = getFirstRunActivity();
4353 if (firstRunIntent != null) {
4354 startActivity(firstRunIntent);
4355 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004356 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004357 }
4358 }
Adam Cohen432609a2014-03-13 17:03:22 -07004359 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004360 }
4361
4362 private void markFirstRunActivityShown() {
4363 SharedPreferences.Editor editor = mSharedPrefs.edit();
4364 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4365 editor.apply();
4366 }
4367
Adam Cohen432609a2014-03-13 17:03:22 -07004368 /**
4369 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4370 * screen that must be displayed and dismissed.
4371 */
4372 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004373 if (mLauncherCallbacks != null) {
4374 return mLauncherCallbacks.hasDismissableIntroScreen();
4375 }
Adam Cohen432609a2014-03-13 17:03:22 -07004376 return false;
4377 }
4378
4379 /**
4380 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4381 */
4382 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004383 if (mLauncherCallbacks != null) {
4384 return mLauncherCallbacks.getIntroScreen();
4385 }
Adam Cohen432609a2014-03-13 17:03:22 -07004386 return null;
4387 }
4388
4389 /**
4390 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4391 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4392 */
4393 private boolean shouldShowIntroScreen() {
4394 return hasDismissableIntroScreen() &&
4395 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4396 }
4397
4398 protected void showIntroScreen() {
4399 View introScreen = getIntroScreen();
4400 changeWallpaperVisiblity(false);
4401 if (introScreen != null) {
4402 mDragLayer.showOverlayView(introScreen);
4403 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004404 if (mLauncherOverlayContainer != null) {
4405 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4406 }
Adam Cohen432609a2014-03-13 17:03:22 -07004407 }
4408
4409 public void dismissIntroScreen() {
4410 markIntroScreenDismissed();
4411 if (showFirstRunActivity()) {
4412 // We delay hiding the intro view until the first run activity is showing. This
4413 // avoids a blip.
4414 mWorkspace.postDelayed(new Runnable() {
4415 @Override
4416 public void run() {
4417 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004418 if (mLauncherOverlayContainer != null) {
4419 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4420 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004421 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004422 }
4423 }, ACTIVITY_START_DELAY);
4424 } else {
4425 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004426 if (mLauncherOverlayContainer != null) {
4427 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4428 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004429 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004430 }
4431 changeWallpaperVisiblity(true);
4432 }
4433
4434 private void markIntroScreenDismissed() {
4435 SharedPreferences.Editor editor = mSharedPrefs.edit();
4436 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4437 editor.apply();
4438 }
4439
Adam Cohen091440a2015-03-18 14:16:05 -07004440 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004441 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4442 // on the device, then we always show the first run cling experience (or if there is no
4443 // launcher2). Otherwise, we prompt the user upon started for migration
4444 LauncherClings launcherClings = new LauncherClings(this);
4445 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4446 if (mModel.canMigrateFromOldLauncherDb(this)) {
4447 launcherClings.showMigrationCling();
4448 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004449 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004450 }
4451 }
4452 }
4453
Winson Chunga6945242014-01-08 14:04:34 -08004454 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004455 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4456 if (mHotseat != null) mHotseat.setAlpha(1f);
4457 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004458 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4459 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004460 }
4461
4462 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004463 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4464 if (mHotseat != null) mHotseat.setAlpha(0f);
4465 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004466 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4467 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004468 }
4469
Winson Chung5ffd51d2015-06-25 11:36:50 -07004470 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004471 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004472 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004473 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004474 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004475 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004476 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4477 if (userHandle != null) {
4478 user = UserHandleCompat.fromUser(userHandle);
4479 }
4480 }
4481 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004482 }
4483
4484 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004485 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004486 if (user == null) {
4487 user = UserHandleCompat.myUserHandle();
4488 }
4489
4490 // Called from search suggestion, add the profile extra to the intent to ensure that we
4491 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004492 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004493 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004494 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004495 return null;
4496 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004497 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004498 }
4499
Winson Chung5ffd51d2015-06-25 11:36:50 -07004500 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004501 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4502 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004503 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004504 UserHandleCompat.myUserHandle());
4505 }
4506
Winson Chung5ffd51d2015-06-25 11:36:50 -07004507 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004508 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4509 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004510 mWorkspace.onExternalDragStartedWithItem(dragView);
4511 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004512 }
4513
Winson Chung5ffd51d2015-06-25 11:36:50 -07004514 protected void moveWorkspaceToDefaultScreen() {
4515 mWorkspace.moveToDefaultScreen(false);
4516 }
4517
Anjali Koppalf5d29132014-02-28 13:33:27 -08004518 @Override
4519 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004520 if (mLauncherCallbacks != null) {
4521 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4522 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004523 }
4524
Winson Chung80baf5a2010-08-09 16:03:15 -07004525 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004526 * Returns a FastBitmapDrawable with the icon, accurately sized.
4527 */
4528 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4529 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4530 d.setFilterBitmap(true);
4531 resizeIconDrawable(d);
4532 return d;
4533 }
4534
4535 /**
4536 * Resizes an icon drawable to the correct icon size.
4537 */
4538 public void resizeIconDrawable(Drawable icon) {
4539 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4540 }
4541
4542 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004543 * Prints out out state for debugging.
4544 */
4545 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004546 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004547 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004548 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4549 Log.d(TAG, "mRestoring=" + mRestoring);
4550 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004551 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004552 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004553 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004554
Daniel Sandler325dc232013-06-05 22:57:57 -04004555 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004556 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004557
4558 @Override
4559 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4560 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004561 synchronized (sDumpLogs) {
4562 writer.println(" ");
4563 writer.println("Debug logs: ");
4564 for (int i = 0; i < sDumpLogs.size(); i++) {
4565 writer.println(" " + sDumpLogs.get(i));
4566 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004567 }
Adam Cohen9211d422014-10-07 18:14:53 -07004568 if (mLauncherCallbacks != null) {
4569 mLauncherCallbacks.dump(prefix, fd, writer, args);
4570 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004571 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004572
4573 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004574 if (DEBUG_DUMP_LOG) {
4575 synchronized (sDumpLogs) {
4576 Log.d(TAG, "");
4577 Log.d(TAG, "*********************");
4578 Log.d(TAG, "Launcher debug logs: ");
4579 for (int i = 0; i < sDumpLogs.size(); i++) {
4580 Log.d(TAG, " " + sDumpLogs.get(i));
4581 }
4582 Log.d(TAG, "*********************");
4583 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004584 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004585 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004586 }
4587
4588 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004589 addDumpLog(tag, log, null, debugLog);
4590 }
4591
4592 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004593 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004594 if (e != null) {
4595 Log.d(tag, log, e);
4596 } else {
4597 Log.d(tag, log);
4598 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004599 }
Winson Chungede41292013-09-19 16:27:36 -07004600 if (DEBUG_DUMP_LOG) {
4601 sDateStamp.setTime(System.currentTimeMillis());
4602 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004603 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4604 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004605 }
Winson Chungede41292013-09-19 16:27:36 -07004606 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 }
4608
Adam Cohen59400422014-03-05 18:07:04 -08004609 public static CustomAppWidget getCustomAppWidget(String name) {
4610 return sCustomAppWidgets.get(name);
4611 }
4612
4613 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4614 return sCustomAppWidgets;
4615 }
4616
Winson Chungede41292013-09-19 16:27:36 -07004617 public void dumpLogsToLocalData() {
4618 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004619 new AsyncTask<Void, Void, Void>() {
4620 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004621 boolean success = false;
4622 sDateStamp.setTime(sRunStart);
4623 String FILENAME = sDateStamp.getMonth() + "-"
4624 + sDateStamp.getDay() + "_"
4625 + sDateStamp.getHours() + "-"
4626 + sDateStamp.getMinutes() + "_"
4627 + sDateStamp.getSeconds() + ".txt";
4628
4629 FileOutputStream fos = null;
4630 File outFile = null;
4631 try {
4632 outFile = new File(getFilesDir(), FILENAME);
4633 outFile.createNewFile();
4634 fos = new FileOutputStream(outFile);
4635 } catch (Exception e) {
4636 e.printStackTrace();
4637 }
4638 if (fos != null) {
4639 PrintWriter writer = new PrintWriter(fos);
4640
4641 writer.println(" ");
4642 writer.println("Debug logs: ");
4643 synchronized (sDumpLogs) {
4644 for (int i = 0; i < sDumpLogs.size(); i++) {
4645 writer.println(" " + sDumpLogs.get(i));
4646 }
4647 }
4648 writer.close();
4649 }
4650 try {
4651 if (fos != null) {
4652 fos.close();
4653 success = true;
4654 }
4655 } catch (IOException e) {
4656 e.printStackTrace();
4657 }
Michael Jurka43467462013-11-14 12:03:58 +01004658 return null;
Winson Chungede41292013-09-19 16:27:36 -07004659 }
Michael Jurka43467462013-11-14 12:03:58 +01004660 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004661 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004663}
Michael Jurka2763be32011-02-24 11:19:57 -08004664
Dan Sandlerd5024042014-01-09 15:01:33 -05004665interface DebugIntents {
4666 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4667 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004668}