blob: 1de383c657c645a10bbe240dcd52d463e3772094 [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;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700103import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700105import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700106import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700107import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700108
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.DataInputStream;
110import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800117import java.lang.reflect.InvocationTargetException;
118import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700121import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700124import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700125import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000126import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
134 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Winson Chung83f59ab2015-05-05 17:21:58 -0700138 // Temporary flag
139 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
181 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: boolean
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
189 // Type: long
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800218 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000233 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
234 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
235
Winson Chunga2413752012-04-03 14:22:34 -0700236 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700237 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
238 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700240
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800241 private final BroadcastReceiver mCloseSystemDialogsReceiver
242 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private LayoutInflater mInflater;
246
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700248 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800249 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800251 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700252 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Sunny Goyalffe83f12014-08-14 17:39:34 -0700254 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Adam Cohen091440a2015-03-18 14:16:05 -0700257 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800258 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800259 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700260
Michael Jurka0280c3b2010-09-17 15:00:07 -0700261 private int[] mTmpAddItemCellCoordinates = new int[2];
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private FolderInfo mFolderInfo;
264
Winson Chung3d503fb2011-07-13 17:25:49 -0700265 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800266 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700267
Michael Jurka838a4ca2011-02-07 13:33:06 -0800268 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700269
Winson Chungc51db6a2011-10-05 11:44:49 -0700270 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
272 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the widget tray screen.
276 private WidgetsContainerView mWidgetsView;
277
Winson Chungf0ea4d32011-06-06 14:27:16 -0700278 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800279 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
283 // scroll issues (because the workspace may not have been measured yet) and extra work.
284 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
285 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
287 private SpannableStringBuilder mDefaultKeySsb = null;
288
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800291 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private boolean mRestoring;
293 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700294 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700297 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 private Bundle mSavedInstanceState;
300
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700305 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800306 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309
Sunny Goyale2df0622015-04-24 11:27:00 -0700310 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
Adam Cohen61f560d2013-09-30 15:58:20 -0700312 private View.OnTouchListener mHapticFeedbackTouchListener;
313
Adam Cohended9f8d2010-11-03 13:25:16 -0700314 // Related to the auto-advancing of widgets
315 private final int ADVANCE_MSG = 1;
316 private final int mAdvanceInterval = 20000;
317 private final int mAdvanceStagger = 250;
318 private long mAutoAdvanceSentTime;
319 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700320 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700321 new HashMap<View, AppWidgetProviderInfo>();
322
Winson Chung400438b2011-01-16 17:53:48 -0800323 // Determines how long to wait after a rotation before restoring the screen orientation to
324 // match the sensor state.
325 private final int mRestoreScreenOrientationDelay = 500;
326
Adam Cohen091440a2015-03-18 14:16:05 -0700327 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 // Holds the page that we need to animate to, and the icon views that we need to animate up
344 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700345 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700346 private Bitmap mFolderIconBitmap;
347 private Canvas mFolderIconCanvas;
348 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349
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;
395
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700396 FocusIndicatorView mFocusHandler;
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 @Override
399 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700400 if (DEBUG_STRICT_MODE) {
401 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
402 .detectDiskReads()
403 .detectDiskWrites()
404 .detectNetwork() // or .detectAll() for all detectable problems
405 .penaltyLog()
406 .build());
407 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
408 .detectLeakedSqlLiteObjects()
409 .detectLeakedClosableObjects()
410 .penaltyLog()
411 .penaltyDeath()
412 .build());
413 }
414
Adam Cohen9211d422014-10-07 18:14:53 -0700415 if (mLauncherCallbacks != null) {
416 mLauncherCallbacks.preOnCreate();
417 }
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400420
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400421 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400422 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700423 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700424
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700426 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700427
428 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400429 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700430 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700431 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mModel = app.setLauncher(this);
433 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700434 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400435 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800437 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Daniel Sandlerff02d492013-08-05 02:12:05 -0400439 mStats = new Stats(this);
440
Sunny Goyalffe83f12014-08-14 17:39:34 -0700441 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
444 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700446 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
447 // this also ensures that any synchronous binding below doesn't re-trigger another
448 // LauncherModel load.
449 mPaused = false;
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200452 android.os.Debug.startMethodTracing(
453 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100460 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800462 registerContentObservers();
463
Joe Onorato7c312c12009-08-13 21:36:53 -0700464 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 mSavedState = savedInstanceState;
467 restoreState(mSavedState);
468
469 if (PROFILE_STARTUP) {
470 android.os.Debug.stopMethodTracing();
471 }
472
473 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700474 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 // If the user leaves launcher, then we should just load items asynchronously when
476 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500477 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 } else {
479 // We only load the page synchronously if the user rotates (or triggers a
480 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800481 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484
485 // For handling default keys
486 mDefaultKeySsb = new SpannableStringBuilder();
487 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800488
489 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
490 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800491
Adam Cohenf9426d52012-06-04 17:26:21 -0700492 // On large interfaces, we want the screen to auto-rotate based on the current orientation
493 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700494
Adam Cohen9211d422014-10-07 18:14:53 -0700495 if (mLauncherCallbacks != null) {
496 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 if (mLauncherCallbacks.hasLauncherOverlay()) {
498 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700499 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
500 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
501 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700502 mWorkspace.setLauncherOverlay(mLauncherOverlay);
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700505
506 if (shouldShowIntroScreen()) {
507 showIntroScreen();
508 } else {
509 showFirstRunActivity();
510 showFirstRunClings();
511 }
Adam Cohen9211d422014-10-07 18:14:53 -0700512 }
513
514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
523 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
524 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700525 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
526 @Override
527 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700528 if (LOGD) {
529 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
530 }
Winson Chung0f785722015-04-08 10:27:49 -0700531 mAppsView.setFixedBounds(Launcher.this, bounds);
532 }
533
534 @Override
535 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700536 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
537 // Dismiss All Apps if we aren't already paused/invisible
538 if (!mPaused) {
539 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
540 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
541 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700542 }
Winson Chung0f785722015-04-08 10:27:49 -0700543 }
544 });
Adam Cohen9211d422014-10-07 18:14:53 -0700545 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700546 }
547
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700548 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700549 public void onLauncherProviderChange() {
550 if (mLauncherCallbacks != null) {
551 mLauncherCallbacks.onLauncherProviderChange();
552 }
553 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700554
Adam Cohen9211d422014-10-07 18:14:53 -0700555 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700556 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700557 if (mLauncherCallbacks != null) {
558 return mLauncherCallbacks.hasCustomContentToLeft();
559 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700560 return false;
561 }
562
Dave Hawkeya8881582013-09-17 15:55:33 -0600563 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500564 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600565 * {@link #addToCustomContentPage}. This will only be invoked if
566 * {@link #hasCustomContentToLeft()} is {@code true}.
567 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500568 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700569 if (mLauncherCallbacks != null) {
570 mLauncherCallbacks.populateCustomContentContainer();
571 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600572 }
573
574 /**
575 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
576 * ensure the custom content page is added or removed if necessary.
577 */
578 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600579 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600580 // Not bound yet, wait for bindScreens to be called.
581 return;
582 }
583
584 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
585 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500586 mWorkspace.createCustomContentContainer();
587 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600588 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
589 mWorkspace.removeCustomContentPage();
590 }
591 }
592
Adam Cohen091440a2015-03-18 14:16:05 -0700593 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 if (sLocaleConfiguration == null) {
595 new AsyncTask<Void, Void, LocaleConfiguration>() {
596 @Override
597 protected LocaleConfiguration doInBackground(Void... unused) {
598 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
599 readConfiguration(Launcher.this, localeConfiguration);
600 return localeConfiguration;
601 }
602
603 @Override
604 protected void onPostExecute(LocaleConfiguration result) {
605 sLocaleConfiguration = result;
606 checkForLocaleChange(); // recursive, but now with a locale configuration
607 }
608 }.execute();
609 return;
610 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700611
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 final Configuration configuration = getResources().getConfiguration();
613
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 final String locale = configuration.locale.toString();
616
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700617 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 final int mcc = configuration.mcc;
619
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 final int mnc = configuration.mnc;
622
Romain Guy84f296c2009-11-04 15:00:44 -0800623 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624
Romain Guy84f296c2009-11-04 15:00:44 -0800625 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700626 sLocaleConfiguration.locale = locale;
627 sLocaleConfiguration.mcc = mcc;
628 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700629
Joe Onorato0589f0f2010-02-08 13:44:00 -0800630 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700631
632 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100633 new AsyncTask<Void, Void, Void>() {
634 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700635 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100636 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700637 }
Michael Jurka43467462013-11-14 12:03:58 +0100638 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700639 }
640 }
641
Adam Cohen091440a2015-03-18 14:16:05 -0700642 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700643 public String locale;
644 public int mcc = -1;
645 public int mnc = -1;
646 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700647
Adam Cohen091440a2015-03-18 14:16:05 -0700648 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700649 DataInputStream in = null;
650 try {
Helena Josol28db2802014-10-09 17:04:09 +0100651 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700652 configuration.locale = in.readUTF();
653 configuration.mcc = in.readInt();
654 configuration.mnc = in.readInt();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 // Ignore
659 } finally {
660 if (in != null) {
661 try {
662 in.close();
663 } catch (IOException e) {
664 // Ignore
665 }
666 }
667 }
668 }
669
Adam Cohen091440a2015-03-18 14:16:05 -0700670 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700671 DataOutputStream out = null;
672 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100673 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100674 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700675 out.writeUTF(configuration.locale);
676 out.writeInt(configuration.mcc);
677 out.writeInt(configuration.mnc);
678 out.flush();
679 } catch (FileNotFoundException e) {
680 // Ignore
681 } catch (IOException e) {
682 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100683 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700684 } finally {
685 if (out != null) {
686 try {
687 out.close();
688 } catch (IOException e) {
689 // Ignore
690 }
691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
693 }
694
Anton Hanssona2f665f2013-09-26 12:18:32 +0100695 public Stats getStats() {
696 return mStats;
697 }
698
Bjorn Bringertc459e522013-06-07 19:36:01 +0100699 public LayoutInflater getInflater() {
700 return mInflater;
701 }
702
Hyunyoung Song3f471442015-04-08 19:01:34 -0700703 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700704 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
705 // that is subsequently removed from the workspace in startBinding().
706 return !mModel.isLoadingWorkspace();
707 }
708
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800709 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000710 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100711 if (Build.VERSION.SDK_INT >= 17) {
712 return View.generateViewId();
713 } else {
714 // View.generateViewId() is not available. The following fallback logic is a copy
715 // of its implementation.
716 for (;;) {
717 final int result = sNextGeneratedId.get();
718 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
719 int newValue = result + 1;
720 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
721 if (sNextGeneratedId.compareAndSet(result, newValue)) {
722 return result;
723 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000724 }
725 }
726 }
727
728 public int getViewIdForItem(ItemInfo info) {
729 // This cast is safe given the > 2B range for int.
730 int itemId = (int) info.id;
731 if (mItemIdToViewId.containsKey(itemId)) {
732 return mItemIdToViewId.get(itemId);
733 }
734 int viewId = generateViewId();
735 mItemIdToViewId.put(itemId, viewId);
736 return viewId;
737 }
738
739 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700740 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
741 * a configuration step, this allows the proper animations to run after other transitions.
742 */
Adam Cohendb364c32014-05-20 14:23:40 -0700743 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700744 long screenId = args.screenId;
745 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
746 // When the screen id represents an actual screen (as opposed to a rank) we make sure
747 // that the drop page actually exists.
748 screenId = ensurePendingDropLayoutExists(args.screenId);
749 }
Adam Cohendb364c32014-05-20 14:23:40 -0700750
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700753 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700755 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800756 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700757 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700759 case REQUEST_RECONFIGURE_APPWIDGET:
760 completeRestoreAppWidget(args.appWidgetId);
761 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 }
Michael Jurka27614d22012-04-02 06:40:22 -0700763 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
764 // if you turned the screen off and then back while in All Apps, Launcher would not
765 // return to the workspace. Clearing mAddInfo.container here fixes this issue
766 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700767 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800768 }
769
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800770 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700772 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700773 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700774 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800775 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700776
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 Runnable exitSpringLoaded = new Runnable() {
778 @Override
779 public void run() {
780 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
781 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
782 }
783 };
784
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
788 if (resultCode == RESULT_CANCELED) {
789 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700790 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700792 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800793 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700795 }
796 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200797 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
798 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700799 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200800 }
801 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700802 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200803
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
805 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700806
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 // We have special handling for widgets
809 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800810 final int appWidgetId;
811 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
812 : -1;
813 if (widgetId < 0) {
814 appWidgetId = pendingAddWidgetId;
815 } else {
816 appWidgetId = widgetId;
817 }
818
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800820 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700821 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
822 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700823 result = RESULT_CANCELED;
824 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700825 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 @Override
827 public void run() {
828 exitSpringLoadedDragModeDelayed(false, 0, null);
829 }
830 };
Adam Cohendb364c32014-05-20 14:23:40 -0700831 if (workspaceLocked) {
832 // No need to remove the empty screen if we're mid-binding, as the
833 // the bind will not add the empty screen.
834 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
835 } else {
836 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
838 }
Winson Chung5aaab772012-04-27 15:24:02 -0700839 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700840 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700841 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
842 // When the screen id represents an actual screen (as opposed to a rank)
843 // we make sure that the drop page actually exists.
844 mPendingAddInfo.screenId =
845 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
846 }
Adam Cohendb364c32014-05-20 14:23:40 -0700847 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
848
849 dropLayout.setDropPending(true);
850 final Runnable onComplete = new Runnable() {
851 @Override
852 public void run() {
853 completeTwoStageWidgetDrop(resultCode, appWidgetId);
854 dropLayout.setDropPending(false);
855 }
856 };
857 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
858 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
859 } else {
860 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
861 mPendingAddInfo);
862 sPendingAddItem = args;
863 }
Winson Chung5aaab772012-04-27 15:24:02 -0700864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 return;
866 }
867
Sunny Goyalff572272014-07-23 13:58:07 -0700868 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
869 if (resultCode == RESULT_OK) {
870 // Update the widget view.
871 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
872 pendingAddWidgetId, mPendingAddInfo);
873 if (workspaceLocked) {
874 sPendingAddItem = args;
875 } else {
876 completeAdd(args);
877 }
878 }
879 // Leave the widget in the pending state if the user canceled the configure.
880 return;
881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 // The pattern used here is that a user PICKs a specific application,
884 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
887 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700888 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700889 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
890 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800891 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700892 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800893 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700894 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700895 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
896 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
Adam Cohen00074722013-10-11 17:46:09 -0700898 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700899 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700900 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800903
904 }
905
906 @Override
907 protected void onActivityResult(
908 final int requestCode, final int resultCode, final Intent data) {
909 handleActivityResult(requestCode, resultCode, data);
910 if (mLauncherCallbacks != null) {
911 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
912 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 }
914
Adam Cohendb364c32014-05-20 14:23:40 -0700915 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
916 appWidgetId, ItemInfo info) {
917 PendingAddArguments args = new PendingAddArguments();
918 args.requestCode = requestCode;
919 args.intent = data;
920 args.container = info.container;
921 args.screenId = info.screenId;
922 args.cellX = info.cellX;
923 args.cellY = info.cellY;
924 args.appWidgetId = appWidgetId;
925 return args;
926 }
927
928 /**
929 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
930 *
931 * @param screenId the screen id to check
932 * @return the new screen, or screenId if it exists
933 */
934 private long ensurePendingDropLayoutExists(long screenId) {
935 CellLayout dropLayout =
936 (CellLayout) mWorkspace.getScreenWithId(screenId);
937 if (dropLayout == null) {
938 // it's possible that the add screen was removed because it was
939 // empty and a re-bind occurred
940 mWorkspace.addExtraEmptyScreen();
941 return mWorkspace.commitExtraEmptyScreen();
942 } else {
943 return screenId;
944 }
945 }
946
Adam Cohen091440a2015-03-18 14:16:05 -0700947 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700948 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 Runnable onCompleteRunnable = null;
951 int animationType = 0;
952
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 if (resultCode == RESULT_OK) {
955 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
956 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 mPendingAddWidgetInfo);
958 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 onCompleteRunnable = new Runnable() {
960 @Override
961 public void run() {
962 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700964 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
965 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800966 }
967 };
968 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800969 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800970 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800971 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700972 if (mDragLayer.getAnimatedView() != null) {
973 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
974 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
975 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700976 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700977 // The animated view may be null in the case of a rotation during widget configuration
978 onCompleteRunnable.run();
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981
982 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700983 protected void onStop() {
984 super.onStop();
985 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700986
987 if (mLauncherCallbacks != null) {
988 mLauncherCallbacks.onStop();
989 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700990 }
991
992 @Override
993 protected void onStart() {
994 super.onStart();
995 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.onStart();
999 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001000 }
1001
1002 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001004 long startTime = 0;
1005 if (DEBUG_RESUME_TIME) {
1006 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001007 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001008 }
Adam Cohen9211d422014-10-07 18:14:53 -07001009
1010 if (mLauncherCallbacks != null) {
1011 mLauncherCallbacks.preOnResume();
1012 }
1013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001015
Winson Chung4a2afa32012-07-19 14:53:05 -07001016 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001017 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001018 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001019 } else if (mOnResumeState == State.APPS) {
1020 showAppsView(false /* animated */, false /* resetListToTop */);
1021 } else if (mOnResumeState == State.WIDGETS) {
1022 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001023 }
1024 mOnResumeState = State.NONE;
1025
Winson Chungcd99cd32015-04-29 11:03:24 -07001026 // Restore the apps state if we are in all apps
Winson Chung83f59ab2015-05-05 17:21:58 -07001027 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mState == State.APPS) {
Winson Chungcd99cd32015-04-29 11:03:24 -07001028 if (mLauncherCallbacks != null) {
1029 mLauncherCallbacks.onAllAppsShown();
1030 }
1031 }
1032
Craig Mautner360310b2012-10-26 15:13:08 -07001033 // Background was set to gradient in onPause(), restore to black if in all apps.
1034 setWorkspaceBackground(mState == State.WORKSPACE);
1035
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001036 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001037 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001038 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001039 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001041 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001043 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001044 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1045 // execute them here
1046 long startTimeCallbacks = 0;
1047 if (DEBUG_RESUME_TIME) {
1048 startTimeCallbacks = System.currentTimeMillis();
1049 }
1050
Michael Jurka1e2f4652013-07-08 18:03:46 -07001051 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1052 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001053 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001054 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001055 if (DEBUG_RESUME_TIME) {
1056 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1057 (System.currentTimeMillis() - startTimeCallbacks));
1058 }
Michael Jurka447bf842013-05-15 14:52:15 +02001059 }
Michael Jurka54554252013-08-01 12:52:23 +02001060 if (mOnResumeCallbacks.size() > 0) {
1061 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1062 mOnResumeCallbacks.get(i).run();
1063 }
1064 mOnResumeCallbacks.clear();
1065 }
Winson Chunge4e50662012-01-23 14:45:13 -08001066
1067 // Reset the pressed state of icons that were locked in the press state while activities
1068 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001069 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001070 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001071 mWaitingForResume.setStayPressed(false);
1072 }
Winson Chung82963d52013-10-09 11:20:57 -07001073
Adam Cohen06dff352012-06-01 17:17:08 -07001074 // It is possible that widgets can receive updates while launcher is not in the foreground.
1075 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1076 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1077 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001078 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001079 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001080
Michael Jurka447bf842013-05-15 14:52:15 +02001081 if (DEBUG_RESUME_TIME) {
1082 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1083 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001084
1085 if (mWorkspace.getCustomContentCallbacks() != null) {
1086 // If we are resuming and the custom content is the current page, we call onShow().
1087 // It is also poassible that onShow will instead be called slightly after first layout
1088 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1089 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001090 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001091 }
1092 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001093 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001094 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001095
Sunny Goyal756adbc2015-04-16 15:20:51 -07001096 if (!isWorkspaceLoading()) {
1097 // Process any items that were added while Launcher was away.
1098 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1099 }
Adam Cohen9211d422014-10-07 18:14:53 -07001100
1101 if (mLauncherCallbacks != null) {
1102 mLauncherCallbacks.onResume();
1103 }
Adam Cohen06dff352012-06-01 17:17:08 -07001104 }
1105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001107 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001108 // Ensure that items added to Launcher are queued until Launcher returns
1109 InstallShortcutReceiver.enableInstallQueue();
1110
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001112 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001113 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001114 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001115
1116 // We call onHide() aggressively. The custom content callbacks should be able to
1117 // debounce excess onHide calls.
1118 if (mWorkspace.getCustomContentCallbacks() != null) {
1119 mWorkspace.getCustomContentCallbacks().onHide();
1120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
Adam Cohen9211d422014-10-07 18:14:53 -07001122 if (mLauncherCallbacks != null) {
1123 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001125 }
1126
1127 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001128 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1129 // by a onResume or by scrolling otherwise.
1130 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001131
1132 // Custom content is completely hidden
1133 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001134
1135 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1136 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001137
1138 // Indicates whether the user is allowed to scroll away from the custom content.
1139 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001140 }
1141
Adam Cohenc2d6e892014-10-16 09:49:24 -07001142 public interface LauncherOverlay {
1143
1144 /**
1145 * Touch interaction leading to overscroll has begun
1146 */
1147 public void onScrollInteractionBegin();
1148
1149 /**
1150 * Touch interaction related to overscroll has ended
1151 */
1152 public void onScrollInteractionEnd();
1153
1154 /**
1155 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1156 * screen (or in the case of RTL, the rightmost screen).
1157 */
1158 public void onScrollChange(int progress, boolean rtl);
1159
1160 /**
1161 * Screen has stopped scrolling
1162 */
1163 public void onScrollSettled();
1164
1165 /**
1166 * This method can be called by the Launcher in order to force the LauncherOverlay
1167 * to exit fully immersive mode.
1168 */
1169 public void forceExitFullImmersion();
1170 }
1171
Winson Chung0f785722015-04-08 10:27:49 -07001172 public interface LauncherAppsCallbacks {
1173 /**
1174 * Updates launcher to the available space that AllApps can take so as not to overlap with
1175 * any other views.
1176 */
1177 public void onAllAppsBoundsChanged(Rect bounds);
1178
1179 /**
1180 * Called to dismiss all apps if it is showing.
1181 */
1182 public void dismissAllApps();
1183 }
1184
Adam Cohenc2d6e892014-10-16 09:49:24 -07001185 public interface LauncherOverlayCallbacks {
1186 /**
1187 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1188 * however it doesn't modify any state within the launcher.
1189 */
1190 public boolean canEnterFullImmersion();
1191
1192 /**
1193 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1194 * eg. by occupying the full screen and handling all touch events.
1195 *
1196 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1197 * case, Launcher will modify any necessary state and assumes the overlay is
1198 * handling all interaction. If false, the LauncherOverlay should cancel any
1199 *
1200 */
1201 public boolean enterFullImmersion();
1202
1203 /**
1204 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1205 * full control over UI and state.
1206 */
1207 public void exitFullImmersion();
1208 }
1209
1210 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1211
1212 @Override
1213 public boolean canEnterFullImmersion() {
1214 return mState == State.WORKSPACE;
1215 }
1216
1217 @Override
1218 public boolean enterFullImmersion() {
1219 if (mState == State.WORKSPACE) {
1220 // When fully immersed, disregard any touches which fall through.
1221 mDragLayer.setBlockTouch(true);
1222 return true;
1223 }
1224 return false;
1225 }
1226
1227 @Override
1228 public void exitFullImmersion() {
1229 mDragLayer.setBlockTouch(false);
1230 }
1231 }
1232
Jorim Jaggid017f882014-01-14 17:08:48 -08001233 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001234 if (mLauncherCallbacks != null) {
1235 return mLauncherCallbacks.hasSettings();
1236 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001237 return false;
1238 }
1239
Adam Cohen9211d422014-10-07 18:14:53 -07001240 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001241 CustomContentCallbacks callbacks, String description) {
1242 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001243 }
1244
Adam Cohenedb40762013-07-18 16:45:45 -07001245 // The custom content needs to offset its content to account for the QSB
1246 public int getTopOffsetForCustomContent() {
1247 return mWorkspace.getPaddingTop();
1248 }
1249
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001250 @Override
1251 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001252 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001253 if (mModel.isCurrentCallbacks(this)) {
1254 mModel.stopLoader();
1255 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001256 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1257
Romain Guy13c2e7b2010-03-10 19:45:00 -08001258 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001259 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001260
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001261 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001262 @Override
1263 public void onWindowFocusChanged(boolean hasFocus) {
1264 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001265 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001266
1267 if (mLauncherCallbacks != null) {
1268 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1269 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001270 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 private boolean acceptFilter() {
1273 final InputMethodManager inputManager = (InputMethodManager)
1274 getSystemService(Context.INPUT_METHOD_SERVICE);
1275 return !inputManager.isFullscreenMode();
1276 }
1277
1278 @Override
1279 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001280 final int uniChar = event.getUnicodeChar();
1281 final boolean handled = super.onKeyDown(keyCode, event);
1282 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1283 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1285 keyCode, event);
1286 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001287 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001288 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 // showSearchDialog()
1290 // If there are multiple keystrokes before the search dialog takes focus,
1291 // onSearchRequested() will be called for every keystroke,
1292 // but it is idempotent, so it's fine.
1293 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
1295 }
1296
Joe Onorato8a9625e2010-01-28 15:55:35 -08001297 // Eat the long press event so the keyboard doesn't come up.
1298 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1299 return true;
1300 }
1301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 return handled;
1303 }
1304
Karl Rosaen138a0412009-04-23 19:00:21 -07001305 private String getTypedText() {
1306 return mDefaultKeySsb.toString();
1307 }
1308
1309 private void clearTypedText() {
1310 mDefaultKeySsb.clear();
1311 mDefaultKeySsb.clearSpans();
1312 Selection.setSelection(mDefaultKeySsb, 0);
1313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001316 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1317 * State
1318 */
1319 private static State intToState(int stateOrdinal) {
1320 State state = State.WORKSPACE;
1321 final State[] stateValues = State.values();
1322 for (int i = 0; i < stateValues.length; i++) {
1323 if (stateValues[i].ordinal() == stateOrdinal) {
1324 state = stateValues[i];
1325 break;
1326 }
1327 }
1328 return state;
1329 }
1330
1331 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 * Restores the previous state, if it exists.
1333 *
1334 * @param savedState The previous state.
1335 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001336 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 private void restoreState(Bundle savedState) {
1338 if (savedState == null) {
1339 return;
1340 }
1341
Michael Jurka883f55b2010-10-21 15:47:14 -07001342 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001343 if (state == State.APPS || state == State.WIDGETS) {
1344 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001345 }
1346
Adam Cohen21cd0022013-10-09 18:57:02 -07001347 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1348 PagedView.INVALID_RESTORE_PAGE);
1349 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001350 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001354 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1357 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001358 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1360 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001361 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1362 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001363 AppWidgetProviderInfo info = savedState.getParcelable(
1364 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1365 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001366 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001367 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 mRestoring = true;
1369 }
1370
1371 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1372 if (renameFolder) {
1373 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001374 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 mRestoring = true;
1376 }
Winson Chunga12a2502010-12-20 14:41:35 -08001377
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001378 mItemIdToViewId = (HashMap<Integer, Integer>)
1379 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381
1382 /**
1383 * Finds all the views we need and configure them properly.
1384 */
1385 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001386 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001387
Craig Mautner360310b2012-10-26 15:13:08 -07001388 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001389 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001390 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1391 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001392 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001393 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001394
John Spurlock77e1f472013-09-11 10:09:51 -04001395 mLauncherView.setSystemUiVisibility(
1396 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001397 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398
Winson Chung4c98d922011-05-31 16:50:48 -07001399 // Setup the drag layer
1400 mDragLayer.setup(this, dragController);
1401
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 // Setup the hotseat
1403 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1404 if (mHotseat != null) {
1405 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001406 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001407 }
1408
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001410 View widgetButton = findViewById(R.id.widget_button);
1411 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 @Override
1413 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001414 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001415 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001416 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001417 }
1418 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001419 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1420
1421 View wallpaperButton = findViewById(R.id.wallpaper_button);
1422 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423 @Override
1424 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001425 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001426 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001427 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001428 }
1429 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001430 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1431
1432 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001433 if (hasSettings()) {
1434 settingsButton.setOnClickListener(new OnClickListener() {
1435 @Override
1436 public void onClick(View arg0) {
1437 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001438 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001439 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001440 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001441 });
1442 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1443 } else {
1444 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001445 }
1446
Adam Cohendbdff6b2013-09-18 19:09:15 -07001447 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001448
Winson Chung4c98d922011-05-31 16:50:48 -07001449 // Setup the workspace
1450 mWorkspace.setHapticFeedbackEnabled(false);
1451 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001452 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001453 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001454
Winson Chungf0ea4d32011-06-06 14:27:16 -07001455 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001456 mSearchDropTargetBar = (SearchDropTargetBar)
1457 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001458
Winson Chungb745afb2015-03-02 11:51:23 -08001459 // Setup Apps
1460 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001461 if (isAllAppsSearchOverridden()) {
1462 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001463 }
Winson Chungb745afb2015-03-02 11:51:23 -08001464
Winson Chungf0ea4d32011-06-06 14:27:16 -07001465 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001466 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001467
Winson Chung3d503fb2011-07-13 17:25:49 -07001468 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001469 dragController.setDragScoller(mWorkspace);
1470 dragController.setScrollView(mDragLayer);
1471 dragController.setMoveTarget(mWorkspace);
1472 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001473 if (mSearchDropTargetBar != null) {
1474 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001475 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001476 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001477
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001478 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001479 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001480 mWeightWatcher = new WeightWatcher(this);
1481 mWeightWatcher.setAlpha(0.5f);
1482 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001483 new FrameLayout.LayoutParams(
1484 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001485 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 Gravity.BOTTOM)
1487 );
Adam Cohen39a06042013-07-19 14:30:12 -07001488
1489 boolean show = shouldShowWeightWatcher();
1490 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001495 * Sets the all apps button. This method is called from {@link Hotseat}.
1496 */
1497 public void setAllAppsButton(View allAppsButton) {
1498 mAllAppsButton = allAppsButton;
1499 }
1500
1501 public View getAllAppsButton() {
1502 return mAllAppsButton;
1503 }
1504
1505 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 * Creates a view representing a shortcut.
1507 *
1508 * @param info The data structure describing the shortcut.
1509 *
1510 * @return A View inflated from R.layout.application.
1511 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001512 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001514 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
1517 /**
1518 * Creates a view representing a shortcut inflated from the specified resource.
1519 *
1520 * @param layoutResId The id of the XML layout used to create the shortcut.
1521 * @param parent The group the shortcut belongs to.
1522 * @param info The data structure describing the shortcut.
1523 *
1524 * @return A View inflated from layoutResId.
1525 */
Adam Cohen59400422014-03-05 18:07:04 -08001526 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001527 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001528 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001530 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 return favorite;
1532 }
1533
1534 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 * Add a shortcut to the workspace.
1536 *
1537 * @param data The intent describing the shortcut.
1538 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001541 int cellY) {
1542 int[] cellXY = mTmpAddItemCellCoordinates;
1543 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001547
Sunny Goyal2350bc92014-10-14 16:42:54 -07001548 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001549 if (info == null) {
1550 return;
1551 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001552 final View view = createShortcut(info);
1553
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554 // First we check if we already know the exact location where we want to add this item.
1555 if (cellX >= 0 && cellY >= 0) {
1556 cellXY[0] = cellX;
1557 cellXY[1] = cellY;
1558 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001559
1560 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001561 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001562 true, null,null)) {
1563 return;
1564 }
1565 DragObject dragObject = new DragObject();
1566 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001567 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1568 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001569 return;
1570 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001572 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001573 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001574 foundCellSpan = (result != null);
1575 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001577 }
1578
1579 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001580 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001581 return;
1582 }
1583
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001584 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
1590 }
1591
Adam Cohen2f093b62012-04-30 18:59:53 -07001592 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1593 int minHeight) {
1594 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001595 // We want to account for the extra amount of padding that we are adding to the widget
1596 // to ensure that it gets the full amount of space that it has requested
1597 int requiredWidth = minWidth + padding.left + padding.right;
1598 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001599 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001600 }
1601
Adam Cohen2f093b62012-04-30 18:59:53 -07001602 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1603 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001604 }
1605
Adam Cohen2f093b62012-04-30 18:59:53 -07001606 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1607 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001608 }
1609
Adam Cohen2f093b62012-04-30 18:59:53 -07001610 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1611 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001612 }
1613
Adam Cohen2f093b62012-04-30 18:59:53 -07001614 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1615 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001616 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001617 }
1618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001620 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001622 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 */
Adam Cohen091440a2015-03-18 14:16:05 -07001624 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001625 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1626
1627 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001628 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001629 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1630 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 }
Romain Guycbb89e42009-06-08 15:52:54 -07001632
Adam Cohen59400422014-03-05 18:07:04 -08001633 if (appWidgetInfo.isCustomWidget) {
1634 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001635 }
1636
Adam Cohen59400422014-03-05 18:07:04 -08001637 LauncherAppWidgetInfo launcherInfo;
1638 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1639 launcherInfo.spanX = info.spanX;
1640 launcherInfo.spanY = info.spanY;
1641 launcherInfo.minSpanX = info.minSpanX;
1642 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001643 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001646 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647
1648 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001649 if (hostView == null) {
1650 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001651 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1652 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001653 } else {
1654 // The AppWidgetHostView has already been inflated and instantiated
1655 launcherInfo.hostView = hostView;
1656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001658 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001659 launcherInfo.notifyWidgetSizeChanged(this);
1660
Adam Cohen59400422014-03-05 18:07:04 -08001661 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1662 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001663
1664 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001666 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
Romain Guycbb89e42009-06-08 15:52:54 -07001668
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1670 @Override
1671 public void onReceive(Context context, Intent intent) {
1672 final String action = intent.getAction();
1673 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1674 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001675 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001676 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001677
Winson Chungc100e8e2011-08-09 16:02:43 -07001678 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001679 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001680 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001681 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001682 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001683 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001684 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1685 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001686 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001687 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1688 mModel.resetLoadedState(false, true);
1689 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1690 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1691 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1692 mModel.resetLoadedState(false, true);
1693 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1694 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1695 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 }
1697 }
1698 };
1699
1700 @Override
1701 public void onAttachedToWindow() {
1702 super.onAttachedToWindow();
1703
1704 // Listen for broadcasts related to user-presence
1705 final IntentFilter filter = new IntentFilter();
1706 filter.addAction(Intent.ACTION_SCREEN_OFF);
1707 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001708 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001709 if (ENABLE_DEBUG_INTENTS) {
1710 filter.addAction(DebugIntents.DELETE_DATABASE);
1711 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1712 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001714 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001715 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001716 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 mVisible = true;
1718 }
1719
Adam Cohen0b395352014-06-09 22:54:36 +00001720 /**
1721 * Sets up transparent navigation and status bars in LMP.
1722 * This method is a no-op for other platform versions.
1723 */
Sunny Goyald0091012015-01-20 15:55:34 -08001724 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001725 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001726 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001727 Window window = getWindow();
1728 window.getAttributes().systemUiVisibility |=
1729 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1730 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1731 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1732 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1733 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1734 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1735 window.setStatusBarColor(Color.TRANSPARENT);
1736 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001737 }
1738 }
1739
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 @Override
1741 public void onDetachedFromWindow() {
1742 super.onDetachedFromWindow();
1743 mVisible = false;
1744
Adam Cohend113e0c2010-11-11 10:48:05 -08001745 if (mAttached) {
1746 unregisterReceiver(mReceiver);
1747 mAttached = false;
1748 }
Winson Chungb745afb2015-03-02 11:51:23 -08001749 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751
1752 public void onWindowVisibilityChanged(int visibility) {
1753 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001754 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001755 // The following code used to be in onResume, but it turns out onResume is called when
1756 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1757 // is a more appropriate event to handle
1758 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001759 if (!mWorkspaceLoading) {
1760 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001761 // We want to let Launcher draw itself at least once before we force it to build
1762 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001763 // apps is nice and speedy.
1764 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001765 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001766 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001767 if (mStarted) return;
1768 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001769 // We delay the layer building a bit in order to give
1770 // other message processing a time to run. In particular
1771 // this avoids a delay in hiding the IME if it was
1772 // currently shown, because doing that may involve
1773 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001774 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001775 final ViewTreeObserver.OnDrawListener listener = this;
1776 mWorkspace.post(new Runnable() {
1777 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001778 if (mWorkspace != null &&
1779 mWorkspace.getViewTreeObserver() != null) {
1780 mWorkspace.getViewTreeObserver().
1781 removeOnDrawListener(listener);
1782 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001783 }
1784 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001785 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001786 }
1787 });
1788 }
1789 clearTypedText();
1790 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 }
1792
Adam Cohen091440a2015-03-18 14:16:05 -07001793 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 mHandler.removeMessages(ADVANCE_MSG);
1795 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1796 mHandler.sendMessageDelayed(msg, delay);
1797 mAutoAdvanceSentTime = System.currentTimeMillis();
1798 }
1799
Adam Cohen091440a2015-03-18 14:16:05 -07001800 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1802 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1803 mAutoAdvanceRunning = autoAdvanceRunning;
1804 if (autoAdvanceRunning) {
1805 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1806 sendAdvanceMessage(delay);
1807 } else {
1808 if (!mWidgetsToAdvance.isEmpty()) {
1809 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1810 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1811 }
1812 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001813 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001814 }
1815 }
1816 }
1817
1818 private final Handler mHandler = new Handler() {
1819 @Override
1820 public void handleMessage(Message msg) {
1821 if (msg.what == ADVANCE_MSG) {
1822 int i = 0;
1823 for (View key: mWidgetsToAdvance.keySet()) {
1824 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1825 final int delay = mAdvanceStagger * i;
1826 if (v instanceof Advanceable) {
1827 postDelayed(new Runnable() {
1828 public void run() {
1829 ((Advanceable) v).advance();
1830 }
1831 }, delay);
1832 }
1833 i++;
1834 }
1835 sendAdvanceMessage(mAdvanceInterval);
1836 }
1837 }
1838 };
1839
1840 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001841 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1843 if (v instanceof Advanceable) {
1844 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001845 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001846 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001847 }
1848 }
1849
1850 void removeWidgetToAutoAdvance(View hostView) {
1851 if (mWidgetsToAdvance.containsKey(hostView)) {
1852 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001853 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001854 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001855 }
1856
Joe Onorato9c1289c2009-08-17 11:03:03 -04001857 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001858 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 launcherInfo.hostView = null;
1860 }
1861
Hyunyoung Song3f471442015-04-08 19:01:34 -07001862 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001863 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1864 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001865 }
1866
Winson Chunga6945242014-01-08 14:04:34 -08001867 public DragLayer getDragLayer() {
1868 return mDragLayer;
1869 }
1870
Winson Chungb745afb2015-03-02 11:51:23 -08001871 public AppsContainerView getAppsView() {
1872 return mAppsView;
1873 }
1874
Hyunyoung Song3f471442015-04-08 19:01:34 -07001875 public WidgetsContainerView getWidgetsView() {
1876 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001877 }
1878
Winson Chunga6945242014-01-08 14:04:34 -08001879 public Workspace getWorkspace() {
1880 return mWorkspace;
1881 }
1882
1883 public Hotseat getHotseat() {
1884 return mHotseat;
1885 }
1886
Jorim Jaggid017f882014-01-14 17:08:48 -08001887 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001888 return mOverviewPanel;
1889 }
1890
1891 public SearchDropTargetBar getSearchBar() {
1892 return mSearchDropTargetBar;
1893 }
1894
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001895 public LauncherAppWidgetHost getAppWidgetHost() {
1896 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
Romain Guycbb89e42009-06-08 15:52:54 -07001898
Winson Chunga9abd0e2010-10-27 17:18:37 -07001899 public LauncherModel getModel() {
1900 return mModel;
1901 }
1902
Winson Chunga6945242014-01-08 14:04:34 -08001903 protected SharedPreferences getSharedPrefs() {
1904 return mSharedPrefs;
1905 }
1906
Bjorn Bringertc459e522013-06-07 19:36:01 +01001907 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001908 getWindow().closeAllPanels();
1909
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001910 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001911 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 @Override
1915 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001916 long startTime = 0;
1917 if (DEBUG_RESUME_TIME) {
1918 startTime = System.currentTimeMillis();
1919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 super.onNewIntent(intent);
1921
1922 // Close the menu
1923 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001924 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001925 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001926
Adam Cohened307df2013-10-02 09:37:31 -07001927 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1928 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1929 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001930
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 if (mWorkspace == null) {
1932 // Can be cases where mWorkspace is null, this prevents a NPE
1933 return;
1934 }
1935 Folder openFolder = mWorkspace.getOpenFolder();
1936 // In all these cases, only animate if we're already on home
1937 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001938
1939 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1940 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001941 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001942 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001943 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001944 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001945
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 closeFolder();
1947 exitSpringLoadedDragMode();
1948
1949 // If we are already on home, then just animate back to the workspace,
1950 // otherwise, just wait until onResume to set the state back to Workspace
1951 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001952 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001953 } else {
1954 mOnResumeState = State.WORKSPACE;
1955 }
1956
1957 final View v = getWindow().peekDecorView();
1958 if (v != null && v.getWindowToken() != null) {
1959 InputMethodManager imm = (InputMethodManager)getSystemService(
1960 INPUT_METHOD_SERVICE);
1961 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1962 }
1963
Winson Chungb745afb2015-03-02 11:51:23 -08001964 // Reset the apps view
1965 if (!alreadyOnHome && mAppsView != null) {
1966 mAppsView.scrollToTop();
1967 }
1968
Hyunyoung Song3f471442015-04-08 19:01:34 -07001969 // Reset the widgets view
1970 if (!alreadyOnHome && mWidgetsView != null) {
1971 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001972 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001973
Adam Cohen9211d422014-10-07 18:14:53 -07001974 if (mLauncherCallbacks != null) {
1975 mLauncherCallbacks.onHomeIntent();
1976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
Adam Cohened307df2013-10-02 09:37:31 -07001978
Michael Jurka447bf842013-05-15 14:52:15 +02001979 if (DEBUG_RESUME_TIME) {
1980 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982
Adam Cohen9211d422014-10-07 18:14:53 -07001983 if (mLauncherCallbacks != null) {
1984 mLauncherCallbacks.onNewIntent(intent);
1985 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001989 public void onRestoreInstanceState(Bundle state) {
1990 super.onRestoreInstanceState(state);
1991 for (int page: mSynchronouslyBoundPages) {
1992 mWorkspace.restoreInstanceStateForChild(page);
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995
1996 @Override
1997 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001998 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001999 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2000 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002001 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002002 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
Michael Jurka883f55b2010-10-21 15:47:14 -07002004 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002005 // We close any open folder since it will not be re-opened, and we need to make sure
2006 // this state is reflected.
2007 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008
Adam Cohendcd297f2013-06-18 13:13:40 -07002009 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002010 mWaitingForResult) {
2011 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002013 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2014 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002015 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2016 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2017 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002018 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 if (mFolderInfo != null && mWaitingForResult) {
2022 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2023 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2024 }
Michael Jurka946ad472010-07-09 18:05:18 -07002025
Hyunyoung Song3f471442015-04-08 19:01:34 -07002026 // Save the current widgets tray?
2027 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002028 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002029
2030 if (mLauncherCallbacks != null) {
2031 mLauncherCallbacks.onSaveInstanceState(outState);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
2035 @Override
2036 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Winson Chunge7a03942011-08-05 15:05:12 -07002039 // Remove all pending runnables
2040 mHandler.removeMessages(ADVANCE_MSG);
2041 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002042 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002043
Winson Chungcd2b0142011-06-08 16:02:26 -07002044 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002045 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002046
2047 // It's possible to receive onDestroy after a new Launcher activity has
2048 // been created. In this case, don't interfere with the new Launcher.
2049 if (mModel.isCurrentCallbacks(this)) {
2050 mModel.stopLoader();
2051 app.setLauncher(null);
2052 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002055 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002057 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059 mAppWidgetHost = null;
2060
2061 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062
2063 TextKeyListener.getInstance().release();
2064
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002065 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002066 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002067
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002068 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002069 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002070 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002071 mWorkspace = null;
2072 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002073
Michael Jurka2ecf9952012-06-18 12:52:28 -07002074 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002075
2076 if (mLauncherCallbacks != null) {
2077 mLauncherCallbacks.onDestroy();
2078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
2080
Adam Cohena9cf38f2011-05-02 15:36:58 -07002081 public DragController getDragController() {
2082 return mDragController;
2083 }
2084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 @Override
2086 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002087 onStartForResult(requestCode);
2088 super.startActivityForResult(intent, requestCode);
2089 }
2090
2091 @Override
2092 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2093 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2094 onStartForResult(requestCode);
2095 try {
2096 super.startIntentSenderForResult(intent, requestCode,
2097 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2098 } catch (IntentSender.SendIntentException e) {
2099 throw new ActivityNotFoundException();
2100 }
2101 }
2102
2103 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002104 if (requestCode >= 0) {
2105 setWaitingForResult(true);
2106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108
Winson Chung70d72102011-08-12 11:24:35 -07002109 /**
2110 * Indicates that we want global search for this activity by setting the globalSearch
2111 * argument for {@link #startSearch} to true.
2112 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002114 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002116
Karl Rosaen138a0412009-04-23 19:00:21 -07002117 if (initialQuery == null) {
2118 // Use any text typed in the launcher as the initial query
2119 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 if (appSearchData == null) {
2122 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002123 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
Winson Chungadf0c182012-08-23 14:59:07 -07002125 Rect sourceBounds = new Rect();
2126 if (mSearchDropTargetBar != null) {
2127 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2128 }
Romain Guycbb89e42009-06-08 15:52:54 -07002129
Ian Parkinson047036e2014-09-01 15:40:53 +01002130 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002132 if (clearTextImmediately) {
2133 clearTypedText();
2134 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002135
2136 // We need to show the workspace after starting the search
2137 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002138 }
2139
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 /**
2141 * Start a text search.
2142 *
2143 * @return {@code true} if the search will start immediately, so any further keypresses
2144 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2145 * to buffer keypresses.
2146 */
2147 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002149 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2150 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2151 sourceBounds);
2152 }
2153
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002156 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 }
2158
2159 /**
2160 * Starts the global search activity. This code is a copied from SearchManager
2161 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002162 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002163 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002164 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2166 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2167 if (globalSearchActivity == null) {
2168 Log.w(TAG, "No global search activity found.");
2169 return;
2170 }
2171 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2172 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2173 intent.setComponent(globalSearchActivity);
2174 // Make sure that we have a Bundle to put source in
2175 if (appSearchData == null) {
2176 appSearchData = new Bundle();
2177 } else {
2178 appSearchData = new Bundle(appSearchData);
2179 }
Adam Cohen9211d422014-10-07 18:14:53 -07002180 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002181 if (!appSearchData.containsKey("source")) {
2182 appSearchData.putString("source", getPackageName());
2183 }
2184 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2185 if (!TextUtils.isEmpty(initialQuery)) {
2186 intent.putExtra(SearchManager.QUERY, initialQuery);
2187 }
2188 if (selectInitialQuery) {
2189 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2190 }
2191 intent.setSourceBounds(sourceBounds);
2192 try {
2193 startActivity(intent);
2194 } catch (ActivityNotFoundException ex) {
2195 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Yura4f93ec62014-02-04 14:15:21 +00002199 public boolean isOnCustomContent() {
2200 return mWorkspace.isOnOrMovingToCustomContent();
2201 }
2202
Winson Chung70d72102011-08-12 11:24:35 -07002203 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002204 public boolean onPrepareOptionsMenu(Menu menu) {
2205 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002206 if (!isOnCustomContent()) {
2207 // Close any open folders
2208 closeFolder();
2209 // Stop resizing any widgets
2210 mWorkspace.exitWidgetResizeMode();
2211 if (!mWorkspace.isInOverviewMode()) {
2212 // Show the overview mode
2213 showOverviewMode(true);
2214 } else {
2215 showWorkspace(true);
2216 }
Winson Chunge0298742014-01-17 12:03:00 -08002217 }
Adam Cohen9211d422014-10-07 18:14:53 -07002218 if (mLauncherCallbacks != null) {
2219 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2220 }
2221
Michael Jurkab94f3f82013-09-11 18:08:54 +02002222 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002225 @Override
2226 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002227 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002228 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002229 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002230 }
2231
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 public boolean isWorkspaceLocked() {
2233 return mWorkspaceLoading || mWaitingForResult;
2234 }
2235
Adam Cohen517a7f52014-03-01 12:12:59 -08002236 public boolean isWorkspaceLoading() {
2237 return mWorkspaceLoading;
2238 }
2239
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002240 private void setWorkspaceLoading(boolean value) {
2241 boolean isLocked = isWorkspaceLocked();
2242 mWorkspaceLoading = value;
2243 if (isLocked != isWorkspaceLocked()) {
2244 onWorkspaceLockedChanged();
2245 }
2246 }
2247
2248 private void setWaitingForResult(boolean value) {
2249 boolean isLocked = isWorkspaceLocked();
2250 mWaitingForResult = value;
2251 if (isLocked != isWorkspaceLocked()) {
2252 onWorkspaceLockedChanged();
2253 }
2254 }
2255
Adam Cohen9211d422014-10-07 18:14:53 -07002256 protected void onWorkspaceLockedChanged() {
2257 if (mLauncherCallbacks != null) {
2258 mLauncherCallbacks.onWorkspaceLockedChanged();
2259 }
2260 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002261
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2266 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002270
Sunny Goyale6b63a32015-01-16 16:14:29 -08002271 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002272 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002273 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2274 }
2275
Sunny Goyale6b63a32015-01-16 16:14:29 -08002276 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002277 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2278 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002279 if (appWidgetInfo.configure != null) {
2280 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002281 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002282
Bjorn Bringert7984c942009-12-09 15:38:25 +00002283 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002284 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2285 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002288 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002289 Runnable onComplete = new Runnable() {
2290 @Override
2291 public void run() {
2292 // Exit spring loaded mode if necessary after adding the widget
2293 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2294 null);
2295 }
2296 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002298 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002299 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301 }
Romain Guycbb89e42009-06-08 15:52:54 -07002302
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002303 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002304 // Close any folders that may be open.
2305 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002306 mWorkspace.moveToCustomContentScreen(animate);
2307 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002308
2309 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2310 int[] cell, int spanX, int spanY) {
2311 switch (info.itemType) {
2312 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2313 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2314 int span[] = new int[2];
2315 span[0] = spanX;
2316 span[1] = spanY;
2317 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2318 container, screenId, cell, span);
2319 break;
2320 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2321 processShortcutFromDrop(info.componentName, container, screenId, cell);
2322 break;
2323 default:
2324 throw new IllegalStateException("Unknown item type: " + info.itemType);
2325 }
2326 }
2327
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 /**
2329 * Process a shortcut drop.
2330 *
2331 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002335 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2336 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002337 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002340 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341
2342 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 mPendingAddInfo.cellX = cell[0];
2344 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002346
2347 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2348 createShortcutIntent.setComponent(componentName);
2349 processShortcut(createShortcutIntent);
2350 }
2351
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 /**
2353 * Process a widget drop.
2354 *
2355 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002359 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2360 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002363 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002364 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002365 mPendingAddInfo.minSpanX = info.minSpanX;
2366 mPendingAddInfo.minSpanY = info.minSpanY;
2367
Adam Cohenfbba09b2011-07-18 21:43:05 -07002368 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002369 mPendingAddInfo.cellX = cell[0];
2370 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002372 if (span != null) {
2373 mPendingAddInfo.spanX = span[0];
2374 mPendingAddInfo.spanY = span[1];
2375 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376
Adam Cohened66b2b2012-01-23 17:28:51 -08002377 AppWidgetHostView hostView = info.boundWidget;
2378 int appWidgetId;
2379 if (hostView != null) {
2380 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002381 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002382 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002383 // In this case, we either need to start an activity to get permission to bind
2384 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002385 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002386 Bundle options = info.bindOptions;
2387
Sunny Goyalffe83f12014-08-14 17:39:34 -07002388 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2389 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002390 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002391 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002392 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002393 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2395 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2396 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002397 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2398 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002399 // TODO: we need to make sure that this accounts for the options bundle.
2400 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002401 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2402 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002403 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002404 }
2405
Joe Onoratodeb98af2010-02-19 14:59:39 -08002406 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002407 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
2409
Winson Chung24ab2f12010-09-16 14:10:47 -07002410 void processWallpaper(Intent intent) {
2411 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2412 }
2413
Adam Cohendcd297f2013-06-18 13:13:40 -07002414 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002416 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 folderInfo.title = getText(R.string.folder_name);
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002420 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2421 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002422 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423
2424 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 FolderIcon newFolder =
2426 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002429 // Force measure the new folder icon
2430 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2431 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002432 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 }
Romain Guycbb89e42009-06-08 15:52:54 -07002434
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002436 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 }
2438
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002439 /**
2440 * Registers various content observers. The current implementation registers
2441 * only a favorites observer to keep track of the favorites applications.
2442 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002443 private void registerContentObservers() {
2444 ContentResolver resolver = getContentResolver();
2445 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2446 true, mWidgetObserver);
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 @Override
2450 public boolean dispatchKeyEvent(KeyEvent event) {
2451 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2452 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002455 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002456 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002457 dumpState();
2458 return true;
2459 }
2460 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002461 }
2462 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2463 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002464 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 return true;
2466 }
2467 }
2468
2469 return super.dispatchKeyEvent(event);
2470 }
2471
Joe Onorato88ec0992009-11-19 13:16:06 -08002472 @Override
2473 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002474 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2475 return;
2476 }
2477
Winson Chung3d9490a2015-03-24 17:38:42 -07002478 LauncherAccessibilityDelegate delegate =
2479 LauncherAppState.getInstance().getAccessibilityDelegate();
2480 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002481 return;
2482 }
2483
Winson Chungb745afb2015-03-02 11:51:23 -08002484 if (isAppsViewVisible()) {
2485 showWorkspace(true);
2486 } else if (isWidgetsViewVisible()) {
2487 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002489 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002491 Folder openFolder = mWorkspace.getOpenFolder();
2492 if (openFolder.isEditingName()) {
2493 openFolder.dismissEditingName();
2494 } else {
2495 closeFolder();
2496 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002497 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002498 mWorkspace.exitWidgetResizeMode();
2499
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002500 // Back button is a no-op here, but give at least some feedback for the button press
2501 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002502 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 * Re-listen when widgets are reset.
2507 */
Adam Cohen091440a2015-03-18 14:16:05 -07002508 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002509 if (mAppWidgetHost != null) {
2510 mAppWidgetHost.startListening();
2511 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002512 }
2513
2514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 * Launches the intent referred by the clicked shortcut.
2516 *
2517 * @param v The view representing the clicked shortcut.
2518 */
2519 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2521 // view has detached (it's possible for this to happen if the view is removed mid touch).
2522 if (v.getWindowToken() == null) {
2523 return;
2524 }
2525
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002526 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002527 return;
2528 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002529
Adam Cohen1697b792013-09-17 19:08:21 -07002530 if (v instanceof Workspace) {
2531 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 return;
2535 }
2536
2537 if (v instanceof CellLayout) {
2538 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002539 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002540 }
2541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002544 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002547 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002552 } else if (tag instanceof AppInfo) {
2553 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 } else if (tag instanceof LauncherAppWidgetInfo) {
2555 if (v instanceof PendingAppWidgetHostView) {
2556 onClickPendingWidget((PendingAppWidgetHostView) v);
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559 }
2560
Sunny Goyal508da152014-08-14 10:53:27 -07002561 public void onClickPagedViewIcon(View v) {
2562 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002563 if (mLauncherCallbacks != null) {
2564 mLauncherCallbacks.onClickPagedViewIcon(v);
2565 }
Sunny Goyal508da152014-08-14 10:53:27 -07002566 }
2567
Michael Jurka0e260592010-06-30 17:07:39 -07002568 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002569 return false;
2570 }
2571
Michael Jurkaaf442092010-06-10 17:01:57 -07002572 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002573 * Event handler for the app widget view which has not fully restored.
2574 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002576 if (mIsSafeModeEnabled) {
2577 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2578 return;
2579 }
2580
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002582 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002583 int widgetId = info.appWidgetId;
2584 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2585 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002586 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2587 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 mPendingAddInfo.copyFrom(info);
2589 mPendingAddWidgetId = widgetId;
2590
Sunny Goyalffe83f12014-08-14 17:39:34 -07002591 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2592 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002593 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 } else if (info.installProgress < 0) {
2595 // The install has not been queued
2596 final String packageName = info.providerName.getPackageName();
2597 showBrokenAppInstallDialog(packageName,
2598 new DialogInterface.OnClickListener() {
2599 public void onClick(DialogInterface dialog, int id) {
2600 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2601 }
2602 });
2603 } else {
2604 // Download has started.
2605 final String packageName = info.providerName.getPackageName();
2606 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002607 }
2608 }
2609
2610 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002611 * Event handler for the "grid" button that appears on the home screen, which
2612 * enters all apps mode.
2613 *
2614 * @param v The view that was clicked.
2615 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 protected void onClickAllAppsButton(View v) {
2617 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002618 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 showWorkspace(true);
2620 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002621 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 }
2623 }
2624
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002625 private void showBrokenAppInstallDialog(final String packageName,
2626 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002627 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 .setTitle(R.string.abandoned_promises_title)
2629 .setMessage(R.string.abandoned_promise_explanation)
2630 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2631 .setNeutralButton(R.string.abandoned_clean_this,
2632 new DialogInterface.OnClickListener() {
2633 public void onClick(DialogInterface dialog, int id) {
2634 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2635 mWorkspace.removeAbandonedPromise(packageName, user);
2636 }
2637 })
2638 .create().show();
2639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 /**
2643 * Event handler for an app shortcut click.
2644 *
2645 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2646 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2649 Object tag = v.getTag();
2650 if (!(tag instanceof ShortcutInfo)) {
2651 throw new IllegalArgumentException("Input must be a Shortcut");
2652 }
2653
2654 // Open shortcut
2655 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002656
2657 if (shortcut.isDisabled != 0) {
2658 int error = R.string.activity_not_available;
2659 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2660 error = R.string.safemode_shortcut_error;
2661 }
2662 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2663 return;
2664 }
2665
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666 final Intent intent = shortcut.intent;
2667
2668 // Check for special shortcuts
2669 if (intent.getComponent() != null) {
2670 final String shortcutClass = intent.getComponent().getClassName();
2671
2672 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2673 MemoryDumpActivity.startDump(this);
2674 return;
2675 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2676 toggleShowWeightWatcher();
2677 return;
2678 }
2679 }
2680
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002682 if ((v instanceof BubbleTextView)
2683 && shortcut.isPromise()
2684 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002685 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002686 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 new DialogInterface.OnClickListener() {
2688 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002689 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 return;
2693 }
2694
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002695 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002696 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002697
2698 if (mLauncherCallbacks != null) {
2699 mLauncherCallbacks.onClickAppShortcut(v);
2700 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002701 }
2702
Adam Cohen091440a2015-03-18 14:16:05 -07002703 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002704 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002705 final ShortcutInfo shortcut;
2706 final Intent intent;
2707 if (tag instanceof ShortcutInfo) {
2708 shortcut = (ShortcutInfo) tag;
2709 intent = shortcut.intent;
2710 int[] pos = new int[2];
2711 v.getLocationOnScreen(pos);
2712 intent.setSourceBounds(new Rect(pos[0], pos[1],
2713 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002714
Sunny Goyal508da152014-08-14 10:53:27 -07002715 } else if (tag instanceof AppInfo) {
2716 shortcut = null;
2717 intent = ((AppInfo) tag).intent;
2718 } else {
2719 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2720 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002721
2722 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 mStats.recordLaunch(intent, shortcut);
2724
2725 if (success && v instanceof BubbleTextView) {
2726 mWaitingForResume = (BubbleTextView) v;
2727 mWaitingForResume.setStayPressed(true);
2728 }
2729 }
2730
2731 /**
2732 * Event handler for a folder icon click.
2733 *
2734 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2735 */
2736 protected void onClickFolderIcon(View v) {
2737 if (LOGD) Log.d(TAG, "onClickFolder");
2738 if (!(v instanceof FolderIcon)){
2739 throw new IllegalArgumentException("Input must be a FolderIcon");
2740 }
2741
2742 FolderIcon folderIcon = (FolderIcon) v;
2743 final FolderInfo info = folderIcon.getFolderInfo();
2744 Folder openFolder = mWorkspace.getFolderForTag(info);
2745
2746 // If the folder info reports that the associated folder is open, then verify that
2747 // it is actually opened. There have been a few instances where this gets out of sync.
2748 if (info.opened && openFolder == null) {
2749 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2750 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2751 info.opened = false;
2752 }
2753
2754 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2755 // Close any open folder
2756 closeFolder();
2757 // Open the requested folder
2758 openFolder(folderIcon);
2759 } else {
2760 // Find the open folder...
2761 int folderScreen;
2762 if (openFolder != null) {
2763 folderScreen = mWorkspace.getPageForView(openFolder);
2764 // .. and close it
2765 closeFolder(openFolder);
2766 if (folderScreen != mWorkspace.getCurrentPage()) {
2767 // Close any folder open on the current screen
2768 closeFolder();
2769 // Pull the folder onto this screen
2770 openFolder(folderIcon);
2771 }
2772 }
2773 }
Adam Cohen9211d422014-10-07 18:14:53 -07002774
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onClickFolderIcon(v);
2777 }
Michael Jurka5130e402011-10-13 04:55:35 -07002778 }
2779
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002780 /**
2781 * Event handler for the (Add) Widgets button that appears after a long press
2782 * on the home screen.
2783 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002784 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002785 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002786 if (mIsSafeModeEnabled) {
2787 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2788 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002789 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onClickAddWidgetButton(view);
2792 }
Adam Cohen9211d422014-10-07 18:14:53 -07002793 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002794 }
2795
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 /**
2797 * Event handler for the wallpaper picker button that appears after a long press
2798 * on the home screen.
2799 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002800 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002802 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2803 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002804
2805 if (mLauncherCallbacks != null) {
2806 mLauncherCallbacks.onClickWallpaperPicker(v);
2807 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002808 }
2809
2810 /**
2811 * Event handler for a click on the settings button that appears after a long press
2812 * on the home screen.
2813 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002814 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002815 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002816 if (mLauncherCallbacks != null) {
2817 mLauncherCallbacks.onClickSettingsButton(v);
2818 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002819 }
2820
Michael Jurka5130e402011-10-13 04:55:35 -07002821 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002822 // Provide the same haptic feedback that the system offers for virtual keys.
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002824 }
2825
Adam Cohen61f560d2013-09-30 15:58:20 -07002826 public void performHapticFeedbackOnTouchDown(View v) {
2827 // Provide the same haptic feedback that the system offers for virtual keys.
2828 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2829 }
2830
2831 public View.OnTouchListener getHapticFeedbackTouchListener() {
2832 if (mHapticFeedbackTouchListener == null) {
2833 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2834 @Override
2835 public boolean onTouch(View v, MotionEvent event) {
2836 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2838 }
2839 return false;
2840 }
2841 };
2842 }
2843 return mHapticFeedbackTouchListener;
2844 }
2845
Adam Cohen9211d422014-10-07 18:14:53 -07002846 public void onDragStarted(View view) {
2847 if (isOnCustomContent()) {
2848 // Custom content screen doesn't participate in drag and drop. If on custom
2849 // content screen, move to default.
2850 moveWorkspaceToDefaultScreen();
2851 }
2852
2853 if (mLauncherCallbacks != null) {
2854 mLauncherCallbacks.onDragStarted(view);
2855 }
2856 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002857
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002858 /**
2859 * Called when the user stops interacting with the launcher.
2860 * This implies that the user is now on the homescreen and is not doing housekeeping.
2861 */
Adam Cohen9211d422014-10-07 18:14:53 -07002862 protected void onInteractionEnd() {
2863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onInteractionEnd();
2865 }
2866 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002867
2868 /**
2869 * Called when the user starts interacting with the launcher.
2870 * The possible interactions are:
2871 * - open all apps
2872 * - reorder an app shortcut, or a widget
2873 * - open the overview mode.
2874 * This is a good time to stop doing things that only make sense
2875 * when the user is on the homescreen and not doing housekeeping.
2876 */
Adam Cohen9211d422014-10-07 18:14:53 -07002877 protected void onInteractionBegin() {
2878 if (mLauncherCallbacks != null) {
2879 mLauncherCallbacks.onInteractionBegin();
2880 }
2881 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002882
Winson Chungcd99cd32015-04-29 11:03:24 -07002883 /** Updates the interaction state. */
2884 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002885 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002886 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002887 boolean fromStateWithOverlay;
2888 boolean toStateWithOverlay;
2889 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2890 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2891 toStateWithOverlay = toState != Workspace.State.NORMAL;
2892 } else {
2893 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2894 fromState != Workspace.State.NORMAL_HIDDEN;
2895 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2896 toState != Workspace.State.NORMAL_HIDDEN;
2897 }
2898 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002899 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002900 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002901 onInteractionEnd();
2902 }
2903 }
2904
Kenny Guyf07af7b2014-07-31 11:39:16 +01002905 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002906 try {
2907 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002908 launcherApps.showAppDetailsForProfile(componentName, user);
2909 } catch (SecurityException e) {
2910 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2911 Log.e(TAG, "Launcher does not have permission to launch settings");
2912 } catch (ActivityNotFoundException e) {
2913 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2914 Log.e(TAG, "Unable to launch settings");
2915 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002916 }
2917
Michael Jurka1e2f4652013-07-08 18:03:46 -07002918 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002919 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2920 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002921 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002922 // System applications cannot be installed. For now, show a toast explaining that.
2923 // We may give them the option of disabling apps this way.
2924 int messageId = R.string.uninstall_system_app_text;
2925 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002926 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002927 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002928 String packageName = componentName.getPackageName();
2929 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002930 Intent intent = new Intent(
2931 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002932 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2933 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002934 if (user != null) {
2935 user.addToIntent(intent, Intent.EXTRA_USER);
2936 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002937 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002938 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002939 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002940 }
2941
Michael Jurka86a720e2012-05-09 11:23:23 -07002942 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002943 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002945 // Only launch using the new animation if the shortcut has not opted out (this is a
2946 // private contract between launcher and may be ignored in the future).
2947 boolean useLaunchAnimation = (v != null) &&
2948 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002949 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2950 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002951
Kenny Guy1317e2d2014-05-08 18:52:50 +01002952 UserHandleCompat user = null;
2953 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2954 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2955 user = userManager.getUserForSerialNumber(serialNumber);
2956 }
2957
2958 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002959 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002960 ActivityOptions opts = null;
2961 if (sClipRevealMethod != null) {
2962 // TODO: call method directly when Launcher3 can depend on M APIs
2963 int left = 0, top = 0;
2964 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2965 if (v instanceof TextView) {
2966 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002967 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2968 if (icon != null) {
2969 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002970 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002971 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002972 width = bounds.width();
2973 height = bounds.height();
2974 }
2975 }
2976 try {
2977 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2978 left, top, width, height);
2979 } catch (IllegalAccessException e) {
2980 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2981 sClipRevealMethod = null;
2982 } catch (InvocationTargetException e) {
2983 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2984 sClipRevealMethod = null;
2985 }
2986 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002987 if (opts == null && !Utilities.isLmpOrAbove()) {
2988 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002989 v.getMeasuredWidth(), v.getMeasuredHeight());
2990 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002991 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002992 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002993
2994 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2995 // Could be launching some bookkeeping activity
2996 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002997 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002998 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002999 launcherApps.startActivityForProfile(intent.getComponent(), user,
3000 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003001 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003002 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 } catch (SecurityException e) {
3004 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003005 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003007 "or use the exported attribute for this activity. "
3008 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003010 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003012
Michael Jurka86a720e2012-05-09 11:23:23 -07003013 boolean startActivitySafely(View v, Intent intent, Object tag) {
3014 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003015 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3016 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3017 return false;
3018 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003019 try {
3020 success = startActivity(v, intent, tag);
3021 } catch (ActivityNotFoundException e) {
3022 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3023 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3024 }
3025 return success;
3026 }
3027
Adam Cohen268c4752012-06-06 17:47:33 -07003028 /**
3029 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3030 * in the DragLayer in the exact absolute location of the original FolderIcon.
3031 */
3032 private void copyFolderIconToImage(FolderIcon fi) {
3033 final int width = fi.getMeasuredWidth();
3034 final int height = fi.getMeasuredHeight();
3035
3036 // Lazy load ImageView, Bitmap and Canvas
3037 if (mFolderIconImageView == null) {
3038 mFolderIconImageView = new ImageView(this);
3039 }
3040 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3041 mFolderIconBitmap.getHeight() != height) {
3042 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3043 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3044 }
3045
3046 DragLayer.LayoutParams lp;
3047 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3048 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3049 } else {
3050 lp = new DragLayer.LayoutParams(width, height);
3051 }
3052
Adam Cohen307fe232012-08-16 17:55:58 -07003053 // The layout from which the folder is being opened may be scaled, adjust the starting
3054 // view size by this scale factor.
3055 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003056 lp.customPosition = true;
3057 lp.x = mRectForFolderAnimation.left;
3058 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003059 lp.width = (int) (scale * width);
3060 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003061
3062 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3063 fi.draw(mFolderIconCanvas);
3064 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003065 if (fi.getFolder() != null) {
3066 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3067 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003068 }
Adam Cohen268c4752012-06-06 17:47:33 -07003069 // Just in case this image view is still in the drag layer from a previous animation,
3070 // we remove it and re-add it.
3071 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3072 mDragLayer.removeView(mFolderIconImageView);
3073 }
3074 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003075 if (fi.getFolder() != null) {
3076 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003077 }
Adam Cohen268c4752012-06-06 17:47:33 -07003078 }
3079
Adam Cohen2801caf2011-05-13 20:57:39 -07003080 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003081 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003082 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3083 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3084 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3085
Adam Cohenc51934b2011-07-26 21:07:43 -07003086 FolderInfo info = (FolderInfo) fi.getTag();
3087 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3088 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003089 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3090 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 }
3092
Adam Cohen268c4752012-06-06 17:47:33 -07003093 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3094 copyFolderIconToImage(fi);
3095 fi.setVisibility(View.INVISIBLE);
3096
Michael Jurka2ecf9952012-06-18 12:52:28 -07003097 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003098 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003099 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003100 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3101 }
3102 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 oa.start();
3104 }
3105
Adam Cohen268c4752012-06-06 17:47:33 -07003106 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003107 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003108 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3109 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3110 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3111
Adam Cohen268c4752012-06-06 17:47:33 -07003112 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003113
Adam Cohen268c4752012-06-06 17:47:33 -07003114 // We remove and re-draw the FolderIcon in-case it has changed
3115 mDragLayer.removeView(mFolderIconImageView);
3116 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003117 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003118 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003119 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003120 oa.addListener(new AnimatorListenerAdapter() {
3121 @Override
3122 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003123 if (cl != null) {
3124 cl.clearFolderLeaveBehind();
3125 // Remove the ImageView copy of the FolderIcon and make the original visible.
3126 mDragLayer.removeView(mFolderIconImageView);
3127 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003128 }
3129 }
3130 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003131 oa.start();
3132 }
3133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003135 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 * is animated relative to the specified View. If the View is null, no animation
3137 * is played.
3138 *
3139 * @param folderInfo The FolderInfo describing the folder to open.
3140 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003141 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003142 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003143 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3144 if (openFolder != null && openFolder != folder) {
3145 // Close any open folder before opening a folder.
3146 closeFolder();
3147 }
3148
Adam Cohena9cf38f2011-05-02 15:36:58 -07003149 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150
Adam Cohena9cf38f2011-05-02 15:36:58 -07003151 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152
Sunny Goyalf4066152015-04-15 09:42:19 -07003153 // While the folder is open, the position of the icon cannot change.
3154 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3155
Adam Cohen4554ee12011-08-03 16:13:21 -07003156 // Just verify that the folder hasn't already been added to the DragLayer.
3157 // There was a one-off crash where the folder had a parent already.
3158 if (folder.getParent() == null) {
3159 mDragLayer.addView(folder);
3160 mDragController.addDropTarget((DropTarget) folder);
3161 } else {
3162 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3163 folder.getParent() + ").");
3164 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003165 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003166 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003167
3168 // Notify the accessibility manager that this folder "window" has appeared and occluded
3169 // the workspace items
3170 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3171 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003172 }
3173
3174 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003175 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003176 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003177 if (folder.isEditingName()) {
3178 folder.dismissEditingName();
3179 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003180 closeFolder(folder);
3181 }
3182 }
3183
3184 void closeFolder(Folder folder) {
3185 folder.getInfo().opened = false;
3186
3187 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3188 if (parent != null) {
3189 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3190 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003191 if (fi != null) {
3192 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3193 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003194 }
3195 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003196
3197 // Notify the accessibility manager that this folder "window" has disappeard and no
3198 // longer occludeds the workspace items
3199 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 }
3201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003203 if (!isDraggingEnabled()) return false;
3204 if (isWorkspaceLocked()) return false;
3205 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206
Adam Cohen1697b792013-09-17 19:08:21 -07003207 if (v instanceof Workspace) {
3208 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003209 if (!mWorkspace.isTouchActive()) {
3210 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003211 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3212 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3213 return true;
3214 } else {
3215 return false;
3216 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003217 } else {
3218 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003219 }
Adam Cohen1697b792013-09-17 19:08:21 -07003220 }
3221
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003222 CellLayout.CellInfo longClickCellInfo = null;
3223 View itemUnderLongClick = null;
3224 if (v.getTag() instanceof ItemInfo) {
3225 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003226 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003227 itemUnderLongClick = longClickCellInfo.cell;
3228 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 }
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 // The hotseat touch handling does not go through Workspace, and we always allow long press
3232 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003233 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003234 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003235 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003236 // User long pressed on empty space
3237 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3238 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003239 if (mWorkspace.isInOverviewMode()) {
3240 mWorkspace.startReordering(v);
3241 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003242 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003245 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3246 mHotseat.getOrderInHotseat(
3247 longClickCellInfo.cellX,
3248 longClickCellInfo.cellY));
3249 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003251 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 }
3253 }
3254 }
3255 return true;
3256 }
3257
Winson Chung3d503fb2011-07-13 17:25:49 -07003258 boolean isHotseatLayout(View layout) {
3259 return mHotseat != null && layout != null &&
3260 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3261 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003262
Winson Chung3d503fb2011-07-13 17:25:49 -07003263 /**
3264 * Returns the CellLayout of the specified container at the specified screen.
3265 */
Sunny Goyal92820592015-03-02 11:31:35 -08003266 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003267 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3268 if (mHotseat != null) {
3269 return mHotseat.getLayout();
3270 } else {
3271 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003272 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003273 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003274 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003275 }
3276 }
3277
Winson Chungb745afb2015-03-02 11:51:23 -08003278 /**
3279 * For overridden classes.
3280 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003281 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003282 return isAppsViewVisible();
3283 }
3284
3285 public boolean isAppsViewVisible() {
3286 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3287 }
3288
3289 public boolean isWidgetsViewVisible() {
3290 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003291 }
3292
Craig Mautner360310b2012-10-26 15:13:08 -07003293 private void setWorkspaceBackground(boolean workspace) {
3294 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003295 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003296 }
3297
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003298 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003299 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3300 int curflags = getWindow().getAttributes().flags
3301 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3302 if (wpflags != curflags) {
3303 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3304 }
Craig Mautner360310b2012-10-26 15:13:08 -07003305 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003306 }
3307
Michael Jurkae326f182011-11-21 14:05:46 -08003308 @Override
3309 public void onTrimMemory(int level) {
3310 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003311 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3312 // The widget preview db can result in holding onto over
3313 // 3MB of memory for caching which isn't necessary.
3314 SQLiteDatabase.releaseMemory();
3315
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003316 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003317 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003318 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003319 if (mLauncherCallbacks != null) {
3320 mLauncherCallbacks.onTrimMemory(level);
3321 }
Michael Jurkae326f182011-11-21 14:05:46 -08003322 }
3323
Winson Chungb745afb2015-03-02 11:51:23 -08003324 @Override
3325 public void onStateTransitionHideSearchBar() {
3326 // Hide the search bar
3327 if (mSearchDropTargetBar != null) {
3328 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3329 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003330 }
3331
Winson Chungb745afb2015-03-02 11:51:23 -08003332 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003333 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3334 true);
Adam Cohened307df2013-10-02 09:37:31 -07003335 }
3336
Winson Chungdc61c4d2015-04-20 18:26:57 -07003337 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3338 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3339 onCompleteRunnable, true);
3340 }
3341
3342 protected void showWorkspace(int snapToPage, boolean animated) {
3343 showWorkspace(snapToPage, animated, null, true);
3344 }
3345
3346 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3347 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003348 boolean changed = mState != State.WORKSPACE ||
3349 mWorkspace.getState() != Workspace.State.NORMAL;
3350 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003351 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003353 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003354 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355
Winson Chungc7d2b602012-05-16 17:02:20 -07003356 // Show the search bar (only animate if we were showing the drop target bar in spring
3357 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003358 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003359 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003360 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003361
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 // Set focus to the AppsCustomize button
3363 if (mAllAppsButton != null) {
3364 mAllAppsButton.requestFocus();
3365 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003366 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003367
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003368 // Change the state *after* we've called all the transition code
3369 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003370
Winson Chung5fb63472011-02-02 17:03:37 -08003371 // Resume the auto-advance of widgets
3372 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003373 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003374
Winson Chung0f785722015-04-08 10:27:49 -07003375 if (changed) {
3376 // Send an accessibility event to announce the context change
3377 getWindow().getDecorView()
3378 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003379 if (notifyLauncherCallbacks) {
3380 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003381 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003382 mLauncherCallbacks.onAllAppsHidden();
3383 }
3384 }
Winson Chung0f785722015-04-08 10:27:49 -07003385 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003386 }
3387
Adam Cohened307df2013-10-02 09:37:31 -07003388 void showOverviewMode(boolean animated) {
3389 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003390 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003391 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3392 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003393 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003394 }
3395
Winson Chungb745afb2015-03-02 11:51:23 -08003396 /**
3397 * Shows the apps view.
3398 */
3399 void showAppsView(boolean animated, boolean resetListToTop) {
3400 if (resetListToTop) {
3401 mAppsView.scrollToTop();
3402 }
3403 showAppsOrWidgets(animated, State.APPS);
3404 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405
Winson Chungb745afb2015-03-02 11:51:23 -08003406 /**
3407 * Shows the widgets view.
3408 */
3409 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003410 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003411 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003413 }
Winson Chungb745afb2015-03-02 11:51:23 -08003414 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415
3416 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003417 @Override
3418 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003420 }
3421 });
Winson Chungb745afb2015-03-02 11:51:23 -08003422 }
3423
3424 /**
3425 * Sets up the transition to show the apps/widgets view.
3426 */
3427 private void showAppsOrWidgets(boolean animated, State toState) {
3428 if (mState != State.WORKSPACE) return;
3429 if (toState != State.APPS && toState != State.WIDGETS) return;
3430
3431 if (toState == State.APPS) {
3432 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003433 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003434 mLauncherCallbacks.onAllAppsShown();
3435 }
Winson Chungb745afb2015-03-02 11:51:23 -08003436 } else {
3437 mStateTransitionAnimation.startAnimationToWidgets(animated);
3438 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003441 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003442
3443 // Pause the auto-advance of widgets until we are out of AllApps
3444 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003445 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 closeFolder();
3447
3448 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003449 getWindow().getDecorView()
3450 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003451 }
3452
Winson Chungcd99cd32015-04-29 11:03:24 -07003453 /**
3454 * Updates the workspace and interaction state on state change, and return the animation to this
3455 * new state.
3456 */
3457 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3458 boolean animated, HashMap<View, Integer> layerViews) {
3459 Workspace.State fromState = mWorkspace.getState();
3460 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3461 updateInteraction(fromState, toState);
3462 return anim;
3463 }
3464
Hyunyoung Song3f471442015-04-08 19:01:34 -07003465 public void enterSpringLoadedDragMode() {
3466 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3467 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003468 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3469 mState == State.WIDGETS_SPRING_LOADED) {
3470 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003471 }
Winson Chungb745afb2015-03-02 11:51:23 -08003472
3473 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003474 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3475 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003476 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003477 }
Adam Cohen7777d962011-08-18 18:58:38 -07003478
Hyunyoung Song3f471442015-04-08 19:01:34 -07003479 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003480 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003481 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003482
Winson Chungcd99cd32015-04-29 11:03:24 -07003483 if (successfulDrop) {
3484 // We need to trigger all apps hidden to notify search to update itself before the
3485 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003486 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003487 mLauncherCallbacks.onAllAppsHidden();
3488 }
3489 }
3490
Winson Chunge7a03942011-08-05 15:05:12 -07003491 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003492 @Override
3493 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003494 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003495 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3496 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003497 // Before we show workspace, hide all apps again because
3498 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3499 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003500 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003501 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003502 } else {
3503 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003504 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003505 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003506 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003507 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003508
Michael Jurkad3ef3062010-11-23 16:23:58 -08003509 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003510 if (mState == State.APPS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003511 showAppsView(true, false);
Winson Chungb745afb2015-03-02 11:51:23 -08003512 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003513 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003515 }
3516
Michael Jurkab3e22d92011-10-31 15:58:33 -07003517 void lockAllApps() {
3518 // TODO
3519 }
3520
3521 void unlockAllApps() {
3522 // TODO
3523 }
3524
Sunny Goyal594d76d2014-11-06 10:12:54 -08003525 protected void disableVoiceButtonProxy(boolean disable) {
3526 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003527 }
3528
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003529 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003530 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3531 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003532 }
3533
Adam Cohen24ce0b32014-01-14 16:18:14 -08003534 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003535 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3536 if (searchProvider == null) {
3537 return null;
3538 }
3539
3540 Bundle opts = new Bundle();
3541 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003542 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003543
3544 SharedPreferences sp = getSharedPreferences(
3545 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3546 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003547 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003548 if (!searchProvider.provider.flattenToString().equals(
3549 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003550 || (widgetInfo == null)
3551 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003552 // A valid widget is not already bound.
3553 if (widgetId > -1) {
3554 mAppWidgetHost.deleteAppWidgetId(widgetId);
3555 widgetId = -1;
3556 }
3557
3558 // Try to bind a new widget
3559 widgetId = mAppWidgetHost.allocateAppWidgetId();
3560
3561 if (!AppWidgetManagerCompat.getInstance(this)
3562 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3563 mAppWidgetHost.deleteAppWidgetId(widgetId);
3564 widgetId = -1;
3565 }
3566
3567 sp.edit()
3568 .putInt(QSB_WIDGET_ID, widgetId)
3569 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3570 .commit();
3571 }
3572
3573 if (widgetId != -1) {
3574 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3575 mQsb.updateAppWidgetOptions(opts);
3576 mQsb.setPadding(0, 0, 0, 0);
3577 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003578 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003579 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003580 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003581 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003582 }
3583
Sunny Goyal22235bc2015-04-03 09:23:43 -07003584 private void reinflateQSBIfNecessary() {
3585 if (mQsb instanceof LauncherAppWidgetHostView &&
3586 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3587 mSearchDropTargetBar.removeView(mQsb);
3588 mQsb = null;
3589 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3590 }
3591 }
3592
Michael Jurkad7c28052012-04-27 15:43:36 -07003593 @Override
3594 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003595 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003596 final List<CharSequence> text = event.getText();
3597 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003598 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003599 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003600 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003601 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003602 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003603 } else {
3604 text.add(getString(R.string.all_apps_home_button_label));
3605 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003606 return result;
3607 }
3608
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003609 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003610 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003611 */
Adam Cohen091440a2015-03-18 14:16:05 -07003612 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003613 @Override
3614 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003615 closeSystemDialogs();
3616 }
3617 }
3618
3619 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003620 * Receives notifications whenever the appwidgets are reset.
3621 */
3622 private class AppWidgetResetObserver extends ContentObserver {
3623 public AppWidgetResetObserver() {
3624 super(new Handler());
3625 }
3626
3627 @Override
3628 public void onChange(boolean selfChange) {
3629 onAppWidgetReset();
3630 }
3631 }
3632
3633 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003634 * If the activity is currently paused, signal that we need to run the passed Runnable
3635 * in onResume.
3636 *
3637 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003638 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3639 * wrong when we're not running, and if the activity comes back to what the configuration was
3640 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 *
3642 * Implementation of the method from LauncherModel.Callbacks.
3643 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003644 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003645 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003646 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003647 if (mPaused) {
3648 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003649 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003650 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003651 }
3652 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003653 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003654 return true;
3655 } else {
3656 return false;
3657 }
3658 }
3659
Michael Jurkac402cd92013-05-20 15:49:32 +02003660 private boolean waitUntilResume(Runnable run) {
3661 return waitUntilResume(run, false);
3662 }
3663
Michael Jurka1e2f4652013-07-08 18:03:46 -07003664 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003665 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003666 }
3667
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003669 * If the activity is currently paused, signal that we need to re-run the loader
3670 * in onResume.
3671 *
3672 * This needs to be called from incoming places where resources might have been loaded
3673 * while we are paused. That is becaues the Configuration might be wrong
3674 * when we're not running, and if it comes back to what it was when we
3675 * were paused, we are not restarted.
3676 *
3677 * Implementation of the method from LauncherModel.Callbacks.
3678 *
3679 * @return true if we are currently paused. The caller might be able to
3680 * skip some work in that case since we will come back again.
3681 */
3682 public boolean setLoadOnResume() {
3683 if (mPaused) {
3684 Log.i(TAG, "setLoadOnResume");
3685 mOnResumeNeedsLoad = true;
3686 return true;
3687 } else {
3688 return false;
3689 }
3690 }
3691
3692 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003696 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003697 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003698 } else {
3699 return SCREEN_COUNT / 2;
3700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003702
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 /**
3704 * Refreshes the shortcuts shown on the workspace.
3705 *
3706 * Implementation of the method from LauncherModel.Callbacks.
3707 */
3708 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003709 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003710
Michael Jurka7607c2f2013-04-03 14:33:19 -07003711 // If we're starting binding all over again, clear any bind calls we'd postponed in
3712 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3713 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003714 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003715
Winson Chungd64d1762013-08-20 14:37:16 -07003716 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003717 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003718 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003719
Michael Jurka05bf644e2011-11-30 20:28:53 -08003720 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003721 if (mHotseat != null) {
3722 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 }
3724 }
3725
Adam Cohendcd297f2013-06-18 13:13:40 -07003726 @Override
3727 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003728 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003729
Adam Cohen5084cba2013-09-03 12:01:16 -07003730 // If there are no screens, we need to have an empty screen
3731 if (orderedScreenIds.size() == 0) {
3732 mWorkspace.addExtraEmptyScreen();
3733 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003734
3735 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003736 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003737 if (hasCustomContentToLeft()) {
3738 mWorkspace.createCustomContentContainer();
3739 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003740 }
Winson Chung64359a52013-07-08 17:17:08 -07003741 }
3742
3743 @Override
3744 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003745 // Log to disk
3746 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3747 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3748 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003749 int count = orderedScreenIds.size();
3750 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003751 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003752 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 }
3754
Adam Cohen39a06042013-07-19 14:30:12 -07003755 private boolean shouldShowWeightWatcher() {
3756 String spKey = LauncherAppState.getSharedPreferencesKey();
3757 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003758 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003759
3760 return show;
3761 }
3762
3763 private void toggleShowWeightWatcher() {
3764 String spKey = LauncherAppState.getSharedPreferencesKey();
3765 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3766 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3767
3768 show = !show;
3769
3770 SharedPreferences.Editor editor = sp.edit();
3771 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3772 editor.commit();
3773
3774 if (mWeightWatcher != null) {
3775 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3776 }
3777 }
3778
Winson Chungd64d1762013-08-20 14:37:16 -07003779 public void bindAppsAdded(final ArrayList<Long> newScreens,
3780 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003781 final ArrayList<ItemInfo> addAnimated,
3782 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003783 Runnable r = new Runnable() {
3784 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003785 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003786 }
3787 };
3788 if (waitUntilResume(r)) {
3789 return;
3790 }
3791
Winson Chungd64d1762013-08-20 14:37:16 -07003792 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003793 if (newScreens != null) {
3794 bindAddScreens(newScreens);
3795 }
Winson Chungd64d1762013-08-20 14:37:16 -07003796
3797 // We add the items without animation on non-visible pages, and with
3798 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003799 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 bindItems(addNotAnimated, 0,
3801 addNotAnimated.size(), false);
3802 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003803 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003804 bindItems(addAnimated, 0,
3805 addAnimated.size(), true);
3806 }
Winson Chungc58497e2013-09-03 17:48:37 -07003807
Winson Chung87412982013-10-03 18:34:14 -07003808 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003809 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003810
Winson Chungb745afb2015-03-02 11:51:23 -08003811 if (addedApps != null && mAppsView != null) {
3812 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003813 }
Winson Chungd64d1762013-08-20 14:37:16 -07003814 }
3815
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003816 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 * Bind the items start-end from the list.
3818 *
3819 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003820 */
Winson Chung64359a52013-07-08 17:17:08 -07003821 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3822 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003823 Runnable r = new Runnable() {
3824 public void run() {
3825 bindItems(shortcuts, start, end, forceAnimateIcons);
3826 }
3827 };
3828 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003829 return;
3830 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003831
Winson Chungf0c6ae02012-03-21 16:10:31 -07003832 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003833 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3834 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003835 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003837 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003838 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003840
3841 // Short circuit if we are loading dock items for a configuration which has no dock
3842 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3843 mHotseat == null) {
3844 continue;
3845 }
3846
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 switch (item.itemType) {
3848 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3849 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003850 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003851 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003852
Winson Chung64359a52013-07-08 17:17:08 -07003853 /*
3854 * TODO: FIX collision case
3855 */
Winson Chungce376632013-07-11 16:12:41 -07003856 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3857 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3858 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003859 View v = cl.getChildAt(item.cellX, item.cellY);
3860 Object tag = v.getTag();
3861 String desc = "Collision while binding workspace item: " + item
3862 + ". Collides with " + tag;
3863 if (LauncherAppState.isDogfoodBuild()) {
3864 throw (new RuntimeException(desc));
3865 } else {
3866 Log.d(TAG, desc);
3867 }
Winson Chungce376632013-07-11 16:12:41 -07003868 }
Winson Chung64359a52013-07-08 17:17:08 -07003869 }
3870
Adam Cohendcd297f2013-06-18 13:13:40 -07003871 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3872 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003873 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003874 // Animate all the applications up now
3875 shortcut.setAlpha(0f);
3876 shortcut.setScaleX(0f);
3877 shortcut.setScaleY(0f);
3878 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003879 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003882 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003883 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003884 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003885 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003886 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3887 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003889 default:
3890 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003892 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003893
Winson Chung997a9232013-07-24 15:33:46 -07003894 if (animateIcons) {
3895 // Animate to the correct page
3896 if (newShortcutsScreenId > -1) {
3897 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003898 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003899 final Runnable startBounceAnimRunnable = new Runnable() {
3900 public void run() {
3901 anim.playTogether(bounceAnims);
3902 anim.start();
3903 }
3904 };
Winson Chung997a9232013-07-24 15:33:46 -07003905 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003906 // We post the animation slightly delayed to prevent slowdowns
3907 // when we are loading right after we return to launcher.
3908 mWorkspace.postDelayed(new Runnable() {
3909 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003910 if (mWorkspace != null) {
3911 mWorkspace.snapToPage(newScreenIndex);
3912 mWorkspace.postDelayed(startBounceAnimRunnable,
3913 NEW_APPS_ANIMATION_DELAY);
3914 }
Winson Chung94d67682013-09-25 16:29:40 -07003915 }
3916 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003917 } else {
3918 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003919 }
3920 }
Winson Chung64359a52013-07-08 17:17:08 -07003921 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003923 }
3924
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 /**
3926 * Implementation of the method from LauncherModel.Callbacks.
3927 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003928 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003929 Runnable r = new Runnable() {
3930 public void run() {
3931 bindFolders(folders);
3932 }
3933 };
3934 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003935 return;
3936 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003937 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003939
3940 /**
3941 * Add the views for a widget to the workspace.
3942 *
3943 * Implementation of the method from LauncherModel.Callbacks.
3944 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003945 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003946 Runnable r = new Runnable() {
3947 public void run() {
3948 bindAppWidget(item);
3949 }
3950 };
3951 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003952 return;
3953 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003954
Daniel Sandler843e8602010-06-07 14:59:01 -04003955 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "bindAppWidget: " + item);
3958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 final Workspace workspace = mWorkspace;
3960
Adam Cohen59400422014-03-05 18:07:04 -08003961 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003962 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003963
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003964 if (!mIsSafeModeEnabled
3965 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3966 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003967 if (appWidgetInfo == null) {
3968 if (DEBUG_WIDGETS) {
3969 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3970 + " belongs to component " + item.providerName
3971 + ", as the povider is null");
3972 }
3973 LauncherModel.deleteItemFromDatabase(this, item);
3974 return;
3975 }
3976 // Note: This assumes that the id remap broadcast is received before this step.
3977 // If that is not the case, the id remap will be ignored and user may see the
3978 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003979 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003980 pendingInfo.spanX = item.spanX;
3981 pendingInfo.spanY = item.spanY;
3982 pendingInfo.minSpanX = item.minSpanX;
3983 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003984 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003985 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003986
Sunny Goyalff572272014-07-23 13:58:07 -07003987 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003988 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3989 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003990
3991 // TODO consider showing a permission dialog when the widget is clicked.
3992 if (!success) {
3993 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3994 if (DEBUG_WIDGETS) {
3995 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3996 + " belongs to component " + item.providerName
3997 + ", as the launcher is unable to bing a new widget id");
3998 }
3999 LauncherModel.deleteItemFromDatabase(this, item);
4000 return;
4001 }
4002
4003 item.appWidgetId = newWidgetId;
4004
4005 // If the widget has a configure activity, it is still needs to set it up, otherwise
4006 // the widget is ready to go.
4007 item.restoreStatus = (appWidgetInfo.configure == null)
4008 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4009 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4010
4011 LauncherModel.updateItemInDatabase(this, item);
4012 }
4013
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004014 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004015 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004016 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004017 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4018 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004019 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004020
Sunny Goyal651077b2014-06-30 14:15:31 -07004021 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4022 } else {
4023 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004024 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4025 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004026 view.updateIcon(mIconCache);
4027 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004028 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004029 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004030 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004033 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034
Adam Cohendcd297f2013-06-18 13:13:40 -07004035 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004037 if (!item.isCustomWidget()) {
4038 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4039 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004040
Joe Onorato9c1289c2009-08-17 11:03:03 -04004041 workspace.requestLayout();
4042
Daniel Sandler843e8602010-06-07 14:59:01 -04004043 if (DEBUG_WIDGETS) {
4044 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4045 + (SystemClock.uptimeMillis()-start) + "ms");
4046 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004047 }
4048
Sunny Goyalff572272014-07-23 13:58:07 -07004049 /**
4050 * Restores a pending widget.
4051 *
4052 * @param appWidgetId The app widget id
4053 * @param cellInfo The position on screen where to create the widget.
4054 */
4055 private void completeRestoreAppWidget(final int appWidgetId) {
4056 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4057 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4058 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4059 return;
4060 }
4061
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004062 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004063 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4064
4065 mWorkspace.reinflateWidgetsIfNecessary();
4066 LauncherModel.updateItemInDatabase(this, info);
4067 }
4068
Adam Cohen1462de32012-07-24 22:34:36 -07004069 public void onPageBoundSynchronously(int page) {
4070 mSynchronouslyBoundPages.add(page);
4071 }
4072
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 /**
4074 * Callback saying that there aren't any more items to bind.
4075 *
4076 * Implementation of the method from LauncherModel.Callbacks.
4077 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004078 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004079 Runnable r = new Runnable() {
4080 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004081 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004082 }
4083 };
4084 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 return;
4086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 if (mSavedState != null) {
4088 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004089 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 mSavedState = null;
4092 }
4093
Adam Cohen1462de32012-07-24 22:34:36 -07004094 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004096 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004097 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004098
4099 // If we received the result of any pending adds while the loader was running (e.g. the
4100 // widget configuration forced an orientation change), process them now.
4101 if (sPendingAddItem != null) {
4102 final long screenId = completeAdd(sPendingAddItem);
4103
4104 // TODO: this moves the user to the page where the pending item was added. Ideally,
4105 // the screen would be guaranteed to exist after bind, and the page would be set through
4106 // the workspace restore process.
4107 mWorkspace.post(new Runnable() {
4108 @Override
4109 public void run() {
4110 mWorkspace.snapToScreenId(screenId);
4111 }
4112 });
4113 sPendingAddItem = null;
4114 }
4115
Sunny Goyal756adbc2015-04-16 15:20:51 -07004116 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004117
4118 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004119 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004120 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004121 }
4122
Adam Cohen3ed316a2014-07-23 18:21:20 -07004123 private void sendLoadingCompleteBroadcastIfNecessary() {
4124 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4125 String permission =
4126 getResources().getString(R.string.receive_first_load_broadcast_permission);
4127 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4128 sendBroadcast(intent, permission);
4129 SharedPreferences.Editor editor = mSharedPrefs.edit();
4130 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4131 editor.apply();
4132 }
4133 }
4134
Winson Chunga0b7e862013-09-05 16:03:15 -07004135 public boolean isAllAppsButtonRank(int rank) {
4136 if (mHotseat != null) {
4137 return mHotseat.isAllAppsButtonRank(rank);
4138 }
4139 return false;
4140 }
4141
Winson Chunga2413752012-04-03 14:22:34 -07004142 private boolean canRunNewAppsAnimation() {
4143 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4144 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4145 }
4146
Winson Chungc9168342013-06-26 14:54:55 -07004147 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4148 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4149 PropertyValuesHolder.ofFloat("alpha", 1f),
4150 PropertyValuesHolder.ofFloat("scaleX", 1f),
4151 PropertyValuesHolder.ofFloat("scaleY", 1f));
4152 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4153 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4154 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4155 return bounceAnim;
4156 }
4157
Adam Cohen27772732013-11-11 14:00:56 +00004158 public boolean useVerticalBarLayout() {
4159 return LauncherAppState.getInstance().getDynamicGrid().
4160 getDeviceProfile().isVerticalBarLayout();
4161 }
4162
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004163 protected Rect getSearchBarBounds() {
4164 return LauncherAppState.getInstance().getDynamicGrid().
4165 getDeviceProfile().getSearchBarBounds();
4166 }
4167
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004168 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004169 if (mSearchDropTargetBar == null) {
4170 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004171 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004172 if (mQsb != null) {
4173 mSearchDropTargetBar.removeView(mQsb);
4174 mQsb = null;
4175 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004176 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004177 }
4178
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004179 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004180 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4181 * multiple calls to bind the same list.)
4182 */
4183 @Thunk ArrayList<AppInfo> mTmpAppsList;
4184 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4185 public void run() {
4186 bindAllApplications(mTmpAppsList);
4187 mTmpAppsList = null;
4188 }
4189 };
4190
4191 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 * Add the icons for all apps.
4193 *
4194 * Implementation of the method from LauncherModel.Callbacks.
4195 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004196 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004197 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4198 mTmpAppsList = apps;
4199 return;
4200 }
4201
Winson Chungb745afb2015-03-02 11:51:23 -08004202 if (mAppsView != null) {
4203 mAppsView.setApps(apps);
4204 }
Adam Cohen9211d422014-10-07 18:14:53 -07004205 if (mLauncherCallbacks != null) {
4206 mLauncherCallbacks.bindAllApplications(apps);
4207 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 }
4209
4210 /**
4211 * A package was updated.
4212 *
4213 * Implementation of the method from LauncherModel.Callbacks.
4214 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004215 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004216 Runnable r = new Runnable() {
4217 public void run() {
4218 bindAppsUpdated(apps);
4219 }
4220 };
4221 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004222 return;
4223 }
4224
Winson Chungb745afb2015-03-02 11:51:23 -08004225 if (mAppsView != null) {
4226 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004227 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004228 }
4229
Sunny Goyal4390ace2014-10-13 11:33:11 -07004230 @Override
4231 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4232 Runnable r = new Runnable() {
4233 public void run() {
4234 bindWidgetsRestored(widgets);
4235 }
4236 };
4237 if (waitUntilResume(r)) {
4238 return;
4239 }
4240 mWorkspace.widgetsRestored(widgets);
4241 }
4242
Joe Onorato9c1289c2009-08-17 11:03:03 -04004243 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004244 * Some shortcuts were updated in the background.
4245 *
4246 * Implementation of the method from LauncherModel.Callbacks.
4247 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004248 @Override
4249 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4250 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004251 Runnable r = new Runnable() {
4252 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004253 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004254 }
4255 };
4256 if (waitUntilResume(r)) {
4257 return;
4258 }
4259
Sunny Goyal4390ace2014-10-13 11:33:11 -07004260 if (!updated.isEmpty()) {
4261 mWorkspace.updateShortcuts(updated);
4262 }
4263
4264 if (!removed.isEmpty()) {
4265 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4266 for (ShortcutInfo si : removed) {
4267 removedComponents.add(si.getTargetComponent());
4268 }
4269 mWorkspace.removeItemsByComponentName(removedComponents, user);
4270 // Notify the drag controller
4271 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004272 }
4273 }
4274
4275 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004276 * Update the state of a package, typically related to install state.
4277 *
4278 * Implementation of the method from LauncherModel.Callbacks.
4279 */
Sunny Goyale755d462014-07-22 13:48:29 -07004280 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004281 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4282 Runnable r = new Runnable() {
4283 public void run() {
4284 bindRestoreItemsChange(updates);
4285 }
4286 };
4287 if (waitUntilResume(r)) {
4288 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004289 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004290
Sunny Goyal756adbc2015-04-16 15:20:51 -07004291 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004292 }
4293
4294 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004295 * A package was uninstalled. We take both the super set of packageNames
4296 * in addition to specific applications to remove, the reason being that
4297 * this can be called when a package is updated as well. In that scenario,
4298 * we only remove specific components from the workspace, where as
4299 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004300 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004301 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004302 * Implementation of the method from LauncherModel.Callbacks.
4303 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004304 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004305 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004306 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004307 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004308 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004309 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004310 }
Winson Chungd64d1762013-08-20 14:37:16 -07004311 };
4312 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004313 return;
4314 }
4315
Sunny Goyal1a745e82014-10-02 15:58:31 -07004316 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004317 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4318 for (AppInfo info : appInfos) {
4319 removedComponents.add(info.componentName);
4320 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004321 if (!packageNames.isEmpty()) {
4322 mWorkspace.removeItemsByPackageName(packageNames, user);
4323 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004324 if (!removedComponents.isEmpty()) {
4325 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004326 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004327 // Notify the drag controller
4328 mDragController.onAppsRemoved(packageNames, removedComponents);
4329
Sunny Goyal1a745e82014-10-02 15:58:31 -07004330 } else {
4331 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004332 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004333
Winson Chungdf95eb12013-10-16 14:57:07 -07004334 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004335 if (mAppsView != null) {
4336 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004338 }
Joe Onoratobe386092009-11-17 17:32:16 -08004339
Adam Cohen091440a2015-03-18 14:16:05 -07004340 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004341 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004342 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004343 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004344 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004345 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004346
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004347 @Override
4348 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004349 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4350 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004351 return;
4352 }
4353
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004354 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4355 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4356 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004357 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004358 }
4359
Winson Chung400438b2011-01-16 17:53:48 -08004360 private int mapConfigurationOriActivityInfoOri(int configOri) {
4361 final Display d = getWindowManager().getDefaultDisplay();
4362 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4363 switch (d.getRotation()) {
4364 case Surface.ROTATION_0:
4365 case Surface.ROTATION_180:
4366 // We are currently in the same basic orientation as the natural orientation
4367 naturalOri = configOri;
4368 break;
4369 case Surface.ROTATION_90:
4370 case Surface.ROTATION_270:
4371 // We are currently in the other basic orientation to the natural orientation
4372 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4373 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4374 break;
4375 }
4376
4377 int[] oriMap = {
4378 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4379 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4380 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4381 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4382 };
4383 // Since the map starts at portrait, we need to offset if this device's natural orientation
4384 // is landscape.
4385 int indexOffset = 0;
4386 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4387 indexOffset = 1;
4388 }
4389 return oriMap[(d.getRotation() + indexOffset) % 4];
4390 }
Adam Cohen446e9402011-09-15 18:21:21 -07004391
Winson Chung4b919f82012-05-01 10:44:08 -07004392 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004393 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004394 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4395 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4396 .getConfiguration().orientation));
4397 } else {
4398 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4399 }
Winson Chung4b919f82012-05-01 10:44:08 -07004400 }
4401 }
4402 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004403 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004404 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004405 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004406 } else {
4407 mHandler.postDelayed(new Runnable() {
4408 public void run() {
4409 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4410 }
4411 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004412 }
Winson Chung4b919f82012-05-01 10:44:08 -07004413 }
Winson Chung400438b2011-01-16 17:53:48 -08004414 }
4415
Winson Chunge43a1e72014-01-15 10:33:02 -08004416 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004417 if (mLauncherCallbacks != null) {
4418 return mLauncherCallbacks.isLauncherPreinstalled();
4419 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004420 PackageManager pm = getPackageManager();
4421 try {
4422 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4423 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4424 return true;
4425 } else {
4426 return false;
4427 }
4428 } catch (NameNotFoundException e) {
4429 e.printStackTrace();
4430 return false;
4431 }
4432 }
4433
Adam Cohenea90f832014-05-21 15:03:34 -07004434 /**
4435 * This method indicates whether or not we should suggest default wallpaper dimensions
4436 * when our wallpaper cropper was not yet used to set a wallpaper.
4437 */
4438 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004439 if (mLauncherCallbacks != null) {
4440 return mLauncherCallbacks.overrideWallpaperDimensions();
4441 }
Adam Cohenea90f832014-05-21 15:03:34 -07004442 return true;
4443 }
4444
Adam Cohen432609a2014-03-13 17:03:22 -07004445 /**
4446 * To be overridden by subclasses to indicate that there is an activity to launch
4447 * before showing the standard launcher experience.
4448 */
4449 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004450 if (mLauncherCallbacks != null) {
4451 return mLauncherCallbacks.hasFirstRunActivity();
4452 }
Adam Cohen432609a2014-03-13 17:03:22 -07004453 return false;
4454 }
4455
4456 /**
4457 * To be overridden by subclasses to launch any first run activity
4458 */
4459 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004460 if (mLauncherCallbacks != null) {
4461 return mLauncherCallbacks.getFirstRunActivity();
4462 }
Adam Cohen432609a2014-03-13 17:03:22 -07004463 return null;
4464 }
4465
Winson Chung2826a402015-04-17 16:18:53 -07004466 /**
4467 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004468 */
4469 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004470 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4471 return false;
4472 }
4473
Winson Chung2826a402015-04-17 16:18:53 -07004474 if (mLauncherCallbacks != null) {
4475 return mLauncherCallbacks.overrideAllAppsSearch();
4476 }
4477 return false;
4478 }
4479
Winson Chunga6945242014-01-08 14:04:34 -08004480 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004481 return !ActivityManager.isRunningInTestHarness() &&
4482 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004483 }
4484
Adam Cohen4a9423d2014-03-28 14:22:31 -07004485 protected boolean hasRunFirstRunActivity() {
4486 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4487 }
4488
Adam Cohen432609a2014-03-13 17:03:22 -07004489 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004490 if (shouldRunFirstRunActivity() &&
4491 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004492 Intent firstRunIntent = getFirstRunActivity();
4493 if (firstRunIntent != null) {
4494 startActivity(firstRunIntent);
4495 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004496 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004497 }
4498 }
Adam Cohen432609a2014-03-13 17:03:22 -07004499 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004500 }
4501
4502 private void markFirstRunActivityShown() {
4503 SharedPreferences.Editor editor = mSharedPrefs.edit();
4504 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4505 editor.apply();
4506 }
4507
Adam Cohen432609a2014-03-13 17:03:22 -07004508 /**
4509 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4510 * screen that must be displayed and dismissed.
4511 */
4512 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004513 if (mLauncherCallbacks != null) {
4514 return mLauncherCallbacks.hasDismissableIntroScreen();
4515 }
Adam Cohen432609a2014-03-13 17:03:22 -07004516 return false;
4517 }
4518
4519 /**
4520 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4521 */
4522 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004523 if (mLauncherCallbacks != null) {
4524 return mLauncherCallbacks.getIntroScreen();
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return null;
4527 }
4528
4529 /**
4530 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4531 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4532 */
4533 private boolean shouldShowIntroScreen() {
4534 return hasDismissableIntroScreen() &&
4535 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4536 }
4537
4538 protected void showIntroScreen() {
4539 View introScreen = getIntroScreen();
4540 changeWallpaperVisiblity(false);
4541 if (introScreen != null) {
4542 mDragLayer.showOverlayView(introScreen);
4543 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004544 if (mLauncherOverlayContainer != null) {
4545 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4546 }
Adam Cohen432609a2014-03-13 17:03:22 -07004547 }
4548
4549 public void dismissIntroScreen() {
4550 markIntroScreenDismissed();
4551 if (showFirstRunActivity()) {
4552 // We delay hiding the intro view until the first run activity is showing. This
4553 // avoids a blip.
4554 mWorkspace.postDelayed(new Runnable() {
4555 @Override
4556 public void run() {
4557 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004558 if (mLauncherOverlayContainer != null) {
4559 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4560 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004561 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004562 }
4563 }, ACTIVITY_START_DELAY);
4564 } else {
4565 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004566 if (mLauncherOverlayContainer != null) {
4567 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4568 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004569 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004570 }
4571 changeWallpaperVisiblity(true);
4572 }
4573
4574 private void markIntroScreenDismissed() {
4575 SharedPreferences.Editor editor = mSharedPrefs.edit();
4576 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4577 editor.apply();
4578 }
4579
Adam Cohen091440a2015-03-18 14:16:05 -07004580 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004581 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4582 // on the device, then we always show the first run cling experience (or if there is no
4583 // launcher2). Otherwise, we prompt the user upon started for migration
4584 LauncherClings launcherClings = new LauncherClings(this);
4585 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4586 if (mModel.canMigrateFromOldLauncherDb(this)) {
4587 launcherClings.showMigrationCling();
4588 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004589 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004590 }
4591 }
4592 }
4593
Winson Chunga6945242014-01-08 14:04:34 -08004594 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004595 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4596 if (mHotseat != null) mHotseat.setAlpha(1f);
4597 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4598 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004599 }
4600
4601 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004602 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4603 if (mHotseat != null) mHotseat.setAlpha(0f);
4604 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4605 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004606 }
4607
Mathew Inwood72fbec12013-11-19 15:45:07 +00004608 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004609 // Called from search suggestion, not supported in other profiles.
4610 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4611 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4612 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4613 myUser);
4614 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004615 return null;
4616 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004617 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004618 }
4619
4620 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4621 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004622 // Called from search suggestion, not supported in other profiles.
4623 return createShortcutDragInfo(shortcutIntent, caption, icon,
4624 UserHandleCompat.myUserHandle());
4625 }
4626
4627 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4628 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004629 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004630 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004631 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004632 }
4633
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004634 protected void moveWorkspaceToDefaultScreen() {
4635 mWorkspace.moveToDefaultScreen(false);
4636 }
4637
Mathew Inwood72fbec12013-11-19 15:45:07 +00004638 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4639 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004640 mWorkspace.onExternalDragStartedWithItem(dragView);
4641 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004642 }
4643
Anjali Koppalf5d29132014-02-28 13:33:27 -08004644 @Override
4645 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004646 if (mLauncherCallbacks != null) {
4647 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4648 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004649 }
4650
Winson Chung80baf5a2010-08-09 16:03:15 -07004651 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004652 * Prints out out state for debugging.
4653 */
4654 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004655 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004656 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004657 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4658 Log.d(TAG, "mRestoring=" + mRestoring);
4659 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4660 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004661 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004662 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004663 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004664
Daniel Sandler325dc232013-06-05 22:57:57 -04004665 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004666 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004667
4668 @Override
4669 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4670 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004671 synchronized (sDumpLogs) {
4672 writer.println(" ");
4673 writer.println("Debug logs: ");
4674 for (int i = 0; i < sDumpLogs.size(); i++) {
4675 writer.println(" " + sDumpLogs.get(i));
4676 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004677 }
Adam Cohen9211d422014-10-07 18:14:53 -07004678 if (mLauncherCallbacks != null) {
4679 mLauncherCallbacks.dump(prefix, fd, writer, args);
4680 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004681 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004682
4683 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004684 if (DEBUG_DUMP_LOG) {
4685 synchronized (sDumpLogs) {
4686 Log.d(TAG, "");
4687 Log.d(TAG, "*********************");
4688 Log.d(TAG, "Launcher debug logs: ");
4689 for (int i = 0; i < sDumpLogs.size(); i++) {
4690 Log.d(TAG, " " + sDumpLogs.get(i));
4691 }
4692 Log.d(TAG, "*********************");
4693 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004694 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004695 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004696 }
4697
4698 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004699 addDumpLog(tag, log, null, debugLog);
4700 }
4701
4702 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004703 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004704 if (e != null) {
4705 Log.d(tag, log, e);
4706 } else {
4707 Log.d(tag, log);
4708 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004709 }
Winson Chungede41292013-09-19 16:27:36 -07004710 if (DEBUG_DUMP_LOG) {
4711 sDateStamp.setTime(System.currentTimeMillis());
4712 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004713 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4714 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004715 }
Winson Chungede41292013-09-19 16:27:36 -07004716 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004717 }
4718
Adam Cohen59400422014-03-05 18:07:04 -08004719 public static CustomAppWidget getCustomAppWidget(String name) {
4720 return sCustomAppWidgets.get(name);
4721 }
4722
4723 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4724 return sCustomAppWidgets;
4725 }
4726
Winson Chungede41292013-09-19 16:27:36 -07004727 public void dumpLogsToLocalData() {
4728 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004729 new AsyncTask<Void, Void, Void>() {
4730 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004731 boolean success = false;
4732 sDateStamp.setTime(sRunStart);
4733 String FILENAME = sDateStamp.getMonth() + "-"
4734 + sDateStamp.getDay() + "_"
4735 + sDateStamp.getHours() + "-"
4736 + sDateStamp.getMinutes() + "_"
4737 + sDateStamp.getSeconds() + ".txt";
4738
4739 FileOutputStream fos = null;
4740 File outFile = null;
4741 try {
4742 outFile = new File(getFilesDir(), FILENAME);
4743 outFile.createNewFile();
4744 fos = new FileOutputStream(outFile);
4745 } catch (Exception e) {
4746 e.printStackTrace();
4747 }
4748 if (fos != null) {
4749 PrintWriter writer = new PrintWriter(fos);
4750
4751 writer.println(" ");
4752 writer.println("Debug logs: ");
4753 synchronized (sDumpLogs) {
4754 for (int i = 0; i < sDumpLogs.size(); i++) {
4755 writer.println(" " + sDumpLogs.get(i));
4756 }
4757 }
4758 writer.close();
4759 }
4760 try {
4761 if (fos != null) {
4762 fos.close();
4763 success = true;
4764 }
4765 } catch (IOException e) {
4766 e.printStackTrace();
4767 }
Michael Jurka43467462013-11-14 12:03:58 +01004768 return null;
Winson Chungede41292013-09-19 16:27:36 -07004769 }
Michael Jurka43467462013-11-14 12:03:58 +01004770 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004771 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004773}
Michael Jurka2763be32011-02-24 11:19:57 -08004774
Michael Jurkaabded662011-03-04 12:06:57 -08004775interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004776 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004777 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004778 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004779 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004780 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004781}
Dan Sandlerd5024042014-01-09 15:01:33 -05004782
4783interface DebugIntents {
4784 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4785 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004786}