blob: 9989abb6d298dc0616233789ead69a2f8d0c5af4 [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;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import 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;
Adam Cohen6c5891a2014-07-09 23:53:15 -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;
Winson Chungef7f8742015-06-04 17:18:17 -0700100import com.android.launcher3.allapps.AllAppsSearchBarController;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherActivityInfoCompat;
103import com.android.launcher3.compat.LauncherAppsCompat;
104import com.android.launcher3.compat.UserHandleCompat;
105import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700106import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700107import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700108import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700110import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700112
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800118import java.lang.reflect.InvocationTargetException;
119import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000127import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129/**
130 * Default launcher application.
131 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100132public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700133 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800134 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
135 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700136 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700155 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
156 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
157 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
158
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700159 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
160
Mathew Inwood876a8462013-06-14 14:12:41 +0100161 /**
162 * IntentStarter uses request codes starting with this. This must be greater than all activity
163 * request codes used internally.
164 */
165 protected static final int REQUEST_LAST = 100;
166
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800167 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
183 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: int[]
198 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Adam Cohen3ed316a2014-07-23 18:21:20 -0700203 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
204 static final String ACTION_FIRST_LOAD_COMPLETE =
205 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
206
Adam Cohen39a06042013-07-19 14:30:12 -0700207 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700208 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700209
Sunny Goyal594d76d2014-11-06 10:12:54 -0800210 private static final String QSB_WIDGET_ID = "qsb_widget_id";
211 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
212
Winson Chunga6945242014-01-08 14:04:34 -0800213 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
214
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700216 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
217
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700221 private boolean mIsSafeModeEnabled;
222
Adam Cohenc2d6e892014-10-16 09:49:24 -0700223 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
224 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700225 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700228 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
229 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700230 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000232 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
233 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
234
Winson Chunga2413752012-04-03 14:22:34 -0700235 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700236 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
237 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700239
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800240 private final BroadcastReceiver mCloseSystemDialogsReceiver
241 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Sunny Goyalffe83f12014-08-14 17:39:34 -0700252 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800256 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800257 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700258
Michael Jurka0280c3b2010-09-17 15:00:07 -0700259 private int[] mTmpAddItemCellCoordinates = new int[2];
260
Sunny Goyal316490e2015-06-02 09:38:28 -0700261 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800262 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700263
Michael Jurka838a4ca2011-02-07 13:33:06 -0800264 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700265
Winson Chungc51db6a2011-10-05 11:44:49 -0700266 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
268 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700269 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700271 // Main container view and the model for the widget tray screen.
272 @Thunk WidgetsContainerView mWidgetsView;
273 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
Winson Chungf0ea4d32011-06-06 14:27:16 -0700275 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800276 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
280 // scroll issues (because the workspace may not have been measured yet) and extra work.
281 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
282 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
284 private SpannableStringBuilder mDefaultKeySsb = null;
285
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800288 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private boolean mRestoring;
290 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700291 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700294 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private Bundle mSavedInstanceState;
297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700302 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304
Sunny Goyale2df0622015-04-24 11:27:00 -0700305 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Adam Cohen61f560d2013-09-30 15:58:20 -0700307 private View.OnTouchListener mHapticFeedbackTouchListener;
308
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 // Related to the auto-advancing of widgets
310 private final int ADVANCE_MSG = 1;
311 private final int mAdvanceInterval = 20000;
312 private final int mAdvanceStagger = 250;
313 private long mAutoAdvanceSentTime;
314 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 new HashMap<View, AppWidgetProviderInfo>();
317
Winson Chung400438b2011-01-16 17:53:48 -0800318 // Determines how long to wait after a rotation before restoring the screen orientation to
319 // match the sensor state.
320 private final int mRestoreScreenOrientationDelay = 500;
321
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700323
Adam Cohen1462de32012-07-24 22:34:36 -0700324 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700326
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700327 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700328 static Date sDateStamp = new Date();
329 static DateFormat sDateFormat =
330 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
331 static long sRunStart = System.currentTimeMillis();
332 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333
Winson Chung46353de2012-02-16 14:05:10 -0800334 // We only want to get the SharedPreferences once since it does an FS stat each time we get
335 // it from the context.
336 private SharedPreferences mSharedPrefs;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700340 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700345 private DeviceProfile mDeviceProfile;
346
Winson Chung13eb5272015-05-11 16:30:13 -0700347 // This is set to the view that launched the activity that navigated the user away from
348 // launcher. Since there is no callback for when the activity has finished launching, enable
349 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800350 private BubbleTextView mWaitingForResume;
351
Adam Cohen59400422014-03-05 18:07:04 -0800352 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
353 new HashMap<String, CustomAppWidget>();
354
355 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
356 static {
357 if (ENABLE_CUSTOM_WIDGET_TEST) {
358 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
359 }
360 }
361
Chet Haasea8f996d2015-02-17 12:56:04 -0800362 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
363 private static Method sClipRevealMethod = null;
364 static {
365 Class<?> activityOptionsClass = ActivityOptions.class;
366 try {
367 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
368 View.class, int.class, int.class, int.class, int.class);
369 } catch (Exception e) {
370 // Earlier version
371 }
372 }
373
Adam Cohen091440a2015-03-18 14:16:05 -0700374 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700375 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700376 if (mWorkspace != null) {
377 mWorkspace.buildPageHardwareLayers();
378 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700379 }
380 };
381
Adam Cohendb364c32014-05-20 14:23:40 -0700382 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383
Adam Cohen091440a2015-03-18 14:16:05 -0700384 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800385 int requestCode;
386 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700387 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700388 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 int cellX;
390 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700391 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 }
393
Daniel Sandlerff02d492013-08-05 02:12:05 -0400394 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700395 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700396 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400397
398 @Thunk void setOrientation() {
399 if (mRotationEnabled) {
400 unlockScreenOrientation(true);
401 } else {
402 setRequestedOrientation(
403 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700404 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400405 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700406
Sunny Goyal7779d622015-06-11 16:18:39 -0700407 private Runnable mUpdateOrientationRunnable = new Runnable() {
408 public void run() {
409 setOrientation();
410 }
411 };
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 @Override
414 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700415 if (DEBUG_STRICT_MODE) {
416 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
417 .detectDiskReads()
418 .detectDiskWrites()
419 .detectNetwork() // or .detectAll() for all detectable problems
420 .penaltyLog()
421 .build());
422 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
423 .detectLeakedSqlLiteObjects()
424 .detectLeakedClosableObjects()
425 .penaltyLog()
426 .penaltyDeath()
427 .build());
428 }
429
Adam Cohen9211d422014-10-07 18:14:53 -0700430 if (mLauncherCallbacks != null) {
431 mLauncherCallbacks.preOnCreate();
432 }
433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400435
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400436 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400437 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700438
Adam Cohen2e6da152015-05-06 11:42:25 -0700439 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700440 mDeviceProfile = getResources().getConfiguration().orientation
441 == Configuration.ORIENTATION_LANDSCAPE ?
442 app.getInvariantDeviceProfile().landscapeProfile
443 : app.getInvariantDeviceProfile().portraitProfile;
444
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400445 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700446 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700447 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800448 mModel = app.setLauncher(this);
449 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700450
Joe Onorato41a12d22009-10-31 18:30:00 -0400451 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800453 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700454
Daniel Sandlerff02d492013-08-05 02:12:05 -0400455 mStats = new Stats(this);
456
Sunny Goyalffe83f12014-08-14 17:39:34 -0700457 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700458
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700459 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
460 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700461
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700462 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
463 // this also ensures that any synchronous binding below doesn't re-trigger another
464 // LauncherModel load.
465 mPaused = false;
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200468 android.os.Debug.startMethodTracing(
469 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700475 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Joe Onorato7c312c12009-08-13 21:36:53 -0700477 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 mSavedState = savedInstanceState;
480 restoreState(mSavedState);
481
482 if (PROFILE_STARTUP) {
483 android.os.Debug.stopMethodTracing();
484 }
485
486 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700487 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700488 // If the user leaves launcher, then we should just load items asynchronously when
489 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700490 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700491 } else {
492 // We only load the page synchronously if the user rotates (or triggers a
493 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700494 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 }
497
498 // For handling default keys
499 mDefaultKeySsb = new SpannableStringBuilder();
500 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800501
502 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
503 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800504
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700505 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
506 // In case we are on a device with locked rotation, we should look at preferences to check
507 // if the user has specifically allowed rotation.
508 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400509 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700510 }
511
512 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
513 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400514 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700515
Adam Cohen9211d422014-10-07 18:14:53 -0700516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700518 if (mLauncherCallbacks.hasLauncherOverlay()) {
519 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700520 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
521 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
522 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700523 mWorkspace.setLauncherOverlay(mLauncherOverlay);
524 }
Adam Cohen9211d422014-10-07 18:14:53 -0700525 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700526
527 if (shouldShowIntroScreen()) {
528 showIntroScreen();
529 } else {
530 showFirstRunActivity();
531 showFirstRunClings();
532 }
Adam Cohen9211d422014-10-07 18:14:53 -0700533 }
534
Sunny Goyal7779d622015-06-11 16:18:39 -0700535 @Override
536 public void onSettingsChanged(String settings, boolean value) {
537 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
538 mRotationEnabled = value;
539 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
540 mUpdateOrientationRunnable.run();
541 }
542 }
543 }
544
Adam Cohen9211d422014-10-07 18:14:53 -0700545 private LauncherCallbacks mLauncherCallbacks;
546
547 public void onPostCreate(Bundle savedInstanceState) {
548 super.onPostCreate(savedInstanceState);
549 if (mLauncherCallbacks != null) {
550 mLauncherCallbacks.onPostCreate(savedInstanceState);
551 }
552 }
553
554 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
555 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700556 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
557 private boolean mImportanceStored = false;
558 private int mWorkspaceImportanceForAccessibility =
559 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
560 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
561
562 @Override
563 public void onSearchOverlayOpened() {
564 if (mImportanceStored) {
565 return;
566 }
567 // The underlying workspace and hotseat are temporarily suppressed by the search
568 // overlay. So they sholudn't be accessible.
569 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
570 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
571 mWorkspace.setImportantForAccessibility(
572 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
573 mHotseat.setImportantForAccessibility(
574 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
575 mImportanceStored = true;
576 }
577
578 @Override
579 public void onSearchOverlayClosed() {
580 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
581 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
582 mImportanceStored = false;
583 }
584 });
Adam Cohen9211d422014-10-07 18:14:53 -0700585 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700586 }
587
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700588 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700589 public void onLauncherProviderChange() {
590 if (mLauncherCallbacks != null) {
591 mLauncherCallbacks.onLauncherProviderChange();
592 }
593 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594
Winson Chungef7f8742015-06-04 17:18:17 -0700595 /**
596 * Updates the bounds of all the overlays to match the new fixed bounds.
597 */
598 public void updateOverlayBounds(Rect newBounds) {
599 mAppsView.setSearchBarBounds(newBounds);
600 mWidgetsView.setSearchBarBounds(newBounds);
601 }
602
Adam Cohen9211d422014-10-07 18:14:53 -0700603 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700604 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700605 if (mLauncherCallbacks != null) {
606 return mLauncherCallbacks.hasCustomContentToLeft();
607 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700608 return false;
609 }
610
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500612 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600613 * {@link #addToCustomContentPage}. This will only be invoked if
614 * {@link #hasCustomContentToLeft()} is {@code true}.
615 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500616 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700617 if (mLauncherCallbacks != null) {
618 mLauncherCallbacks.populateCustomContentContainer();
619 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600620 }
621
622 /**
623 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
624 * ensure the custom content page is added or removed if necessary.
625 */
626 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600627 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600628 // Not bound yet, wait for bindScreens to be called.
629 return;
630 }
631
632 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
633 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500634 mWorkspace.createCustomContentContainer();
635 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600636 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
637 mWorkspace.removeCustomContentPage();
638 }
639 }
640
Anton Hanssona2f665f2013-09-26 12:18:32 +0100641 public Stats getStats() {
642 return mStats;
643 }
644
Bjorn Bringertc459e522013-06-07 19:36:01 +0100645 public LayoutInflater getInflater() {
646 return mInflater;
647 }
648
Hyunyoung Song3f471442015-04-08 19:01:34 -0700649 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700650 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
651 // that is subsequently removed from the workspace in startBinding().
652 return !mModel.isLoadingWorkspace();
653 }
654
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800655 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000656 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100657 if (Build.VERSION.SDK_INT >= 17) {
658 return View.generateViewId();
659 } else {
660 // View.generateViewId() is not available. The following fallback logic is a copy
661 // of its implementation.
662 for (;;) {
663 final int result = sNextGeneratedId.get();
664 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
665 int newValue = result + 1;
666 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
667 if (sNextGeneratedId.compareAndSet(result, newValue)) {
668 return result;
669 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000670 }
671 }
672 }
673
674 public int getViewIdForItem(ItemInfo info) {
675 // This cast is safe given the > 2B range for int.
676 int itemId = (int) info.id;
677 if (mItemIdToViewId.containsKey(itemId)) {
678 return mItemIdToViewId.get(itemId);
679 }
680 int viewId = generateViewId();
681 mItemIdToViewId.put(itemId, viewId);
682 return viewId;
683 }
684
685 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700686 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
687 * a configuration step, this allows the proper animations to run after other transitions.
688 */
Adam Cohendb364c32014-05-20 14:23:40 -0700689 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700690 long screenId = args.screenId;
691 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
692 // When the screen id represents an actual screen (as opposed to a rank) we make sure
693 // that the drop page actually exists.
694 screenId = ensurePendingDropLayoutExists(args.screenId);
695 }
Adam Cohendb364c32014-05-20 14:23:40 -0700696
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700699 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700700 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700701 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700703 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700704 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700705 case REQUEST_RECONFIGURE_APPWIDGET:
706 completeRestoreAppWidget(args.appWidgetId);
707 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 }
Michael Jurka27614d22012-04-02 06:40:22 -0700709 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
710 // if you turned the screen off and then back while in All Apps, Launcher would not
711 // return to the workspace. Clearing mAddInfo.container here fixes this issue
712 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700713 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
715
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800716 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700717 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700718 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700719 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700720 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800721 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700722
Adam Cohenad4e15c2013-10-17 16:21:35 -0700723 Runnable exitSpringLoaded = new Runnable() {
724 @Override
725 public void run() {
726 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
727 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
728 }
729 };
730
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700732 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700733 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
734 if (resultCode == RESULT_CANCELED) {
735 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700736 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800739 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 }
742 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200743 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
744 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700745 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200746 }
747 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200749
Adam Cohened66b2b2012-01-23 17:28:51 -0800750 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700751 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700752
Adam Cohendb364c32014-05-20 14:23:40 -0700753 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800754 // We have special handling for widgets
755 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800756 final int appWidgetId;
757 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
758 : -1;
759 if (widgetId < 0) {
760 appWidgetId = pendingAddWidgetId;
761 } else {
762 appWidgetId = widgetId;
763 }
764
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800766 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700767 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
768 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 result = RESULT_CANCELED;
770 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700771 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 @Override
773 public void run() {
774 exitSpringLoadedDragModeDelayed(false, 0, null);
775 }
776 };
Adam Cohendb364c32014-05-20 14:23:40 -0700777 if (workspaceLocked) {
778 // No need to remove the empty screen if we're mid-binding, as the
779 // the bind will not add the empty screen.
780 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
781 } else {
782 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
784 }
Winson Chung5aaab772012-04-27 15:24:02 -0700785 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700786 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700787 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
788 // When the screen id represents an actual screen (as opposed to a rank)
789 // we make sure that the drop page actually exists.
790 mPendingAddInfo.screenId =
791 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
792 }
Adam Cohendb364c32014-05-20 14:23:40 -0700793 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
794
795 dropLayout.setDropPending(true);
796 final Runnable onComplete = new Runnable() {
797 @Override
798 public void run() {
799 completeTwoStageWidgetDrop(resultCode, appWidgetId);
800 dropLayout.setDropPending(false);
801 }
802 };
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
805 } else {
806 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
807 mPendingAddInfo);
808 sPendingAddItem = args;
809 }
Winson Chung5aaab772012-04-27 15:24:02 -0700810 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 return;
812 }
813
Sunny Goyalff572272014-07-23 13:58:07 -0700814 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
815 if (resultCode == RESULT_OK) {
816 // Update the widget view.
817 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
818 pendingAddWidgetId, mPendingAddInfo);
819 if (workspaceLocked) {
820 sPendingAddItem = args;
821 } else {
822 completeAdd(args);
823 }
824 }
825 // Leave the widget in the pending state if the user canceled the configure.
826 return;
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 // The pattern used here is that a user PICKs a specific application,
830 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
833 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700834 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700835 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
836 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800837 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700838 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800839 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700840 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700841 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
842 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
Adam Cohen00074722013-10-11 17:46:09 -0700844 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700845 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700846 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800849
850 }
851
852 @Override
853 protected void onActivityResult(
854 final int requestCode, final int resultCode, final Intent data) {
855 handleActivityResult(requestCode, resultCode, data);
856 if (mLauncherCallbacks != null) {
857 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
858 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 }
860
Adam Cohendb364c32014-05-20 14:23:40 -0700861 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
862 appWidgetId, ItemInfo info) {
863 PendingAddArguments args = new PendingAddArguments();
864 args.requestCode = requestCode;
865 args.intent = data;
866 args.container = info.container;
867 args.screenId = info.screenId;
868 args.cellX = info.cellX;
869 args.cellY = info.cellY;
870 args.appWidgetId = appWidgetId;
871 return args;
872 }
873
874 /**
875 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
876 *
877 * @param screenId the screen id to check
878 * @return the new screen, or screenId if it exists
879 */
880 private long ensurePendingDropLayoutExists(long screenId) {
881 CellLayout dropLayout =
882 (CellLayout) mWorkspace.getScreenWithId(screenId);
883 if (dropLayout == null) {
884 // it's possible that the add screen was removed because it was
885 // empty and a re-bind occurred
886 mWorkspace.addExtraEmptyScreen();
887 return mWorkspace.commitExtraEmptyScreen();
888 } else {
889 return screenId;
890 }
891 }
892
Adam Cohen091440a2015-03-18 14:16:05 -0700893 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700894 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700895 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 Runnable onCompleteRunnable = null;
897 int animationType = 0;
898
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 if (resultCode == RESULT_OK) {
901 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
902 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 mPendingAddWidgetInfo);
904 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 onCompleteRunnable = new Runnable() {
906 @Override
907 public void run() {
908 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700909 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
911 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 }
913 };
914 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800915 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700918 if (mDragLayer.getAnimatedView() != null) {
919 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
920 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
921 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700922 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 // The animated view may be null in the case of a rotation during widget configuration
924 onCompleteRunnable.run();
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927
928 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700929 protected void onStop() {
930 super.onStop();
931 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700932
933 if (mLauncherCallbacks != null) {
934 mLauncherCallbacks.onStop();
935 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700936 }
937
938 @Override
939 protected void onStart() {
940 super.onStart();
941 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700942
943 if (mLauncherCallbacks != null) {
944 mLauncherCallbacks.onStart();
945 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 }
947
948 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200950 long startTime = 0;
951 if (DEBUG_RESUME_TIME) {
952 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400953 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200954 }
Adam Cohen9211d422014-10-07 18:14:53 -0700955
956 if (mLauncherCallbacks != null) {
957 mLauncherCallbacks.preOnResume();
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700961
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700963 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700964 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800965 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700966 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700967 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700968 // view after launching an app, as they may be depending on the UI to be static to
969 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700970 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700971 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800972 } else if (mOnResumeState == State.WIDGETS) {
973 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 }
975 mOnResumeState = State.NONE;
976
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700977 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -0700978 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
979 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -0700980
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800981 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700982 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700983 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700984 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700986 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700988 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200989 // We might have postponed some bind calls until onResume (see waitUntilResume) --
990 // execute them here
991 long startTimeCallbacks = 0;
992 if (DEBUG_RESUME_TIME) {
993 startTimeCallbacks = System.currentTimeMillis();
994 }
995
Michael Jurka1e2f4652013-07-08 18:03:46 -0700996 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
997 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200998 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700999 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001000 if (DEBUG_RESUME_TIME) {
1001 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1002 (System.currentTimeMillis() - startTimeCallbacks));
1003 }
Michael Jurka447bf842013-05-15 14:52:15 +02001004 }
Michael Jurka54554252013-08-01 12:52:23 +02001005 if (mOnResumeCallbacks.size() > 0) {
1006 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1007 mOnResumeCallbacks.get(i).run();
1008 }
1009 mOnResumeCallbacks.clear();
1010 }
Winson Chunge4e50662012-01-23 14:45:13 -08001011
1012 // Reset the pressed state of icons that were locked in the press state while activities
1013 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001014 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001015 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001016 mWaitingForResume.setStayPressed(false);
1017 }
Winson Chung82963d52013-10-09 11:20:57 -07001018
Adam Cohen06dff352012-06-01 17:17:08 -07001019 // It is possible that widgets can receive updates while launcher is not in the foreground.
1020 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1021 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1022 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001023 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001024 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001025
Michael Jurka447bf842013-05-15 14:52:15 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1028 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001029
1030 if (mWorkspace.getCustomContentCallbacks() != null) {
1031 // If we are resuming and the custom content is the current page, we call onShow().
1032 // It is also poassible that onShow will instead be called slightly after first layout
1033 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1034 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001035 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001036 }
1037 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001038 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001039 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001040
Sunny Goyal756adbc2015-04-16 15:20:51 -07001041 if (!isWorkspaceLoading()) {
1042 // Process any items that were added while Launcher was away.
1043 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1044 }
Adam Cohen9211d422014-10-07 18:14:53 -07001045
1046 if (mLauncherCallbacks != null) {
1047 mLauncherCallbacks.onResume();
1048 }
Adam Cohen06dff352012-06-01 17:17:08 -07001049 }
1050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001052 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001053 // Ensure that items added to Launcher are queued until Launcher returns
1054 InstallShortcutReceiver.enableInstallQueue();
1055
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001057 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001058 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001059 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001060
1061 // We call onHide() aggressively. The custom content callbacks should be able to
1062 // debounce excess onHide calls.
1063 if (mWorkspace.getCustomContentCallbacks() != null) {
1064 mWorkspace.getCustomContentCallbacks().onHide();
1065 }
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Adam Cohen9211d422014-10-07 18:14:53 -07001067 if (mLauncherCallbacks != null) {
1068 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001069 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 }
1071
1072 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001073 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1074 // by a onResume or by scrolling otherwise.
1075 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001076
1077 // Custom content is completely hidden
1078 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001079
1080 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1081 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001082
1083 // Indicates whether the user is allowed to scroll away from the custom content.
1084 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
1086
Adam Cohenc2d6e892014-10-16 09:49:24 -07001087 public interface LauncherOverlay {
1088
1089 /**
1090 * Touch interaction leading to overscroll has begun
1091 */
1092 public void onScrollInteractionBegin();
1093
1094 /**
1095 * Touch interaction related to overscroll has ended
1096 */
1097 public void onScrollInteractionEnd();
1098
1099 /**
1100 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1101 * screen (or in the case of RTL, the rightmost screen).
1102 */
1103 public void onScrollChange(int progress, boolean rtl);
1104
1105 /**
1106 * Screen has stopped scrolling
1107 */
1108 public void onScrollSettled();
1109
1110 /**
1111 * This method can be called by the Launcher in order to force the LauncherOverlay
1112 * to exit fully immersive mode.
1113 */
1114 public void forceExitFullImmersion();
1115 }
1116
Winson Chung0f785722015-04-08 10:27:49 -07001117 public interface LauncherAppsCallbacks {
1118 /**
1119 * Updates launcher to the available space that AllApps can take so as not to overlap with
1120 * any other views.
1121 */
Winson Chungef7f8742015-06-04 17:18:17 -07001122 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001123 public void onAllAppsBoundsChanged(Rect bounds);
1124
1125 /**
1126 * Called to dismiss all apps if it is showing.
1127 */
Winson Chungef7f8742015-06-04 17:18:17 -07001128 @Deprecated
Winson Chung0f785722015-04-08 10:27:49 -07001129 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001130
1131 /**
1132 * Sets the search manager to be used for app search.
1133 */
Winson Chungef7f8742015-06-04 17:18:17 -07001134 @Deprecated
1135 public void setSearchManager(Object manager);
Winson Chung0f785722015-04-08 10:27:49 -07001136 }
1137
Jun Mukai8af0cd82015-05-12 12:32:12 -07001138 public interface LauncherSearchCallbacks {
1139 /**
1140 * Called when the search overlay is shown.
1141 */
1142 public void onSearchOverlayOpened();
1143
1144 /**
1145 * Called when the search overlay is dismissed.
1146 */
1147 public void onSearchOverlayClosed();
1148 }
1149
Adam Cohenc2d6e892014-10-16 09:49:24 -07001150 public interface LauncherOverlayCallbacks {
1151 /**
1152 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1153 * however it doesn't modify any state within the launcher.
1154 */
1155 public boolean canEnterFullImmersion();
1156
1157 /**
1158 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1159 * eg. by occupying the full screen and handling all touch events.
1160 *
1161 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1162 * case, Launcher will modify any necessary state and assumes the overlay is
1163 * handling all interaction. If false, the LauncherOverlay should cancel any
1164 *
1165 */
1166 public boolean enterFullImmersion();
1167
1168 /**
1169 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1170 * full control over UI and state.
1171 */
1172 public void exitFullImmersion();
1173 }
1174
1175 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1176
1177 @Override
1178 public boolean canEnterFullImmersion() {
1179 return mState == State.WORKSPACE;
1180 }
1181
1182 @Override
1183 public boolean enterFullImmersion() {
1184 if (mState == State.WORKSPACE) {
1185 // When fully immersed, disregard any touches which fall through.
1186 mDragLayer.setBlockTouch(true);
1187 return true;
1188 }
1189 return false;
1190 }
1191
1192 @Override
1193 public void exitFullImmersion() {
1194 mDragLayer.setBlockTouch(false);
1195 }
1196 }
1197
Jorim Jaggid017f882014-01-14 17:08:48 -08001198 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001199 if (mLauncherCallbacks != null) {
1200 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001201 } else {
1202 // On devices with a locked orientation, we will at least have the allow rotation
1203 // setting.
1204 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001205 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001206 }
1207
Adam Cohen9211d422014-10-07 18:14:53 -07001208 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001209 CustomContentCallbacks callbacks, String description) {
1210 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001211 }
1212
Adam Cohenedb40762013-07-18 16:45:45 -07001213 // The custom content needs to offset its content to account for the QSB
1214 public int getTopOffsetForCustomContent() {
1215 return mWorkspace.getPaddingTop();
1216 }
1217
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001218 @Override
1219 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001221 if (mModel.isCurrentCallbacks(this)) {
1222 mModel.stopLoader();
1223 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001224 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1225
Romain Guy13c2e7b2010-03-10 19:45:00 -08001226 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001227 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001228
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230 @Override
1231 public void onWindowFocusChanged(boolean hasFocus) {
1232 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001233 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001234
1235 if (mLauncherCallbacks != null) {
1236 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1237 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001238 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001239
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 private boolean acceptFilter() {
1241 final InputMethodManager inputManager = (InputMethodManager)
1242 getSystemService(Context.INPUT_METHOD_SERVICE);
1243 return !inputManager.isFullscreenMode();
1244 }
1245
1246 @Override
1247 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001248 final int uniChar = event.getUnicodeChar();
1249 final boolean handled = super.onKeyDown(keyCode, event);
1250 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1251 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1253 keyCode, event);
1254 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001256 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 // showSearchDialog()
1258 // If there are multiple keystrokes before the search dialog takes focus,
1259 // onSearchRequested() will be called for every keystroke,
1260 // but it is idempotent, so it's fine.
1261 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263 }
1264
Joe Onorato8a9625e2010-01-28 15:55:35 -08001265 // Eat the long press event so the keyboard doesn't come up.
1266 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1267 return true;
1268 }
1269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 return handled;
1271 }
1272
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 private String getTypedText() {
1274 return mDefaultKeySsb.toString();
1275 }
1276
1277 private void clearTypedText() {
1278 mDefaultKeySsb.clear();
1279 mDefaultKeySsb.clearSpans();
1280 Selection.setSelection(mDefaultKeySsb, 0);
1281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001284 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1285 * State
1286 */
1287 private static State intToState(int stateOrdinal) {
1288 State state = State.WORKSPACE;
1289 final State[] stateValues = State.values();
1290 for (int i = 0; i < stateValues.length; i++) {
1291 if (stateValues[i].ordinal() == stateOrdinal) {
1292 state = stateValues[i];
1293 break;
1294 }
1295 }
1296 return state;
1297 }
1298
1299 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 * Restores the previous state, if it exists.
1301 *
1302 * @param savedState The previous state.
1303 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001304 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 private void restoreState(Bundle savedState) {
1306 if (savedState == null) {
1307 return;
1308 }
1309
Michael Jurka883f55b2010-10-21 15:47:14 -07001310 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001311 if (state == State.APPS || state == State.WIDGETS) {
1312 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001313 }
1314
Adam Cohen21cd0022013-10-09 18:57:02 -07001315 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1316 PagedView.INVALID_RESTORE_PAGE);
1317 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001318 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
Winson Chung3d503fb2011-07-13 17:25:49 -07001321 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001322 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323
Winson Chung3d503fb2011-07-13 17:25:49 -07001324 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1325 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001326 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001327 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1328 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001329 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1330 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001331 AppWidgetProviderInfo info = savedState.getParcelable(
1332 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001333 mPendingAddWidgetInfo = info == null ?
1334 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1335
Adam Cohen4637b5a2013-11-04 18:21:24 -08001336 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001337 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 mRestoring = true;
1339 }
1340
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001341 mItemIdToViewId = (HashMap<Integer, Integer>)
1342 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
1345 /**
1346 * Finds all the views we need and configure them properly.
1347 */
1348 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001349 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001350
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001352 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001353 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1354 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001355 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001356 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001357
John Spurlock77e1f472013-09-11 10:09:51 -04001358 mLauncherView.setSystemUiVisibility(
1359 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001360 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
Winson Chung4c98d922011-05-31 16:50:48 -07001362 // Setup the drag layer
1363 mDragLayer.setup(this, dragController);
1364
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 // Setup the hotseat
1366 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1367 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001368 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 }
1370
Jorim Jaggid017f882014-01-14 17:08:48 -08001371 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001372 View widgetButton = findViewById(R.id.widget_button);
1373 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001374 @Override
1375 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001376 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001377 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001378 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001379 }
1380 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001381 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1382
1383 View wallpaperButton = findViewById(R.id.wallpaper_button);
1384 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 @Override
1386 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001387 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001388 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001389 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 }
1391 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001392 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1393
1394 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001395 if (hasSettings()) {
1396 settingsButton.setOnClickListener(new OnClickListener() {
1397 @Override
1398 public void onClick(View arg0) {
1399 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001400 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001401 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001402 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001403 });
1404 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1405 } else {
1406 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001407 }
1408
Adam Cohendbdff6b2013-09-18 19:09:15 -07001409 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410
Winson Chung4c98d922011-05-31 16:50:48 -07001411 // Setup the workspace
1412 mWorkspace.setHapticFeedbackEnabled(false);
1413 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001414 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001415 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001416
Winson Chungf0ea4d32011-06-06 14:27:16 -07001417 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001418 mSearchDropTargetBar = (SearchDropTargetBar)
1419 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001420
Winson Chungef7f8742015-06-04 17:18:17 -07001421 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001422 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001423 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001424 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1425 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1426 } else {
1427 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1428 }
Craig Mautner360310b2012-10-26 15:13:08 -07001429
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001431 dragController.setDragScoller(mWorkspace);
1432 dragController.setScrollView(mDragLayer);
1433 dragController.setMoveTarget(mWorkspace);
1434 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001435 if (mSearchDropTargetBar != null) {
1436 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001437 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001438 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001439
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001440 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001441 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001442 mWeightWatcher = new WeightWatcher(this);
1443 mWeightWatcher.setAlpha(0.5f);
1444 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001445 new FrameLayout.LayoutParams(
1446 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001447 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001448 Gravity.BOTTOM)
1449 );
Adam Cohen39a06042013-07-19 14:30:12 -07001450
1451 boolean show = shouldShowWeightWatcher();
1452 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
1455
1456 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001457 * Sets the all apps button. This method is called from {@link Hotseat}.
1458 */
1459 public void setAllAppsButton(View allAppsButton) {
1460 mAllAppsButton = allAppsButton;
1461 }
1462
1463 public View getAllAppsButton() {
1464 return mAllAppsButton;
1465 }
1466
1467 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 * Creates a view representing a shortcut.
1469 *
1470 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001472 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001473 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 /**
1477 * Creates a view representing a shortcut inflated from the specified resource.
1478 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * @param parent The group the shortcut belongs to.
1480 * @param info The data structure describing the shortcut.
1481 *
1482 * @return A View inflated from layoutResId.
1483 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001484 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001485 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001486 parent, false);
1487 favorite.applyFromShortcutInfo(info, mIconCache);
1488 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001490 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 return favorite;
1492 }
1493
1494 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 * Add a shortcut to the workspace.
1496 *
1497 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001499 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001500 int cellY) {
1501 int[] cellXY = mTmpAddItemCellCoordinates;
1502 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001503 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001504
Sunny Goyal5c97f512015-05-19 16:03:28 -07001505 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001506 if (info == null) {
1507 return;
1508 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 final View view = createShortcut(info);
1510
Sunny Goyal5c97f512015-05-19 16:03:28 -07001511 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001512 // First we check if we already know the exact location where we want to add this item.
1513 if (cellX >= 0 && cellY >= 0) {
1514 cellXY[0] = cellX;
1515 cellXY[1] = cellY;
1516 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001517
1518 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001519 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001520 true, null,null)) {
1521 return;
1522 }
1523 DragObject dragObject = new DragObject();
1524 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1526 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001527 return;
1528 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001532 foundCellSpan = (result != null);
1533 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001534 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 }
1536
1537 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001538 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 return;
1540 }
1541
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001542 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548 }
1549
Adam Cohen2e6da152015-05-06 11:42:25 -07001550 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1551 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001552 // We want to account for the extra amount of padding that we are adding to the widget
1553 // to ensure that it gets the full amount of space that it has requested
1554 int requiredWidth = minWidth + padding.left + padding.right;
1555 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001556 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001557 }
1558
Adam Cohen2e6da152015-05-06 11:42:25 -07001559 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1560 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001561 }
1562
Adam Cohen2e6da152015-05-06 11:42:25 -07001563 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1564 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001565 }
1566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001568 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001570 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 */
Adam Cohen091440a2015-03-18 14:16:05 -07001572 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001573 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1574
1575 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001577 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1578 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001579 }
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Adam Cohen59400422014-03-05 18:07:04 -08001581 if (appWidgetInfo.isCustomWidget) {
1582 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001583 }
1584
Adam Cohen59400422014-03-05 18:07:04 -08001585 LauncherAppWidgetInfo launcherInfo;
1586 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1587 launcherInfo.spanX = info.spanX;
1588 launcherInfo.spanY = info.spanY;
1589 launcherInfo.minSpanX = info.minSpanX;
1590 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001591 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001594 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595
1596 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001597 if (hostView == null) {
1598 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001599 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1600 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 } else {
1602 // The AppWidgetHostView has already been inflated and instantiated
1603 launcherInfo.hostView = hostView;
1604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001606 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001607 launcherInfo.notifyWidgetSizeChanged(this);
1608
Adam Cohen59400422014-03-05 18:07:04 -08001609 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1610 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001611
1612 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001614 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 }
Romain Guycbb89e42009-06-08 15:52:54 -07001616
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1618 @Override
1619 public void onReceive(Context context, Intent intent) {
1620 final String action = intent.getAction();
1621 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1622 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001623 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001624 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001625
Winson Chungc100e8e2011-08-09 16:02:43 -07001626 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001627 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001628 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001629 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001630 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001631 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1633 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001634 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001635 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1636 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001637 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001638 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1639 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1640 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001641 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001642 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1643 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 }
1645 }
1646 };
1647
1648 @Override
1649 public void onAttachedToWindow() {
1650 super.onAttachedToWindow();
1651
1652 // Listen for broadcasts related to user-presence
1653 final IntentFilter filter = new IntentFilter();
1654 filter.addAction(Intent.ACTION_SCREEN_OFF);
1655 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001656 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001657 if (ENABLE_DEBUG_INTENTS) {
1658 filter.addAction(DebugIntents.DELETE_DATABASE);
1659 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1660 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001661 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001662 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001663 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001664 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 mVisible = true;
1666 }
1667
Adam Cohen0b395352014-06-09 22:54:36 +00001668 /**
1669 * Sets up transparent navigation and status bars in LMP.
1670 * This method is a no-op for other platform versions.
1671 */
Sunny Goyald0091012015-01-20 15:55:34 -08001672 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001673 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001674 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001675 Window window = getWindow();
1676 window.getAttributes().systemUiVisibility |=
1677 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1678 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1679 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1680 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1681 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1682 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1683 window.setStatusBarColor(Color.TRANSPARENT);
1684 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001685 }
1686 }
1687
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 @Override
1689 public void onDetachedFromWindow() {
1690 super.onDetachedFromWindow();
1691 mVisible = false;
1692
Adam Cohend113e0c2010-11-11 10:48:05 -08001693 if (mAttached) {
1694 unregisterReceiver(mReceiver);
1695 mAttached = false;
1696 }
Winson Chungb745afb2015-03-02 11:51:23 -08001697 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
1699
1700 public void onWindowVisibilityChanged(int visibility) {
1701 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001702 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001703 // The following code used to be in onResume, but it turns out onResume is called when
1704 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1705 // is a more appropriate event to handle
1706 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001707 if (!mWorkspaceLoading) {
1708 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001709 // We want to let Launcher draw itself at least once before we force it to build
1710 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001711 // apps is nice and speedy.
1712 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001713 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001714 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001715 if (mStarted) return;
1716 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001717 // We delay the layer building a bit in order to give
1718 // other message processing a time to run. In particular
1719 // this avoids a delay in hiding the IME if it was
1720 // currently shown, because doing that may involve
1721 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001722 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001723 final ViewTreeObserver.OnDrawListener listener = this;
1724 mWorkspace.post(new Runnable() {
1725 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001726 if (mWorkspace != null &&
1727 mWorkspace.getViewTreeObserver() != null) {
1728 mWorkspace.getViewTreeObserver().
1729 removeOnDrawListener(listener);
1730 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001731 }
1732 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001733 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001734 }
1735 });
1736 }
1737 clearTypedText();
1738 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
1740
Adam Cohen091440a2015-03-18 14:16:05 -07001741 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 mHandler.removeMessages(ADVANCE_MSG);
1743 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1744 mHandler.sendMessageDelayed(msg, delay);
1745 mAutoAdvanceSentTime = System.currentTimeMillis();
1746 }
1747
Adam Cohen091440a2015-03-18 14:16:05 -07001748 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1750 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1751 mAutoAdvanceRunning = autoAdvanceRunning;
1752 if (autoAdvanceRunning) {
1753 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1754 sendAdvanceMessage(delay);
1755 } else {
1756 if (!mWidgetsToAdvance.isEmpty()) {
1757 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1758 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1759 }
1760 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001761 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
1763 }
1764 }
1765
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001766 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1767
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001769 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 if (msg.what == ADVANCE_MSG) {
1771 int i = 0;
1772 for (View key: mWidgetsToAdvance.keySet()) {
1773 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1774 final int delay = mAdvanceStagger * i;
1775 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001776 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 public void run() {
1778 ((Advanceable) v).advance();
1779 }
1780 }, delay);
1781 }
1782 i++;
1783 }
1784 sendAdvanceMessage(mAdvanceInterval);
1785 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001786 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001788 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001789
1790 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001791 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1793 if (v instanceof Advanceable) {
1794 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001795 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001796 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798 }
1799
1800 void removeWidgetToAutoAdvance(View hostView) {
1801 if (mWidgetsToAdvance.containsKey(hostView)) {
1802 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001803 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 }
1806
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809 launcherInfo.hostView = null;
1810 }
1811
Hyunyoung Song3f471442015-04-08 19:01:34 -07001812 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001813 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1814 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 }
1816
Winson Chunga6945242014-01-08 14:04:34 -08001817 public DragLayer getDragLayer() {
1818 return mDragLayer;
1819 }
1820
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001821 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001822 return mAppsView;
1823 }
1824
Hyunyoung Song3f471442015-04-08 19:01:34 -07001825 public WidgetsContainerView getWidgetsView() {
1826 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001827 }
1828
Winson Chunga6945242014-01-08 14:04:34 -08001829 public Workspace getWorkspace() {
1830 return mWorkspace;
1831 }
1832
1833 public Hotseat getHotseat() {
1834 return mHotseat;
1835 }
1836
Jorim Jaggid017f882014-01-14 17:08:48 -08001837 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001838 return mOverviewPanel;
1839 }
1840
1841 public SearchDropTargetBar getSearchBar() {
1842 return mSearchDropTargetBar;
1843 }
1844
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001845 public LauncherAppWidgetHost getAppWidgetHost() {
1846 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
Romain Guycbb89e42009-06-08 15:52:54 -07001848
Winson Chunga9abd0e2010-10-27 17:18:37 -07001849 public LauncherModel getModel() {
1850 return mModel;
1851 }
1852
Winson Chunga6945242014-01-08 14:04:34 -08001853 protected SharedPreferences getSharedPrefs() {
1854 return mSharedPrefs;
1855 }
1856
Adam Cohen2e6da152015-05-06 11:42:25 -07001857 public DeviceProfile getDeviceProfile() {
1858 return mDeviceProfile;
1859 }
1860
Bjorn Bringertc459e522013-06-07 19:36:01 +01001861 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001862 getWindow().closeAllPanels();
1863
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001864 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001865 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 }
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 @Override
1869 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001870 long startTime = 0;
1871 if (DEBUG_RESUME_TIME) {
1872 startTime = System.currentTimeMillis();
1873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 super.onNewIntent(intent);
1875
1876 // Close the menu
1877 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001878 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001879 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001880
Adam Cohened307df2013-10-02 09:37:31 -07001881 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1882 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1883 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001884
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 if (mWorkspace == null) {
1886 // Can be cases where mWorkspace is null, this prevents a NPE
1887 return;
1888 }
1889 Folder openFolder = mWorkspace.getOpenFolder();
1890 // In all these cases, only animate if we're already on home
1891 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001892
1893 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1894 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001895 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001896 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001898 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001899
Adam Cohen6fecd412013-10-02 17:41:50 -07001900 closeFolder();
1901 exitSpringLoadedDragMode();
1902
1903 // If we are already on home, then just animate back to the workspace,
1904 // otherwise, just wait until onResume to set the state back to Workspace
1905 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001906 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001907 } else {
1908 mOnResumeState = State.WORKSPACE;
1909 }
1910
1911 final View v = getWindow().peekDecorView();
1912 if (v != null && v.getWindowToken() != null) {
1913 InputMethodManager imm = (InputMethodManager)getSystemService(
1914 INPUT_METHOD_SERVICE);
1915 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1916 }
1917
Winson Chungb745afb2015-03-02 11:51:23 -08001918 // Reset the apps view
1919 if (!alreadyOnHome && mAppsView != null) {
1920 mAppsView.scrollToTop();
1921 }
1922
Hyunyoung Song3f471442015-04-08 19:01:34 -07001923 // Reset the widgets view
1924 if (!alreadyOnHome && mWidgetsView != null) {
1925 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001927
Adam Cohen9211d422014-10-07 18:14:53 -07001928 if (mLauncherCallbacks != null) {
1929 mLauncherCallbacks.onHomeIntent();
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
Adam Cohened307df2013-10-02 09:37:31 -07001932
Michael Jurka447bf842013-05-15 14:52:15 +02001933 if (DEBUG_RESUME_TIME) {
1934 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
Adam Cohen9211d422014-10-07 18:14:53 -07001937 if (mLauncherCallbacks != null) {
1938 mLauncherCallbacks.onNewIntent(intent);
1939 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001940 }
1941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001943 public void onRestoreInstanceState(Bundle state) {
1944 super.onRestoreInstanceState(state);
1945 for (int page: mSynchronouslyBoundPages) {
1946 mWorkspace.restoreInstanceStateForChild(page);
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
1950 @Override
1951 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001952 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001953 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1954 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001955 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001956 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Michael Jurka883f55b2010-10-21 15:47:14 -07001958 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001959 // We close any open folder since it will not be re-opened, and we need to make sure
1960 // this state is reflected.
1961 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mWaitingForResult) {
1965 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1968 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001969 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1970 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1971 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001972 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
Hyunyoung Song3f471442015-04-08 19:01:34 -07001975 // Save the current widgets tray?
1976 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001977 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001978
1979 if (mLauncherCallbacks != null) {
1980 mLauncherCallbacks.onSaveInstanceState(outState);
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001987
Winson Chunge7a03942011-08-05 15:05:12 -07001988 // Remove all pending runnables
1989 mHandler.removeMessages(ADVANCE_MSG);
1990 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001991 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001992
Winson Chungcd2b0142011-06-08 16:02:26 -07001993 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001994 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07001995
1996 // It's possible to receive onDestroy after a new Launcher activity has
1997 // been created. In this case, don't interfere with the new Launcher.
1998 if (mModel.isCurrentCallbacks(this)) {
1999 mModel.stopLoader();
2000 app.setLauncher(null);
2001 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002004 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002006 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002008 mAppWidgetHost = null;
2009
2010 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
2012 TextKeyListener.getInstance().release();
2013
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002014 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002016 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002017 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002018 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002019 mWorkspace = null;
2020 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002021
Michael Jurka2ecf9952012-06-18 12:52:28 -07002022 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002023
2024 if (mLauncherCallbacks != null) {
2025 mLauncherCallbacks.onDestroy();
2026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
Adam Cohena9cf38f2011-05-02 15:36:58 -07002029 public DragController getDragController() {
2030 return mDragController;
2031 }
2032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 @Override
2034 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002035 onStartForResult(requestCode);
2036 super.startActivityForResult(intent, requestCode);
2037 }
2038
2039 @Override
2040 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2041 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2042 onStartForResult(requestCode);
2043 try {
2044 super.startIntentSenderForResult(intent, requestCode,
2045 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2046 } catch (IntentSender.SendIntentException e) {
2047 throw new ActivityNotFoundException();
2048 }
2049 }
2050
2051 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002052 if (requestCode >= 0) {
2053 setWaitingForResult(true);
2054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
2056
Winson Chung70d72102011-08-12 11:24:35 -07002057 /**
2058 * Indicates that we want global search for this activity by setting the globalSearch
2059 * argument for {@link #startSearch} to true.
2060 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002062 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002064
Karl Rosaen138a0412009-04-23 19:00:21 -07002065 if (initialQuery == null) {
2066 // Use any text typed in the launcher as the initial query
2067 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 if (appSearchData == null) {
2070 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002071 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
Winson Chungadf0c182012-08-23 14:59:07 -07002073 Rect sourceBounds = new Rect();
2074 if (mSearchDropTargetBar != null) {
2075 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2076 }
Romain Guycbb89e42009-06-08 15:52:54 -07002077
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002080 if (clearTextImmediately) {
2081 clearTypedText();
2082 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002083
2084 // We need to show the workspace after starting the search
2085 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002086 }
2087
Ian Parkinson047036e2014-09-01 15:40:53 +01002088 /**
2089 * Start a text search.
2090 *
2091 * @return {@code true} if the search will start immediately, so any further keypresses
2092 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2093 * to buffer keypresses.
2094 */
2095 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002096 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002097 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2098 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2099 sourceBounds);
2100 }
2101
Michael Jurkaa33411c2012-06-14 16:18:21 -07002102 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002103 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002104 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 }
2106
2107 /**
2108 * Starts the global search activity. This code is a copied from SearchManager
2109 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002110 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002111 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002112 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002113 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2114 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2115 if (globalSearchActivity == null) {
2116 Log.w(TAG, "No global search activity found.");
2117 return;
2118 }
2119 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2120 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2121 intent.setComponent(globalSearchActivity);
2122 // Make sure that we have a Bundle to put source in
2123 if (appSearchData == null) {
2124 appSearchData = new Bundle();
2125 } else {
2126 appSearchData = new Bundle(appSearchData);
2127 }
Adam Cohen9211d422014-10-07 18:14:53 -07002128 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 if (!appSearchData.containsKey("source")) {
2130 appSearchData.putString("source", getPackageName());
2131 }
2132 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2133 if (!TextUtils.isEmpty(initialQuery)) {
2134 intent.putExtra(SearchManager.QUERY, initialQuery);
2135 }
2136 if (selectInitialQuery) {
2137 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2138 }
2139 intent.setSourceBounds(sourceBounds);
2140 try {
2141 startActivity(intent);
2142 } catch (ActivityNotFoundException ex) {
2143 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 }
2146
Yura4f93ec62014-02-04 14:15:21 +00002147 public boolean isOnCustomContent() {
2148 return mWorkspace.isOnOrMovingToCustomContent();
2149 }
2150
Winson Chung70d72102011-08-12 11:24:35 -07002151 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002152 public boolean onPrepareOptionsMenu(Menu menu) {
2153 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002154 if (!isOnCustomContent()) {
2155 // Close any open folders
2156 closeFolder();
2157 // Stop resizing any widgets
2158 mWorkspace.exitWidgetResizeMode();
2159 if (!mWorkspace.isInOverviewMode()) {
2160 // Show the overview mode
2161 showOverviewMode(true);
2162 } else {
2163 showWorkspace(true);
2164 }
Winson Chunge0298742014-01-17 12:03:00 -08002165 }
Adam Cohen9211d422014-10-07 18:14:53 -07002166 if (mLauncherCallbacks != null) {
2167 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2168 }
2169
Michael Jurkab94f3f82013-09-11 18:08:54 +02002170 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002173 @Override
2174 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002175 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002176 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002177 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002178 }
2179
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 public boolean isWorkspaceLocked() {
2181 return mWorkspaceLoading || mWaitingForResult;
2182 }
2183
Adam Cohen517a7f52014-03-01 12:12:59 -08002184 public boolean isWorkspaceLoading() {
2185 return mWorkspaceLoading;
2186 }
2187
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002188 private void setWorkspaceLoading(boolean value) {
2189 boolean isLocked = isWorkspaceLocked();
2190 mWorkspaceLoading = value;
2191 if (isLocked != isWorkspaceLocked()) {
2192 onWorkspaceLockedChanged();
2193 }
2194 }
2195
2196 private void setWaitingForResult(boolean value) {
2197 boolean isLocked = isWorkspaceLocked();
2198 mWaitingForResult = value;
2199 if (isLocked != isWorkspaceLocked()) {
2200 onWorkspaceLockedChanged();
2201 }
2202 }
2203
Adam Cohen9211d422014-10-07 18:14:53 -07002204 protected void onWorkspaceLockedChanged() {
2205 if (mLauncherCallbacks != null) {
2206 mLauncherCallbacks.onWorkspaceLockedChanged();
2207 }
2208 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002209
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002211 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002213 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2214 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002215 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002216 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002217 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002218
Sunny Goyale6b63a32015-01-16 16:14:29 -08002219 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002220 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002221 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2222 }
2223
Sunny Goyale6b63a32015-01-16 16:14:29 -08002224 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002225 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2226 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002227 if (appWidgetInfo.configure != null) {
2228 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002229 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002230
Bjorn Bringert7984c942009-12-09 15:38:25 +00002231 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002232 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2233 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002236 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002237 Runnable onComplete = new Runnable() {
2238 @Override
2239 public void run() {
2240 // Exit spring loaded mode if necessary after adding the widget
2241 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2242 null);
2243 }
2244 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002246 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002247 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
Romain Guycbb89e42009-06-08 15:52:54 -07002250
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002251 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002252 // Close any folders that may be open.
2253 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002254 mWorkspace.moveToCustomContentScreen(animate);
2255 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002256
2257 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2258 int[] cell, int spanX, int spanY) {
2259 switch (info.itemType) {
2260 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2261 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2262 int span[] = new int[2];
2263 span[0] = spanX;
2264 span[1] = spanY;
2265 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2266 container, screenId, cell, span);
2267 break;
2268 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2269 processShortcutFromDrop(info.componentName, container, screenId, cell);
2270 break;
2271 default:
2272 throw new IllegalStateException("Unknown item type: " + info.itemType);
2273 }
2274 }
2275
Adam Cohenfbba09b2011-07-18 21:43:05 -07002276 /**
2277 * Process a shortcut drop.
2278 *
2279 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002283 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2284 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002288 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289
2290 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 mPendingAddInfo.cellX = cell[0];
2292 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294
2295 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2296 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002297 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002298 }
2299
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 /**
2301 * Process a widget drop.
2302 *
2303 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002307 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2308 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002312 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002313 mPendingAddInfo.minSpanX = info.minSpanX;
2314 mPendingAddInfo.minSpanY = info.minSpanY;
2315
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 mPendingAddInfo.cellX = cell[0];
2318 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002320 if (span != null) {
2321 mPendingAddInfo.spanX = span[0];
2322 mPendingAddInfo.spanY = span[1];
2323 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324
Adam Cohened66b2b2012-01-23 17:28:51 -08002325 AppWidgetHostView hostView = info.boundWidget;
2326 int appWidgetId;
2327 if (hostView != null) {
2328 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002329 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002330
2331 // Clear the boundWidget so that it doesn't get destroyed.
2332 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002333 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002334 // In this case, we either need to start an activity to get permission to bind
2335 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002336 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002337 Bundle options = info.bindOptions;
2338
Sunny Goyalffe83f12014-08-14 17:39:34 -07002339 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2340 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002341 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002342 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002343 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002344 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002345 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2346 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2347 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002348 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2349 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002350 // TODO: we need to make sure that this accounts for the options bundle.
2351 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002352 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2353 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002354 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 }
2356
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002359 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 folderInfo.title = getText(R.string.folder_name);
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002363 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2364 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002365 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366
2367 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 FolderIcon newFolder =
2369 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002370 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002372 // Force measure the new folder icon
2373 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2374 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002375 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 }
Romain Guycbb89e42009-06-08 15:52:54 -07002377
Joe Onorato9c1289c2009-08-17 11:03:03 -04002378 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002379 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002380 }
2381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 @Override
2383 public boolean dispatchKeyEvent(KeyEvent event) {
2384 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2385 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002387 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002388 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002389 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002390 dumpState();
2391 return true;
2392 }
2393 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002394 }
2395 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2396 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002397 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 return true;
2399 }
2400 }
2401
2402 return super.dispatchKeyEvent(event);
2403 }
2404
Joe Onorato88ec0992009-11-19 13:16:06 -08002405 @Override
2406 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002407 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2408 return;
2409 }
2410
Sunny Goyal45478022015-06-08 16:52:41 -07002411 if (mDragController.isDragging()) {
2412 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002413 return;
2414 }
2415
Winson Chungb745afb2015-03-02 11:51:23 -08002416 if (isAppsViewVisible()) {
2417 showWorkspace(true);
2418 } else if (isWidgetsViewVisible()) {
2419 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002420 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002421 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002422 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002423 Folder openFolder = mWorkspace.getOpenFolder();
2424 if (openFolder.isEditingName()) {
2425 openFolder.dismissEditingName();
2426 } else {
2427 closeFolder();
2428 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002429 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002430 mWorkspace.exitWidgetResizeMode();
2431
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002432 // Back button is a no-op here, but give at least some feedback for the button press
2433 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002434 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002435 }
2436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002438 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002439 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002440 @Override
2441 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002442 if (mAppWidgetHost != null) {
2443 mAppWidgetHost.startListening();
2444 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002445 }
2446
2447 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 * Launches the intent referred by the clicked shortcut.
2449 *
2450 * @param v The view representing the clicked shortcut.
2451 */
2452 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002453 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2454 // view has detached (it's possible for this to happen if the view is removed mid touch).
2455 if (v.getWindowToken() == null) {
2456 return;
2457 }
2458
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002459 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002460 return;
2461 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002462
Adam Cohen1697b792013-09-17 19:08:21 -07002463 if (v instanceof Workspace) {
2464 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002465 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002466 }
2467 return;
2468 }
2469
2470 if (v instanceof CellLayout) {
2471 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002472 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002473 }
2474 }
2475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002477 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002478 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002480 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002481 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002482 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002483 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002484 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002485 } else if (tag instanceof AppInfo) {
2486 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002487 } else if (tag instanceof LauncherAppWidgetInfo) {
2488 if (v instanceof PendingAppWidgetHostView) {
2489 onClickPendingWidget((PendingAppWidgetHostView) v);
2490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 }
2492 }
2493
Sunny Goyal70660032015-05-14 00:07:08 -07002494 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002495 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002496 return false;
2497 }
2498
Michael Jurkaaf442092010-06-10 17:01:57 -07002499 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002500 * Event handler for the app widget view which has not fully restored.
2501 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002502 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002503 if (mIsSafeModeEnabled) {
2504 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2505 return;
2506 }
2507
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002508 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002509 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002510 int widgetId = info.appWidgetId;
2511 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2512 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002513 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2514 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002515 mPendingAddInfo.copyFrom(info);
2516 mPendingAddWidgetId = widgetId;
2517
Sunny Goyalffe83f12014-08-14 17:39:34 -07002518 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2519 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002520 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002521 } else if (info.installProgress < 0) {
2522 // The install has not been queued
2523 final String packageName = info.providerName.getPackageName();
2524 showBrokenAppInstallDialog(packageName,
2525 new DialogInterface.OnClickListener() {
2526 public void onClick(DialogInterface dialog, int id) {
2527 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2528 }
2529 });
2530 } else {
2531 // Download has started.
2532 final String packageName = info.providerName.getPackageName();
2533 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002534 }
2535 }
2536
2537 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002538 * Event handler for the "grid" button that appears on the home screen, which
2539 * enters all apps mode.
2540 *
2541 * @param v The view that was clicked.
2542 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 protected void onClickAllAppsButton(View v) {
2544 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002545 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 showWorkspace(true);
2547 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002548 // Try and refresh the set of predicted apps before we enter launcher
2549 showAppsView(true /* animated */, false /* resetListToTop */,
2550 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 }
2552 }
2553
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 private void showBrokenAppInstallDialog(final String packageName,
2555 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002556 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002557 .setTitle(R.string.abandoned_promises_title)
2558 .setMessage(R.string.abandoned_promise_explanation)
2559 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2560 .setNeutralButton(R.string.abandoned_clean_this,
2561 new DialogInterface.OnClickListener() {
2562 public void onClick(DialogInterface dialog, int id) {
2563 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2564 mWorkspace.removeAbandonedPromise(packageName, user);
2565 }
2566 })
2567 .create().show();
2568 return;
2569 }
2570
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002571 /**
2572 * Event handler for an app shortcut click.
2573 *
2574 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2575 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002576 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002577 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2578 Object tag = v.getTag();
2579 if (!(tag instanceof ShortcutInfo)) {
2580 throw new IllegalArgumentException("Input must be a Shortcut");
2581 }
2582
2583 // Open shortcut
2584 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002585
2586 if (shortcut.isDisabled != 0) {
2587 int error = R.string.activity_not_available;
2588 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2589 error = R.string.safemode_shortcut_error;
2590 }
2591 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2592 return;
2593 }
2594
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 final Intent intent = shortcut.intent;
2596
2597 // Check for special shortcuts
2598 if (intent.getComponent() != null) {
2599 final String shortcutClass = intent.getComponent().getClassName();
2600
2601 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2602 MemoryDumpActivity.startDump(this);
2603 return;
2604 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2605 toggleShowWeightWatcher();
2606 return;
2607 }
2608 }
2609
Chris Wren40c5ed32014-06-24 18:24:23 -04002610 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002611 if ((v instanceof BubbleTextView)
2612 && shortcut.isPromise()
2613 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002614 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002615 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002616 new DialogInterface.OnClickListener() {
2617 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002618 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002619 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002621 return;
2622 }
2623
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002625 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002626
2627 if (mLauncherCallbacks != null) {
2628 mLauncherCallbacks.onClickAppShortcut(v);
2629 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 }
2631
Adam Cohen091440a2015-03-18 14:16:05 -07002632 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002634 final ShortcutInfo shortcut;
2635 final Intent intent;
2636 if (tag instanceof ShortcutInfo) {
2637 shortcut = (ShortcutInfo) tag;
2638 intent = shortcut.intent;
2639 int[] pos = new int[2];
2640 v.getLocationOnScreen(pos);
2641 intent.setSourceBounds(new Rect(pos[0], pos[1],
2642 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002643
Sunny Goyal508da152014-08-14 10:53:27 -07002644 } else if (tag instanceof AppInfo) {
2645 shortcut = null;
2646 intent = ((AppInfo) tag).intent;
2647 } else {
2648 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2649 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650
2651 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002652 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653
2654 if (success && v instanceof BubbleTextView) {
2655 mWaitingForResume = (BubbleTextView) v;
2656 mWaitingForResume.setStayPressed(true);
2657 }
2658 }
2659
2660 /**
2661 * Event handler for a folder icon click.
2662 *
2663 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2664 */
2665 protected void onClickFolderIcon(View v) {
2666 if (LOGD) Log.d(TAG, "onClickFolder");
2667 if (!(v instanceof FolderIcon)){
2668 throw new IllegalArgumentException("Input must be a FolderIcon");
2669 }
2670
2671 FolderIcon folderIcon = (FolderIcon) v;
2672 final FolderInfo info = folderIcon.getFolderInfo();
2673 Folder openFolder = mWorkspace.getFolderForTag(info);
2674
2675 // If the folder info reports that the associated folder is open, then verify that
2676 // it is actually opened. There have been a few instances where this gets out of sync.
2677 if (info.opened && openFolder == null) {
2678 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2679 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2680 info.opened = false;
2681 }
2682
2683 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2684 // Close any open folder
2685 closeFolder();
2686 // Open the requested folder
2687 openFolder(folderIcon);
2688 } else {
2689 // Find the open folder...
2690 int folderScreen;
2691 if (openFolder != null) {
2692 folderScreen = mWorkspace.getPageForView(openFolder);
2693 // .. and close it
2694 closeFolder(openFolder);
2695 if (folderScreen != mWorkspace.getCurrentPage()) {
2696 // Close any folder open on the current screen
2697 closeFolder();
2698 // Pull the folder onto this screen
2699 openFolder(folderIcon);
2700 }
2701 }
2702 }
Adam Cohen9211d422014-10-07 18:14:53 -07002703
2704 if (mLauncherCallbacks != null) {
2705 mLauncherCallbacks.onClickFolderIcon(v);
2706 }
Michael Jurka5130e402011-10-13 04:55:35 -07002707 }
2708
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002709 /**
2710 * Event handler for the (Add) Widgets button that appears after a long press
2711 * on the home screen.
2712 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002713 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002715 if (mIsSafeModeEnabled) {
2716 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2717 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002718 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002719 if (mLauncherCallbacks != null) {
2720 mLauncherCallbacks.onClickAddWidgetButton(view);
2721 }
Adam Cohen9211d422014-10-07 18:14:53 -07002722 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002723 }
2724
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 /**
2726 * Event handler for the wallpaper picker button that appears after a long press
2727 * on the home screen.
2728 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002729 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002731 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2732 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002733
2734 if (mLauncherCallbacks != null) {
2735 mLauncherCallbacks.onClickWallpaperPicker(v);
2736 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002737 }
2738
2739 /**
2740 * Event handler for a click on the settings button that appears after a long press
2741 * on the home screen.
2742 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002743 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002745 if (mLauncherCallbacks != null) {
2746 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002747 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002748 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002749 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002750 }
2751
Adam Cohen61f560d2013-09-30 15:58:20 -07002752 public View.OnTouchListener getHapticFeedbackTouchListener() {
2753 if (mHapticFeedbackTouchListener == null) {
2754 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002755 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002756 @Override
2757 public boolean onTouch(View v, MotionEvent event) {
2758 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2759 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2760 }
2761 return false;
2762 }
2763 };
2764 }
2765 return mHapticFeedbackTouchListener;
2766 }
2767
Adam Cohen9211d422014-10-07 18:14:53 -07002768 public void onDragStarted(View view) {
2769 if (isOnCustomContent()) {
2770 // Custom content screen doesn't participate in drag and drop. If on custom
2771 // content screen, move to default.
2772 moveWorkspaceToDefaultScreen();
2773 }
2774
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onDragStarted(view);
2777 }
2778 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002779
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002780 /**
2781 * Called when the user stops interacting with the launcher.
2782 * This implies that the user is now on the homescreen and is not doing housekeeping.
2783 */
Adam Cohen9211d422014-10-07 18:14:53 -07002784 protected void onInteractionEnd() {
2785 if (mLauncherCallbacks != null) {
2786 mLauncherCallbacks.onInteractionEnd();
2787 }
2788 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002789
2790 /**
2791 * Called when the user starts interacting with the launcher.
2792 * The possible interactions are:
2793 * - open all apps
2794 * - reorder an app shortcut, or a widget
2795 * - open the overview mode.
2796 * This is a good time to stop doing things that only make sense
2797 * when the user is on the homescreen and not doing housekeeping.
2798 */
Adam Cohen9211d422014-10-07 18:14:53 -07002799 protected void onInteractionBegin() {
2800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onInteractionBegin();
2802 }
2803 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002804
Winson Chungcd99cd32015-04-29 11:03:24 -07002805 /** Updates the interaction state. */
2806 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002807 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002808 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002809 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2810 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002811 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002812 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002813 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002814 onInteractionEnd();
2815 }
2816 }
2817
Kenny Guyf07af7b2014-07-31 11:39:16 +01002818 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002819 try {
2820 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002821 launcherApps.showAppDetailsForProfile(componentName, user);
2822 } catch (SecurityException e) {
2823 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2824 Log.e(TAG, "Launcher does not have permission to launch settings");
2825 } catch (ActivityNotFoundException e) {
2826 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2827 Log.e(TAG, "Unable to launch settings");
2828 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002829 }
2830
Michael Jurka1e2f4652013-07-08 18:03:46 -07002831 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002832 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2833 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002834 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002835 // System applications cannot be installed. For now, show a toast explaining that.
2836 // We may give them the option of disabling apps this way.
2837 int messageId = R.string.uninstall_system_app_text;
2838 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002839 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002840 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002841 String packageName = componentName.getPackageName();
2842 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002843 Intent intent = new Intent(
2844 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002845 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2846 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002847 if (user != null) {
2848 user.addToIntent(intent, Intent.EXTRA_USER);
2849 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002850 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002851 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002852 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002853 }
2854
Winson Chung8f1eff72015-05-28 17:33:40 -07002855 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002856 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002858 // Only launch using the new animation if the shortcut has not opted out (this is a
2859 // private contract between launcher and may be ignored in the future).
2860 boolean useLaunchAnimation = (v != null) &&
2861 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002862 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2863 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002864
Kenny Guy1317e2d2014-05-08 18:52:50 +01002865 UserHandleCompat user = null;
2866 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2867 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2868 user = userManager.getUserForSerialNumber(serialNumber);
2869 }
2870
2871 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002872 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002873 ActivityOptions opts = null;
2874 if (sClipRevealMethod != null) {
2875 // TODO: call method directly when Launcher3 can depend on M APIs
2876 int left = 0, top = 0;
2877 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2878 if (v instanceof TextView) {
2879 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002880 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2881 if (icon != null) {
2882 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002883 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002884 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002885 width = bounds.width();
2886 height = bounds.height();
2887 }
2888 }
2889 try {
2890 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2891 left, top, width, height);
2892 } catch (IllegalAccessException e) {
2893 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2894 sClipRevealMethod = null;
2895 } catch (InvocationTargetException e) {
2896 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2897 sClipRevealMethod = null;
2898 }
2899 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002900 if (opts == null && !Utilities.isLmpOrAbove()) {
2901 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002902 v.getMeasuredWidth(), v.getMeasuredHeight());
2903 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002904 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002905 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002906
2907 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2908 // Could be launching some bookkeeping activity
2909 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002910 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002911 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002912 launcherApps.startActivityForProfile(intent.getComponent(), user,
2913 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002914 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002915 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 } catch (SecurityException e) {
2917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002918 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002920 "or use the exported attribute for this activity. "
2921 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002923 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002925
Sunny Goyal383c5072015-06-12 21:18:53 -07002926 @Thunk boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002927 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002928 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2929 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2930 return false;
2931 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002932 try {
2933 success = startActivity(v, intent, tag);
2934 } catch (ActivityNotFoundException e) {
2935 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2936 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2937 }
2938 return success;
2939 }
2940
Adam Cohen268c4752012-06-06 17:47:33 -07002941 /**
2942 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2943 * in the DragLayer in the exact absolute location of the original FolderIcon.
2944 */
2945 private void copyFolderIconToImage(FolderIcon fi) {
2946 final int width = fi.getMeasuredWidth();
2947 final int height = fi.getMeasuredHeight();
2948
2949 // Lazy load ImageView, Bitmap and Canvas
2950 if (mFolderIconImageView == null) {
2951 mFolderIconImageView = new ImageView(this);
2952 }
2953 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2954 mFolderIconBitmap.getHeight() != height) {
2955 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2956 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2957 }
2958
2959 DragLayer.LayoutParams lp;
2960 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2961 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2962 } else {
2963 lp = new DragLayer.LayoutParams(width, height);
2964 }
2965
Adam Cohen307fe232012-08-16 17:55:58 -07002966 // The layout from which the folder is being opened may be scaled, adjust the starting
2967 // view size by this scale factor.
2968 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002969 lp.customPosition = true;
2970 lp.x = mRectForFolderAnimation.left;
2971 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002972 lp.width = (int) (scale * width);
2973 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002974
2975 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2976 fi.draw(mFolderIconCanvas);
2977 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002978 if (fi.getFolder() != null) {
2979 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2980 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002981 }
Adam Cohen268c4752012-06-06 17:47:33 -07002982 // Just in case this image view is still in the drag layer from a previous animation,
2983 // we remove it and re-add it.
2984 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2985 mDragLayer.removeView(mFolderIconImageView);
2986 }
2987 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002988 if (fi.getFolder() != null) {
2989 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002990 }
Adam Cohen268c4752012-06-06 17:47:33 -07002991 }
2992
Adam Cohen2801caf2011-05-13 20:57:39 -07002993 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002994 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002995 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2996 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2997 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2998
Adam Cohenc51934b2011-07-26 21:07:43 -07002999 FolderInfo info = (FolderInfo) fi.getTag();
3000 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3001 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003002 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3003 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003004 }
3005
Adam Cohen268c4752012-06-06 17:47:33 -07003006 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3007 copyFolderIconToImage(fi);
3008 fi.setVisibility(View.INVISIBLE);
3009
Michael Jurka2ecf9952012-06-18 12:52:28 -07003010 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003011 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003012 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003013 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3014 }
3015 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 oa.start();
3017 }
3018
Adam Cohen268c4752012-06-06 17:47:33 -07003019 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003020 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003021 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3022 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3023 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3024
Adam Cohen268c4752012-06-06 17:47:33 -07003025 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003026
Adam Cohen268c4752012-06-06 17:47:33 -07003027 // We remove and re-draw the FolderIcon in-case it has changed
3028 mDragLayer.removeView(mFolderIconImageView);
3029 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003030 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003031 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003032 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003033 oa.addListener(new AnimatorListenerAdapter() {
3034 @Override
3035 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003036 if (cl != null) {
3037 cl.clearFolderLeaveBehind();
3038 // Remove the ImageView copy of the FolderIcon and make the original visible.
3039 mDragLayer.removeView(mFolderIconImageView);
3040 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003041 }
3042 }
3043 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 oa.start();
3045 }
3046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003048 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 * is animated relative to the specified View. If the View is null, no animation
3050 * is played.
3051 *
3052 * @param folderInfo The FolderInfo describing the folder to open.
3053 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003054 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003055 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003056 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3057 if (openFolder != null && openFolder != folder) {
3058 // Close any open folder before opening a folder.
3059 closeFolder();
3060 }
3061
Adam Cohena9cf38f2011-05-02 15:36:58 -07003062 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063
Adam Cohena9cf38f2011-05-02 15:36:58 -07003064 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065
Sunny Goyalf4066152015-04-15 09:42:19 -07003066 // While the folder is open, the position of the icon cannot change.
3067 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3068
Adam Cohen4554ee12011-08-03 16:13:21 -07003069 // Just verify that the folder hasn't already been added to the DragLayer.
3070 // There was a one-off crash where the folder had a parent already.
3071 if (folder.getParent() == null) {
3072 mDragLayer.addView(folder);
3073 mDragController.addDropTarget((DropTarget) folder);
3074 } else {
3075 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3076 folder.getParent() + ").");
3077 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003078 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003079 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003080
3081 // Notify the accessibility manager that this folder "window" has appeared and occluded
3082 // the workspace items
3083 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3084 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003085 }
3086
3087 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003088 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003089 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003090 if (folder.isEditingName()) {
3091 folder.dismissEditingName();
3092 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003093 closeFolder(folder);
3094 }
3095 }
3096
Sunny Goyal83a8f042015-05-19 12:52:12 -07003097 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003098 folder.getInfo().opened = false;
3099
3100 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3101 if (parent != null) {
3102 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3103 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003104 if (fi != null) {
3105 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3106 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 }
3108 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003109
3110 // Notify the accessibility manager that this folder "window" has disappeard and no
3111 // longer occludeds the workspace items
3112 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 }
3114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003116 if (!isDraggingEnabled()) return false;
3117 if (isWorkspaceLocked()) return false;
3118 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119
Adam Cohen1697b792013-09-17 19:08:21 -07003120 if (v instanceof Workspace) {
3121 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003122 if (!mWorkspace.isTouchActive()) {
3123 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003124 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3125 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3126 return true;
3127 } else {
3128 return false;
3129 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003130 } else {
3131 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003132 }
Adam Cohen1697b792013-09-17 19:08:21 -07003133 }
3134
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003135 CellLayout.CellInfo longClickCellInfo = null;
3136 View itemUnderLongClick = null;
3137 if (v.getTag() instanceof ItemInfo) {
3138 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003139 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003140 itemUnderLongClick = longClickCellInfo.cell;
3141 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 }
3143
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 // The hotseat touch handling does not go through Workspace, and we always allow long press
3145 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003146 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003147 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003148 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003149 // User long pressed on empty space
3150 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3151 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003152 if (mWorkspace.isInOverviewMode()) {
3153 mWorkspace.startReordering(v);
3154 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003155 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003158 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3159 mHotseat.getOrderInHotseat(
3160 longClickCellInfo.cellX,
3161 longClickCellInfo.cellY));
3162 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003163 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003164 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 }
3166 }
3167 }
3168 return true;
3169 }
3170
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 boolean isHotseatLayout(View layout) {
3172 return mHotseat != null && layout != null &&
3173 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3174 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003175
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 /**
3177 * Returns the CellLayout of the specified container at the specified screen.
3178 */
Sunny Goyal92820592015-03-02 11:31:35 -08003179 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3181 if (mHotseat != null) {
3182 return mHotseat.getLayout();
3183 } else {
3184 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003185 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003186 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003187 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003188 }
3189 }
3190
Winson Chungb745afb2015-03-02 11:51:23 -08003191 /**
3192 * For overridden classes.
3193 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003194 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003195 return isAppsViewVisible();
3196 }
3197
3198 public boolean isAppsViewVisible() {
3199 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3200 }
3201
3202 public boolean isWidgetsViewVisible() {
3203 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003204 }
3205
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003206 private void setWorkspaceBackground(int background) {
3207 switch (background) {
3208 case WORKSPACE_BACKGROUND_TRANSPARENT:
3209 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3210 break;
3211 case WORKSPACE_BACKGROUND_BLACK:
3212 getWindow().setBackgroundDrawable(null);
3213 break;
3214 default:
3215 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3216 }
Craig Mautner360310b2012-10-26 15:13:08 -07003217 }
3218
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003219 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003220 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3221 int curflags = getWindow().getAttributes().flags
3222 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3223 if (wpflags != curflags) {
3224 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3225 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003226 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003227 }
3228
Michael Jurkae326f182011-11-21 14:05:46 -08003229 @Override
3230 public void onTrimMemory(int level) {
3231 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003232 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3233 // The widget preview db can result in holding onto over
3234 // 3MB of memory for caching which isn't necessary.
3235 SQLiteDatabase.releaseMemory();
3236
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003237 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003238 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003239 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003240 if (mLauncherCallbacks != null) {
3241 mLauncherCallbacks.onTrimMemory(level);
3242 }
Michael Jurkae326f182011-11-21 14:05:46 -08003243 }
3244
Winson Chungb745afb2015-03-02 11:51:23 -08003245 @Override
3246 public void onStateTransitionHideSearchBar() {
3247 // Hide the search bar
3248 if (mSearchDropTargetBar != null) {
3249 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3250 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003251 }
3252
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003253 public void showWorkspace(boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003254 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003255 }
3256
Sunny Goyal83a8f042015-05-19 12:52:12 -07003257 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003258 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003259 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003260 }
3261
3262 protected void showWorkspace(int snapToPage, boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003263 showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003264 }
3265
Winson Chungef7f8742015-06-04 17:18:17 -07003266 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003267 boolean changed = mState != State.WORKSPACE ||
3268 mWorkspace.getState() != Workspace.State.NORMAL;
3269 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003270 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003272 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003273 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003274
Winson Chungc7d2b602012-05-16 17:02:20 -07003275 // Show the search bar (only animate if we were showing the drop target bar in spring
3276 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003277 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003278 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003279 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003280
Michael Jurkab3e22d92011-10-31 15:58:33 -07003281 // Set focus to the AppsCustomize button
3282 if (mAllAppsButton != null) {
3283 mAllAppsButton.requestFocus();
3284 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003285 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003286
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003287 // Change the state *after* we've called all the transition code
3288 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003289
Winson Chung5fb63472011-02-02 17:03:37 -08003290 // Resume the auto-advance of widgets
3291 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003292 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003293
Winson Chung0f785722015-04-08 10:27:49 -07003294 if (changed) {
3295 // Send an accessibility event to announce the context change
3296 getWindow().getDecorView()
3297 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003298 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003299 }
3300
Adam Cohened307df2013-10-02 09:37:31 -07003301 void showOverviewMode(boolean animated) {
3302 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003303 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003304 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3305 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003306 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003307 }
3308
Winson Chungb745afb2015-03-02 11:51:23 -08003309 /**
3310 * Shows the apps view.
3311 */
Winson Chung4ac30062015-05-08 17:34:17 -07003312 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003313 if (resetListToTop) {
3314 mAppsView.scrollToTop();
3315 }
Winson Chung4ac30062015-05-08 17:34:17 -07003316 if (updatePredictedApps) {
3317 tryAndUpdatePredictedApps();
3318 }
Winson Chungb745afb2015-03-02 11:51:23 -08003319 showAppsOrWidgets(animated, State.APPS);
3320 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321
Winson Chungb745afb2015-03-02 11:51:23 -08003322 /**
3323 * Shows the widgets view.
3324 */
3325 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003326 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003327 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003328 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003329 }
Winson Chungb745afb2015-03-02 11:51:23 -08003330 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003331
3332 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003333 @Override
3334 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003335 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003336 }
3337 });
Winson Chungb745afb2015-03-02 11:51:23 -08003338 }
3339
3340 /**
3341 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003342 *
3343 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003344 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003345 // TODO: calling method should use the return value so that when {@code false} is returned
3346 // the workspace transition doesn't fall into invalid state.
3347 private boolean showAppsOrWidgets(boolean animated, State toState) {
3348 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3349 mState != State.WIDGETS_SPRING_LOADED) {
3350 return false;
3351 }
3352 if (toState != State.APPS && toState != State.WIDGETS) {
3353 return false;
3354 }
Winson Chungb745afb2015-03-02 11:51:23 -08003355
3356 if (toState == State.APPS) {
Winson Chungef7f8742015-06-04 17:18:17 -07003357 mStateTransitionAnimation.startAnimationToAllApps(mState, animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003358 } else {
3359 mStateTransitionAnimation.startAnimationToWidgets(animated);
3360 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003363 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364
3365 // Pause the auto-advance of widgets until we are out of AllApps
3366 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003367 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 closeFolder();
3369
3370 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003371 getWindow().getDecorView()
3372 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003373 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003374 }
3375
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 /**
3377 * Updates the workspace and interaction state on state change, and return the animation to this
3378 * new state.
3379 */
3380 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chungef7f8742015-06-04 17:18:17 -07003381 boolean animated, boolean hasOverlaySearchBar, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003382 Workspace.State fromState = mWorkspace.getState();
Winson Chungef7f8742015-06-04 17:18:17 -07003383 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated,
3384 hasOverlaySearchBar, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003385 updateInteraction(fromState, toState);
3386 return anim;
3387 }
3388
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003390 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003391 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3392 mState == State.WIDGETS_SPRING_LOADED) {
3393 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003394 }
Winson Chungb745afb2015-03-02 11:51:23 -08003395
3396 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003397 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3398 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003399 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003400 }
Adam Cohen7777d962011-08-18 18:58:38 -07003401
Hyunyoung Song3f471442015-04-08 19:01:34 -07003402 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003403 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003404 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003405
Winson Chunge7a03942011-08-05 15:05:12 -07003406 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003407 @Override
3408 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003409 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3411 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003412 // Before we show workspace, hide all apps again because
3413 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3414 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003416 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003417 } else {
3418 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003419 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003420 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003421 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003422 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423
Michael Jurkad3ef3062010-11-23 16:23:58 -08003424 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003425 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003426 showAppsView(true /* animated */, false /* resetListToTop */,
3427 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003428 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003429 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003430 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003431 }
3432
Winson Chung4ac30062015-05-08 17:34:17 -07003433 /**
3434 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3435 * resumed.
3436 */
3437 private void tryAndUpdatePredictedApps() {
3438 if (mLauncherCallbacks != null) {
3439 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3440 if (!apps.isEmpty()) {
3441 mAppsView.setPredictedApps(apps);
3442 }
3443 }
3444 }
3445
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 void lockAllApps() {
3447 // TODO
3448 }
3449
3450 void unlockAllApps() {
3451 // TODO
3452 }
3453
Sunny Goyal594d76d2014-11-06 10:12:54 -08003454 protected void disableVoiceButtonProxy(boolean disable) {
3455 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003456 }
3457
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003458 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003459 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3460 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003461 }
3462
Adam Cohen24ce0b32014-01-14 16:18:14 -08003463 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003464 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3465 if (searchProvider == null) {
3466 return null;
3467 }
3468
3469 Bundle opts = new Bundle();
3470 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003471 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003472
3473 SharedPreferences sp = getSharedPreferences(
3474 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3475 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003476 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003477 if (!searchProvider.provider.flattenToString().equals(
3478 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003479 || (widgetInfo == null)
3480 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003481 // A valid widget is not already bound.
3482 if (widgetId > -1) {
3483 mAppWidgetHost.deleteAppWidgetId(widgetId);
3484 widgetId = -1;
3485 }
3486
3487 // Try to bind a new widget
3488 widgetId = mAppWidgetHost.allocateAppWidgetId();
3489
3490 if (!AppWidgetManagerCompat.getInstance(this)
3491 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3492 mAppWidgetHost.deleteAppWidgetId(widgetId);
3493 widgetId = -1;
3494 }
3495
3496 sp.edit()
3497 .putInt(QSB_WIDGET_ID, widgetId)
3498 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3499 .commit();
3500 }
3501
3502 if (widgetId != -1) {
3503 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3504 mQsb.updateAppWidgetOptions(opts);
3505 mQsb.setPadding(0, 0, 0, 0);
3506 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003507 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003508 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003509 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003510 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003511 }
3512
Sunny Goyal22235bc2015-04-03 09:23:43 -07003513 private void reinflateQSBIfNecessary() {
3514 if (mQsb instanceof LauncherAppWidgetHostView &&
3515 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3516 mSearchDropTargetBar.removeView(mQsb);
3517 mQsb = null;
3518 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3519 }
3520 }
3521
Michael Jurkad7c28052012-04-27 15:43:36 -07003522 @Override
3523 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003524 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003525 final List<CharSequence> text = event.getText();
3526 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003527 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003528 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003529 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003530 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003531 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003532 } else {
3533 text.add(getString(R.string.all_apps_home_button_label));
3534 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003535 return result;
3536 }
3537
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003538 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003539 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003540 */
Adam Cohen091440a2015-03-18 14:16:05 -07003541 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003542 @Override
3543 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003544 closeSystemDialogs();
3545 }
3546 }
3547
3548 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 * If the activity is currently paused, signal that we need to run the passed Runnable
3550 * in onResume.
3551 *
3552 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003553 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3554 * wrong when we're not running, and if the activity comes back to what the configuration was
3555 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003559 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003560 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003561 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003562 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003563 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003564 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003565 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003566 }
3567 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003568 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003569 return true;
3570 } else {
3571 return false;
3572 }
3573 }
3574
Michael Jurkac402cd92013-05-20 15:49:32 +02003575 private boolean waitUntilResume(Runnable run) {
3576 return waitUntilResume(run, false);
3577 }
3578
Michael Jurka1e2f4652013-07-08 18:03:46 -07003579 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003580 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003581 }
3582
Michael Jurka7607c2f2013-04-03 14:33:19 -07003583 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003584 * If the activity is currently paused, signal that we need to re-run the loader
3585 * in onResume.
3586 *
3587 * This needs to be called from incoming places where resources might have been loaded
3588 * while we are paused. That is becaues the Configuration might be wrong
3589 * when we're not running, and if it comes back to what it was when we
3590 * were paused, we are not restarted.
3591 *
3592 * Implementation of the method from LauncherModel.Callbacks.
3593 *
3594 * @return true if we are currently paused. The caller might be able to
3595 * skip some work in that case since we will come back again.
3596 */
3597 public boolean setLoadOnResume() {
3598 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003599 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003600 mOnResumeNeedsLoad = true;
3601 return true;
3602 } else {
3603 return false;
3604 }
3605 }
3606
3607 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003608 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003609 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003611 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003612 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003613 } else {
3614 return SCREEN_COUNT / 2;
3615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003617
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 /**
3619 * Refreshes the shortcuts shown on the workspace.
3620 *
3621 * Implementation of the method from LauncherModel.Callbacks.
3622 */
3623 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003624 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003625
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 // If we're starting binding all over again, clear any bind calls we'd postponed in
3627 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3628 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003629 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003630
Winson Chungd64d1762013-08-20 14:37:16 -07003631 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003632 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003633 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003634
Michael Jurka05bf644e2011-11-30 20:28:53 -08003635 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003636 if (mHotseat != null) {
3637 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 }
3639 }
3640
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 @Override
3642 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003643 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003644
Adam Cohen5084cba2013-09-03 12:01:16 -07003645 // If there are no screens, we need to have an empty screen
3646 if (orderedScreenIds.size() == 0) {
3647 mWorkspace.addExtraEmptyScreen();
3648 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003649
3650 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003651 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003652 if (hasCustomContentToLeft()) {
3653 mWorkspace.createCustomContentContainer();
3654 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003655 }
Winson Chung64359a52013-07-08 17:17:08 -07003656 }
3657
3658 @Override
3659 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003660 int count = orderedScreenIds.size();
3661 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003662 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003663 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003664 }
3665
Adam Cohen39a06042013-07-19 14:30:12 -07003666 private boolean shouldShowWeightWatcher() {
3667 String spKey = LauncherAppState.getSharedPreferencesKey();
3668 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003669 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003670
3671 return show;
3672 }
3673
3674 private void toggleShowWeightWatcher() {
3675 String spKey = LauncherAppState.getSharedPreferencesKey();
3676 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3677 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3678
3679 show = !show;
3680
3681 SharedPreferences.Editor editor = sp.edit();
3682 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3683 editor.commit();
3684
3685 if (mWeightWatcher != null) {
3686 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3687 }
3688 }
3689
Winson Chungd64d1762013-08-20 14:37:16 -07003690 public void bindAppsAdded(final ArrayList<Long> newScreens,
3691 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003692 final ArrayList<ItemInfo> addAnimated,
3693 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003694 Runnable r = new Runnable() {
3695 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003696 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003697 }
3698 };
3699 if (waitUntilResume(r)) {
3700 return;
3701 }
3702
Winson Chungd64d1762013-08-20 14:37:16 -07003703 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003704 if (newScreens != null) {
3705 bindAddScreens(newScreens);
3706 }
Winson Chungd64d1762013-08-20 14:37:16 -07003707
3708 // We add the items without animation on non-visible pages, and with
3709 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003710 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 bindItems(addNotAnimated, 0,
3712 addNotAnimated.size(), false);
3713 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003714 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003715 bindItems(addAnimated, 0,
3716 addAnimated.size(), true);
3717 }
Winson Chungc58497e2013-09-03 17:48:37 -07003718
Winson Chung87412982013-10-03 18:34:14 -07003719 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003720 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003721
Winson Chungb745afb2015-03-02 11:51:23 -08003722 if (addedApps != null && mAppsView != null) {
3723 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725 }
3726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 * Bind the items start-end from the list.
3729 *
3730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 */
Winson Chung64359a52013-07-08 17:17:08 -07003732 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3733 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
3736 bindItems(shortcuts, start, end, forceAnimateIcons);
3737 }
3738 };
3739 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 return;
3741 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003742
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003744 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3745 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003746 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003748 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003750 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003751
3752 // Short circuit if we are loading dock items for a configuration which has no dock
3753 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3754 mHotseat == null) {
3755 continue;
3756 }
3757
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 switch (item.itemType) {
3759 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3760 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003763
Winson Chung64359a52013-07-08 17:17:08 -07003764 /*
3765 * TODO: FIX collision case
3766 */
Winson Chungce376632013-07-11 16:12:41 -07003767 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3768 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3769 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003770 View v = cl.getChildAt(item.cellX, item.cellY);
3771 Object tag = v.getTag();
3772 String desc = "Collision while binding workspace item: " + item
3773 + ". Collides with " + tag;
3774 if (LauncherAppState.isDogfoodBuild()) {
3775 throw (new RuntimeException(desc));
3776 } else {
3777 Log.d(TAG, desc);
3778 }
Winson Chungce376632013-07-11 16:12:41 -07003779 }
Winson Chung64359a52013-07-08 17:17:08 -07003780 }
3781
Adam Cohendcd297f2013-06-18 13:13:40 -07003782 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3783 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003784 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003785 // Animate all the applications up now
3786 shortcut.setAlpha(0f);
3787 shortcut.setScaleX(0f);
3788 shortcut.setScaleY(0f);
3789 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003790 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003793 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003794 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003795 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003796 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003797 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3798 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003800 default:
3801 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003803 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003804
Winson Chung997a9232013-07-24 15:33:46 -07003805 if (animateIcons) {
3806 // Animate to the correct page
3807 if (newShortcutsScreenId > -1) {
3808 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003809 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003810 final Runnable startBounceAnimRunnable = new Runnable() {
3811 public void run() {
3812 anim.playTogether(bounceAnims);
3813 anim.start();
3814 }
3815 };
Winson Chung997a9232013-07-24 15:33:46 -07003816 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003817 // We post the animation slightly delayed to prevent slowdowns
3818 // when we are loading right after we return to launcher.
3819 mWorkspace.postDelayed(new Runnable() {
3820 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003821 if (mWorkspace != null) {
3822 mWorkspace.snapToPage(newScreenIndex);
3823 mWorkspace.postDelayed(startBounceAnimRunnable,
3824 NEW_APPS_ANIMATION_DELAY);
3825 }
Winson Chung94d67682013-09-25 16:29:40 -07003826 }
3827 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003828 } else {
3829 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003830 }
3831 }
Winson Chung64359a52013-07-08 17:17:08 -07003832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003834 }
3835
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836 /**
3837 * Implementation of the method from LauncherModel.Callbacks.
3838 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003839 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003840 Runnable r = new Runnable() {
3841 public void run() {
3842 bindFolders(folders);
3843 }
3844 };
3845 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 return;
3847 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003848 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850
3851 /**
3852 * Add the views for a widget to the workspace.
3853 *
3854 * Implementation of the method from LauncherModel.Callbacks.
3855 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003856 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003857 Runnable r = new Runnable() {
3858 public void run() {
3859 bindAppWidget(item);
3860 }
3861 };
3862 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003863 return;
3864 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003865
Daniel Sandler843e8602010-06-07 14:59:01 -04003866 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3867 if (DEBUG_WIDGETS) {
3868 Log.d(TAG, "bindAppWidget: " + item);
3869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 final Workspace workspace = mWorkspace;
3871
Adam Cohen59400422014-03-05 18:07:04 -08003872 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003873 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003874
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003875 if (!mIsSafeModeEnabled
3876 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3877 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003878 if (appWidgetInfo == null) {
3879 if (DEBUG_WIDGETS) {
3880 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3881 + " belongs to component " + item.providerName
3882 + ", as the povider is null");
3883 }
3884 LauncherModel.deleteItemFromDatabase(this, item);
3885 return;
3886 }
3887 // Note: This assumes that the id remap broadcast is received before this step.
3888 // If that is not the case, the id remap will be ignored and user may see the
3889 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003890 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003891 pendingInfo.spanX = item.spanX;
3892 pendingInfo.spanY = item.spanY;
3893 pendingInfo.minSpanX = item.minSpanX;
3894 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003895 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003896 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003897
Sunny Goyalff572272014-07-23 13:58:07 -07003898 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003899 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3900 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003901
3902 // TODO consider showing a permission dialog when the widget is clicked.
3903 if (!success) {
3904 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3905 if (DEBUG_WIDGETS) {
3906 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3907 + " belongs to component " + item.providerName
3908 + ", as the launcher is unable to bing a new widget id");
3909 }
3910 LauncherModel.deleteItemFromDatabase(this, item);
3911 return;
3912 }
3913
3914 item.appWidgetId = newWidgetId;
3915
3916 // If the widget has a configure activity, it is still needs to set it up, otherwise
3917 // the widget is ready to go.
3918 item.restoreStatus = (appWidgetInfo.configure == null)
3919 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3920 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3921
3922 LauncherModel.updateItemInDatabase(this, item);
3923 }
3924
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003925 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003926 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003927 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003928 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3929 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003930 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003931
Sunny Goyal651077b2014-06-30 14:15:31 -07003932 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3933 } else {
3934 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003935 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3936 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003937 view.updateIcon(mIconCache);
3938 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003939 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003940 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003944 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945
Adam Cohendcd297f2013-06-18 13:13:40 -07003946 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003948 if (!item.isCustomWidget()) {
3949 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3950 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003951
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 workspace.requestLayout();
3953
Daniel Sandler843e8602010-06-07 14:59:01 -04003954 if (DEBUG_WIDGETS) {
3955 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3956 + (SystemClock.uptimeMillis()-start) + "ms");
3957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 }
3959
Sunny Goyalff572272014-07-23 13:58:07 -07003960 /**
3961 * Restores a pending widget.
3962 *
3963 * @param appWidgetId The app widget id
3964 * @param cellInfo The position on screen where to create the widget.
3965 */
3966 private void completeRestoreAppWidget(final int appWidgetId) {
3967 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3968 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3969 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3970 return;
3971 }
3972
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003973 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003974 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3975
3976 mWorkspace.reinflateWidgetsIfNecessary();
3977 LauncherModel.updateItemInDatabase(this, info);
3978 }
3979
Adam Cohen1462de32012-07-24 22:34:36 -07003980 public void onPageBoundSynchronously(int page) {
3981 mSynchronouslyBoundPages.add(page);
3982 }
3983
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 /**
3985 * Callback saying that there aren't any more items to bind.
3986 *
3987 * Implementation of the method from LauncherModel.Callbacks.
3988 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003989 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003990 Runnable r = new Runnable() {
3991 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003992 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003993 }
3994 };
3995 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003996 return;
3997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 if (mSavedState != null) {
3999 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004000 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 mSavedState = null;
4003 }
4004
Adam Cohen1462de32012-07-24 22:34:36 -07004005 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004007 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004008 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004009
4010 // If we received the result of any pending adds while the loader was running (e.g. the
4011 // widget configuration forced an orientation change), process them now.
4012 if (sPendingAddItem != null) {
4013 final long screenId = completeAdd(sPendingAddItem);
4014
4015 // TODO: this moves the user to the page where the pending item was added. Ideally,
4016 // the screen would be guaranteed to exist after bind, and the page would be set through
4017 // the workspace restore process.
4018 mWorkspace.post(new Runnable() {
4019 @Override
4020 public void run() {
4021 mWorkspace.snapToScreenId(screenId);
4022 }
4023 });
4024 sPendingAddItem = null;
4025 }
4026
Sunny Goyal756adbc2015-04-16 15:20:51 -07004027 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004028
4029 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004030 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004031 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004032 }
4033
Adam Cohen3ed316a2014-07-23 18:21:20 -07004034 private void sendLoadingCompleteBroadcastIfNecessary() {
4035 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4036 String permission =
4037 getResources().getString(R.string.receive_first_load_broadcast_permission);
4038 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4039 sendBroadcast(intent, permission);
4040 SharedPreferences.Editor editor = mSharedPrefs.edit();
4041 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4042 editor.apply();
4043 }
4044 }
4045
Winson Chunga0b7e862013-09-05 16:03:15 -07004046 public boolean isAllAppsButtonRank(int rank) {
4047 if (mHotseat != null) {
4048 return mHotseat.isAllAppsButtonRank(rank);
4049 }
4050 return false;
4051 }
4052
Winson Chunga2413752012-04-03 14:22:34 -07004053 private boolean canRunNewAppsAnimation() {
4054 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4055 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4056 }
4057
Winson Chungc9168342013-06-26 14:54:55 -07004058 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4059 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4060 PropertyValuesHolder.ofFloat("alpha", 1f),
4061 PropertyValuesHolder.ofFloat("scaleX", 1f),
4062 PropertyValuesHolder.ofFloat("scaleY", 1f));
4063 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4064 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004065 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004066 return bounceAnim;
4067 }
4068
Adam Cohen27772732013-11-11 14:00:56 +00004069 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004070 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004071 }
4072
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004073 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004074 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004075 }
4076
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004077 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004078 if (mSearchDropTargetBar == null) {
4079 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004080 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004081 if (mQsb != null) {
4082 mSearchDropTargetBar.removeView(mQsb);
4083 mQsb = null;
4084 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004085 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004086 }
4087
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004088 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004089 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4090 * multiple calls to bind the same list.)
4091 */
4092 @Thunk ArrayList<AppInfo> mTmpAppsList;
4093 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4094 public void run() {
4095 bindAllApplications(mTmpAppsList);
4096 mTmpAppsList = null;
4097 }
4098 };
4099
4100 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 * Add the icons for all apps.
4102 *
4103 * Implementation of the method from LauncherModel.Callbacks.
4104 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004105 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004106 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4107 mTmpAppsList = apps;
4108 return;
4109 }
4110
Winson Chungb745afb2015-03-02 11:51:23 -08004111 if (mAppsView != null) {
4112 mAppsView.setApps(apps);
4113 }
Adam Cohen9211d422014-10-07 18:14:53 -07004114 if (mLauncherCallbacks != null) {
4115 mLauncherCallbacks.bindAllApplications(apps);
4116 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 }
4118
4119 /**
4120 * A package was updated.
4121 *
4122 * Implementation of the method from LauncherModel.Callbacks.
4123 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004124 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004125 Runnable r = new Runnable() {
4126 public void run() {
4127 bindAppsUpdated(apps);
4128 }
4129 };
4130 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004131 return;
4132 }
4133
Winson Chungb745afb2015-03-02 11:51:23 -08004134 if (mAppsView != null) {
4135 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004136 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137 }
4138
Sunny Goyal4390ace2014-10-13 11:33:11 -07004139 @Override
4140 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4141 Runnable r = new Runnable() {
4142 public void run() {
4143 bindWidgetsRestored(widgets);
4144 }
4145 };
4146 if (waitUntilResume(r)) {
4147 return;
4148 }
4149 mWorkspace.widgetsRestored(widgets);
4150 }
4151
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004153 * Some shortcuts were updated in the background.
4154 *
4155 * Implementation of the method from LauncherModel.Callbacks.
4156 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004157 @Override
4158 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4159 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004160 Runnable r = new Runnable() {
4161 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004162 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004163 }
4164 };
4165 if (waitUntilResume(r)) {
4166 return;
4167 }
4168
Sunny Goyal4390ace2014-10-13 11:33:11 -07004169 if (!updated.isEmpty()) {
4170 mWorkspace.updateShortcuts(updated);
4171 }
4172
4173 if (!removed.isEmpty()) {
4174 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4175 for (ShortcutInfo si : removed) {
4176 removedComponents.add(si.getTargetComponent());
4177 }
4178 mWorkspace.removeItemsByComponentName(removedComponents, user);
4179 // Notify the drag controller
4180 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004181 }
4182 }
4183
4184 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004185 * Update the state of a package, typically related to install state.
4186 *
4187 * Implementation of the method from LauncherModel.Callbacks.
4188 */
Sunny Goyale755d462014-07-22 13:48:29 -07004189 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004190 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4191 Runnable r = new Runnable() {
4192 public void run() {
4193 bindRestoreItemsChange(updates);
4194 }
4195 };
4196 if (waitUntilResume(r)) {
4197 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004198 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004199
Sunny Goyal756adbc2015-04-16 15:20:51 -07004200 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004201 }
4202
4203 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004204 * A package was uninstalled. We take both the super set of packageNames
4205 * in addition to specific applications to remove, the reason being that
4206 * this can be called when a package is updated as well. In that scenario,
4207 * we only remove specific components from the workspace, where as
4208 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004210 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004211 * Implementation of the method from LauncherModel.Callbacks.
4212 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004213 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004214 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004215 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004216 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004217 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004218 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004219 }
Winson Chungd64d1762013-08-20 14:37:16 -07004220 };
4221 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004222 return;
4223 }
4224
Sunny Goyal1a745e82014-10-02 15:58:31 -07004225 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004226 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4227 for (AppInfo info : appInfos) {
4228 removedComponents.add(info.componentName);
4229 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004230 if (!packageNames.isEmpty()) {
4231 mWorkspace.removeItemsByPackageName(packageNames, user);
4232 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004233 if (!removedComponents.isEmpty()) {
4234 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004235 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004236 // Notify the drag controller
4237 mDragController.onAppsRemoved(packageNames, removedComponents);
4238
Sunny Goyal1a745e82014-10-02 15:58:31 -07004239 } else {
4240 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004241 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004242
Winson Chungdf95eb12013-10-16 14:57:07 -07004243 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004244 if (mAppsView != null) {
4245 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004246 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004247 }
Joe Onoratobe386092009-11-17 17:32:16 -08004248
Michael Jurkac402cd92013-05-20 15:49:32 +02004249 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004250 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004251 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004252 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004253 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004254
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004255 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004256 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004257 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004258 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004259 return;
4260 }
4261
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004262 if (mWidgetsView != null && model != null) {
4263 mWidgetsView.addWidgets(model);
4264 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004265 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004266 }
4267
Winson Chung400438b2011-01-16 17:53:48 -08004268 private int mapConfigurationOriActivityInfoOri(int configOri) {
4269 final Display d = getWindowManager().getDefaultDisplay();
4270 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4271 switch (d.getRotation()) {
4272 case Surface.ROTATION_0:
4273 case Surface.ROTATION_180:
4274 // We are currently in the same basic orientation as the natural orientation
4275 naturalOri = configOri;
4276 break;
4277 case Surface.ROTATION_90:
4278 case Surface.ROTATION_270:
4279 // We are currently in the other basic orientation to the natural orientation
4280 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4281 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4282 break;
4283 }
4284
4285 int[] oriMap = {
4286 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4287 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4288 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4289 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4290 };
4291 // Since the map starts at portrait, we need to offset if this device's natural orientation
4292 // is landscape.
4293 int indexOffset = 0;
4294 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4295 indexOffset = 1;
4296 }
4297 return oriMap[(d.getRotation() + indexOffset) % 4];
4298 }
Adam Cohen446e9402011-09-15 18:21:21 -07004299
Winson Chung4b919f82012-05-01 10:44:08 -07004300 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004301 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004302 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4303 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4304 .getConfiguration().orientation));
4305 } else {
4306 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4307 }
Winson Chung4b919f82012-05-01 10:44:08 -07004308 }
4309 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004310
Winson Chung4b919f82012-05-01 10:44:08 -07004311 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004312 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004313 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004314 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004315 } else {
4316 mHandler.postDelayed(new Runnable() {
4317 public void run() {
4318 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4319 }
4320 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004321 }
Winson Chung4b919f82012-05-01 10:44:08 -07004322 }
Winson Chung400438b2011-01-16 17:53:48 -08004323 }
4324
Winson Chunge43a1e72014-01-15 10:33:02 -08004325 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004326 if (mLauncherCallbacks != null) {
4327 return mLauncherCallbacks.isLauncherPreinstalled();
4328 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004329 PackageManager pm = getPackageManager();
4330 try {
4331 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4332 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4333 return true;
4334 } else {
4335 return false;
4336 }
4337 } catch (NameNotFoundException e) {
4338 e.printStackTrace();
4339 return false;
4340 }
4341 }
4342
Adam Cohenea90f832014-05-21 15:03:34 -07004343 /**
4344 * This method indicates whether or not we should suggest default wallpaper dimensions
4345 * when our wallpaper cropper was not yet used to set a wallpaper.
4346 */
4347 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004348 if (mLauncherCallbacks != null) {
4349 return mLauncherCallbacks.overrideWallpaperDimensions();
4350 }
Adam Cohenea90f832014-05-21 15:03:34 -07004351 return true;
4352 }
4353
Adam Cohen432609a2014-03-13 17:03:22 -07004354 /**
4355 * To be overridden by subclasses to indicate that there is an activity to launch
4356 * before showing the standard launcher experience.
4357 */
4358 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004359 if (mLauncherCallbacks != null) {
4360 return mLauncherCallbacks.hasFirstRunActivity();
4361 }
Adam Cohen432609a2014-03-13 17:03:22 -07004362 return false;
4363 }
4364
4365 /**
4366 * To be overridden by subclasses to launch any first run activity
4367 */
4368 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.getFirstRunActivity();
4371 }
Adam Cohen432609a2014-03-13 17:03:22 -07004372 return null;
4373 }
4374
Winson Chunga6945242014-01-08 14:04:34 -08004375 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004376 return !ActivityManager.isRunningInTestHarness() &&
4377 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004378 }
4379
Adam Cohen4a9423d2014-03-28 14:22:31 -07004380 protected boolean hasRunFirstRunActivity() {
4381 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4382 }
4383
Adam Cohen432609a2014-03-13 17:03:22 -07004384 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004385 if (shouldRunFirstRunActivity() &&
4386 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004387 Intent firstRunIntent = getFirstRunActivity();
4388 if (firstRunIntent != null) {
4389 startActivity(firstRunIntent);
4390 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004391 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004392 }
4393 }
Adam Cohen432609a2014-03-13 17:03:22 -07004394 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004395 }
4396
4397 private void markFirstRunActivityShown() {
4398 SharedPreferences.Editor editor = mSharedPrefs.edit();
4399 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4400 editor.apply();
4401 }
4402
Adam Cohen432609a2014-03-13 17:03:22 -07004403 /**
4404 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4405 * screen that must be displayed and dismissed.
4406 */
4407 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004408 if (mLauncherCallbacks != null) {
4409 return mLauncherCallbacks.hasDismissableIntroScreen();
4410 }
Adam Cohen432609a2014-03-13 17:03:22 -07004411 return false;
4412 }
4413
4414 /**
4415 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4416 */
4417 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004418 if (mLauncherCallbacks != null) {
4419 return mLauncherCallbacks.getIntroScreen();
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return null;
4422 }
4423
4424 /**
4425 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4426 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4427 */
4428 private boolean shouldShowIntroScreen() {
4429 return hasDismissableIntroScreen() &&
4430 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4431 }
4432
4433 protected void showIntroScreen() {
4434 View introScreen = getIntroScreen();
4435 changeWallpaperVisiblity(false);
4436 if (introScreen != null) {
4437 mDragLayer.showOverlayView(introScreen);
4438 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004439 if (mLauncherOverlayContainer != null) {
4440 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4441 }
Adam Cohen432609a2014-03-13 17:03:22 -07004442 }
4443
4444 public void dismissIntroScreen() {
4445 markIntroScreenDismissed();
4446 if (showFirstRunActivity()) {
4447 // We delay hiding the intro view until the first run activity is showing. This
4448 // avoids a blip.
4449 mWorkspace.postDelayed(new Runnable() {
4450 @Override
4451 public void run() {
4452 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004453 if (mLauncherOverlayContainer != null) {
4454 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4455 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004456 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004457 }
4458 }, ACTIVITY_START_DELAY);
4459 } else {
4460 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004461 if (mLauncherOverlayContainer != null) {
4462 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4463 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004464 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004465 }
4466 changeWallpaperVisiblity(true);
4467 }
4468
4469 private void markIntroScreenDismissed() {
4470 SharedPreferences.Editor editor = mSharedPrefs.edit();
4471 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4472 editor.apply();
4473 }
4474
Adam Cohen091440a2015-03-18 14:16:05 -07004475 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004476 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4477 // on the device, then we always show the first run cling experience (or if there is no
4478 // launcher2). Otherwise, we prompt the user upon started for migration
4479 LauncherClings launcherClings = new LauncherClings(this);
4480 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4481 if (mModel.canMigrateFromOldLauncherDb(this)) {
4482 launcherClings.showMigrationCling();
4483 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004484 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004485 }
4486 }
4487 }
4488
Winson Chunga6945242014-01-08 14:04:34 -08004489 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004490 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4491 if (mHotseat != null) mHotseat.setAlpha(1f);
4492 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4493 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004494 }
4495
4496 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004497 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4498 if (mHotseat != null) mHotseat.setAlpha(0f);
4499 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4500 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004501 }
4502
Mathew Inwood72fbec12013-11-19 15:45:07 +00004503 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004504 // Called from search suggestion, not supported in other profiles.
4505 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4506 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4507 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4508 myUser);
4509 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004510 return null;
4511 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004512 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004513 }
4514
4515 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4516 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004517 // Called from search suggestion, not supported in other profiles.
4518 return createShortcutDragInfo(shortcutIntent, caption, icon,
4519 UserHandleCompat.myUserHandle());
4520 }
4521
4522 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4523 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004524 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004525 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004526 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004527 }
4528
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004529 protected void moveWorkspaceToDefaultScreen() {
4530 mWorkspace.moveToDefaultScreen(false);
4531 }
4532
Mathew Inwood72fbec12013-11-19 15:45:07 +00004533 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4534 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004535 mWorkspace.onExternalDragStartedWithItem(dragView);
4536 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004537 }
4538
Anjali Koppalf5d29132014-02-28 13:33:27 -08004539 @Override
4540 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004541 if (mLauncherCallbacks != null) {
4542 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4543 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004544 }
4545
Winson Chung80baf5a2010-08-09 16:03:15 -07004546 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004547 * Returns a FastBitmapDrawable with the icon, accurately sized.
4548 */
4549 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4550 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4551 d.setFilterBitmap(true);
4552 resizeIconDrawable(d);
4553 return d;
4554 }
4555
4556 /**
4557 * Resizes an icon drawable to the correct icon size.
4558 */
4559 public void resizeIconDrawable(Drawable icon) {
4560 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4561 }
4562
4563 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004564 * Prints out out state for debugging.
4565 */
4566 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004567 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004568 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004569 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4570 Log.d(TAG, "mRestoring=" + mRestoring);
4571 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4572 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004573 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004574 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004575 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004576
Daniel Sandler325dc232013-06-05 22:57:57 -04004577 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004578 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004579
4580 @Override
4581 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4582 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004583 synchronized (sDumpLogs) {
4584 writer.println(" ");
4585 writer.println("Debug logs: ");
4586 for (int i = 0; i < sDumpLogs.size(); i++) {
4587 writer.println(" " + sDumpLogs.get(i));
4588 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004589 }
Adam Cohen9211d422014-10-07 18:14:53 -07004590 if (mLauncherCallbacks != null) {
4591 mLauncherCallbacks.dump(prefix, fd, writer, args);
4592 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004593 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004594
4595 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004596 if (DEBUG_DUMP_LOG) {
4597 synchronized (sDumpLogs) {
4598 Log.d(TAG, "");
4599 Log.d(TAG, "*********************");
4600 Log.d(TAG, "Launcher debug logs: ");
4601 for (int i = 0; i < sDumpLogs.size(); i++) {
4602 Log.d(TAG, " " + sDumpLogs.get(i));
4603 }
4604 Log.d(TAG, "*********************");
4605 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004606 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004607 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004608 }
4609
4610 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004611 addDumpLog(tag, log, null, debugLog);
4612 }
4613
4614 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004616 if (e != null) {
4617 Log.d(tag, log, e);
4618 } else {
4619 Log.d(tag, log);
4620 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004621 }
Winson Chungede41292013-09-19 16:27:36 -07004622 if (DEBUG_DUMP_LOG) {
4623 sDateStamp.setTime(System.currentTimeMillis());
4624 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004625 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4626 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004627 }
Winson Chungede41292013-09-19 16:27:36 -07004628 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004629 }
4630
Adam Cohen59400422014-03-05 18:07:04 -08004631 public static CustomAppWidget getCustomAppWidget(String name) {
4632 return sCustomAppWidgets.get(name);
4633 }
4634
4635 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4636 return sCustomAppWidgets;
4637 }
4638
Winson Chungede41292013-09-19 16:27:36 -07004639 public void dumpLogsToLocalData() {
4640 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004641 new AsyncTask<Void, Void, Void>() {
4642 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004643 boolean success = false;
4644 sDateStamp.setTime(sRunStart);
4645 String FILENAME = sDateStamp.getMonth() + "-"
4646 + sDateStamp.getDay() + "_"
4647 + sDateStamp.getHours() + "-"
4648 + sDateStamp.getMinutes() + "_"
4649 + sDateStamp.getSeconds() + ".txt";
4650
4651 FileOutputStream fos = null;
4652 File outFile = null;
4653 try {
4654 outFile = new File(getFilesDir(), FILENAME);
4655 outFile.createNewFile();
4656 fos = new FileOutputStream(outFile);
4657 } catch (Exception e) {
4658 e.printStackTrace();
4659 }
4660 if (fos != null) {
4661 PrintWriter writer = new PrintWriter(fos);
4662
4663 writer.println(" ");
4664 writer.println("Debug logs: ");
4665 synchronized (sDumpLogs) {
4666 for (int i = 0; i < sDumpLogs.size(); i++) {
4667 writer.println(" " + sDumpLogs.get(i));
4668 }
4669 }
4670 writer.close();
4671 }
4672 try {
4673 if (fos != null) {
4674 fos.close();
4675 success = true;
4676 }
4677 } catch (IOException e) {
4678 e.printStackTrace();
4679 }
Michael Jurka43467462013-11-14 12:03:58 +01004680 return null;
Winson Chungede41292013-09-19 16:27:36 -07004681 }
Michael Jurka43467462013-11-14 12:03:58 +01004682 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004683 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004685}
Michael Jurka2763be32011-02-24 11:19:57 -08004686
Dan Sandlerd5024042014-01-09 15:01:33 -05004687interface DebugIntents {
4688 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4689 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004690}