blob: 5645759045331a6524b1efa444453ff7ce0967b8 [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 Sandler6053b8002013-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 Chung94804152015-05-08 13:06:44 -0700531 mAppsView.setFixedBounds(bounds);
532 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700533 }
534
535 @Override
536 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700537 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
538 // Dismiss All Apps if we aren't already paused/invisible
539 if (!mPaused) {
540 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
541 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
542 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700543 }
Winson Chung0f785722015-04-08 10:27:49 -0700544 }
545 });
Adam Cohen9211d422014-10-07 18:14:53 -0700546 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700547 }
548
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700549 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700550 public void onLauncherProviderChange() {
551 if (mLauncherCallbacks != null) {
552 mLauncherCallbacks.onLauncherProviderChange();
553 }
554 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700555
Adam Cohen9211d422014-10-07 18:14:53 -0700556 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700557 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700558 if (mLauncherCallbacks != null) {
559 return mLauncherCallbacks.hasCustomContentToLeft();
560 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700561 return false;
562 }
563
Dave Hawkeya8881582013-09-17 15:55:33 -0600564 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500565 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 * {@link #addToCustomContentPage}. This will only be invoked if
567 * {@link #hasCustomContentToLeft()} is {@code true}.
568 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500569 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700570 if (mLauncherCallbacks != null) {
571 mLauncherCallbacks.populateCustomContentContainer();
572 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600573 }
574
575 /**
576 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
577 * ensure the custom content page is added or removed if necessary.
578 */
579 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600580 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600581 // Not bound yet, wait for bindScreens to be called.
582 return;
583 }
584
585 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
586 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500587 mWorkspace.createCustomContentContainer();
588 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
590 mWorkspace.removeCustomContentPage();
591 }
592 }
593
Adam Cohen091440a2015-03-18 14:16:05 -0700594 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 if (sLocaleConfiguration == null) {
596 new AsyncTask<Void, Void, LocaleConfiguration>() {
597 @Override
598 protected LocaleConfiguration doInBackground(Void... unused) {
599 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
600 readConfiguration(Launcher.this, localeConfiguration);
601 return localeConfiguration;
602 }
603
604 @Override
605 protected void onPostExecute(LocaleConfiguration result) {
606 sLocaleConfiguration = result;
607 checkForLocaleChange(); // recursive, but now with a locale configuration
608 }
609 }.execute();
610 return;
611 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final Configuration configuration = getResources().getConfiguration();
614
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final String locale = configuration.locale.toString();
617
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final int mcc = configuration.mcc;
620
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final int mnc = configuration.mnc;
623
Romain Guy84f296c2009-11-04 15:00:44 -0800624 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625
Romain Guy84f296c2009-11-04 15:00:44 -0800626 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 sLocaleConfiguration.locale = locale;
628 sLocaleConfiguration.mcc = mcc;
629 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700630
Joe Onorato0589f0f2010-02-08 13:44:00 -0800631 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700632
633 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100634 new AsyncTask<Void, Void, Void>() {
635 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700636 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100637 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700638 }
Michael Jurka43467462013-11-14 12:03:58 +0100639 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700640 }
641 }
642
Adam Cohen091440a2015-03-18 14:16:05 -0700643 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700644 public String locale;
645 public int mcc = -1;
646 public int mnc = -1;
647 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700648
Adam Cohen091440a2015-03-18 14:16:05 -0700649 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700650 DataInputStream in = null;
651 try {
Helena Josol28db2802014-10-09 17:04:09 +0100652 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700653 configuration.locale = in.readUTF();
654 configuration.mcc = in.readInt();
655 configuration.mnc = in.readInt();
656 } catch (FileNotFoundException e) {
657 // Ignore
658 } catch (IOException e) {
659 // Ignore
660 } finally {
661 if (in != null) {
662 try {
663 in.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
668 }
669 }
670
Adam Cohen091440a2015-03-18 14:16:05 -0700671 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700672 DataOutputStream out = null;
673 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100674 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100675 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700676 out.writeUTF(configuration.locale);
677 out.writeInt(configuration.mcc);
678 out.writeInt(configuration.mnc);
679 out.flush();
680 } catch (FileNotFoundException e) {
681 // Ignore
682 } catch (IOException e) {
683 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100684 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700685 } finally {
686 if (out != null) {
687 try {
688 out.close();
689 } catch (IOException e) {
690 // Ignore
691 }
692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694 }
695
Anton Hanssona2f665f2013-09-26 12:18:32 +0100696 public Stats getStats() {
697 return mStats;
698 }
699
Bjorn Bringertc459e522013-06-07 19:36:01 +0100700 public LayoutInflater getInflater() {
701 return mInflater;
702 }
703
Hyunyoung Song3f471442015-04-08 19:01:34 -0700704 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700705 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
706 // that is subsequently removed from the workspace in startBinding().
707 return !mModel.isLoadingWorkspace();
708 }
709
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800710 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000711 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100712 if (Build.VERSION.SDK_INT >= 17) {
713 return View.generateViewId();
714 } else {
715 // View.generateViewId() is not available. The following fallback logic is a copy
716 // of its implementation.
717 for (;;) {
718 final int result = sNextGeneratedId.get();
719 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
720 int newValue = result + 1;
721 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
722 if (sNextGeneratedId.compareAndSet(result, newValue)) {
723 return result;
724 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000725 }
726 }
727 }
728
729 public int getViewIdForItem(ItemInfo info) {
730 // This cast is safe given the > 2B range for int.
731 int itemId = (int) info.id;
732 if (mItemIdToViewId.containsKey(itemId)) {
733 return mItemIdToViewId.get(itemId);
734 }
735 int viewId = generateViewId();
736 mItemIdToViewId.put(itemId, viewId);
737 return viewId;
738 }
739
740 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700741 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
742 * a configuration step, this allows the proper animations to run after other transitions.
743 */
Adam Cohendb364c32014-05-20 14:23:40 -0700744 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700745 long screenId = args.screenId;
746 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
747 // When the screen id represents an actual screen (as opposed to a rank) we make sure
748 // that the drop page actually exists.
749 screenId = ensurePendingDropLayoutExists(args.screenId);
750 }
Adam Cohendb364c32014-05-20 14:23:40 -0700751
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700754 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700755 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700758 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700759 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700760 case REQUEST_RECONFIGURE_APPWIDGET:
761 completeRestoreAppWidget(args.appWidgetId);
762 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 }
Michael Jurka27614d22012-04-02 06:40:22 -0700764 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
765 // if you turned the screen off and then back while in All Apps, Launcher would not
766 // return to the workspace. Clearing mAddInfo.container here fixes this issue
767 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700768 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800769 }
770
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800771 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700773 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700774 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700775 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800776 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700777
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 Runnable exitSpringLoaded = new Runnable() {
779 @Override
780 public void run() {
781 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
782 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
783 }
784 };
785
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
789 if (resultCode == RESULT_CANCELED) {
790 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700791 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800794 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 }
797 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200798 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
799 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700800 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801 }
802 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
806 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700807
Adam Cohendb364c32014-05-20 14:23:40 -0700808 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 // We have special handling for widgets
810 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800811 final int appWidgetId;
812 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
813 : -1;
814 if (widgetId < 0) {
815 appWidgetId = pendingAddWidgetId;
816 } else {
817 appWidgetId = widgetId;
818 }
819
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800821 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700822 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
823 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = RESULT_CANCELED;
825 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700826 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 @Override
828 public void run() {
829 exitSpringLoadedDragModeDelayed(false, 0, null);
830 }
831 };
Adam Cohendb364c32014-05-20 14:23:40 -0700832 if (workspaceLocked) {
833 // No need to remove the empty screen if we're mid-binding, as the
834 // the bind will not add the empty screen.
835 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
836 } else {
837 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
839 }
Winson Chung5aaab772012-04-27 15:24:02 -0700840 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700841 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700842 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
843 // When the screen id represents an actual screen (as opposed to a rank)
844 // we make sure that the drop page actually exists.
845 mPendingAddInfo.screenId =
846 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
847 }
Adam Cohendb364c32014-05-20 14:23:40 -0700848 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
849
850 dropLayout.setDropPending(true);
851 final Runnable onComplete = new Runnable() {
852 @Override
853 public void run() {
854 completeTwoStageWidgetDrop(resultCode, appWidgetId);
855 dropLayout.setDropPending(false);
856 }
857 };
858 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
860 } else {
861 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
862 mPendingAddInfo);
863 sPendingAddItem = args;
864 }
Winson Chung5aaab772012-04-27 15:24:02 -0700865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 return;
867 }
868
Sunny Goyalff572272014-07-23 13:58:07 -0700869 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
870 if (resultCode == RESULT_OK) {
871 // Update the widget view.
872 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
873 pendingAddWidgetId, mPendingAddInfo);
874 if (workspaceLocked) {
875 sPendingAddItem = args;
876 } else {
877 completeAdd(args);
878 }
879 }
880 // Leave the widget in the pending state if the user canceled the configure.
881 return;
882 }
883
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 // The pattern used here is that a user PICKs a specific application,
885 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
888 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700889 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700890 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
891 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800892 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700893 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800894 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700895 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700896 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
897 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
Adam Cohen00074722013-10-11 17:46:09 -0700899 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700900 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800904
905 }
906
907 @Override
908 protected void onActivityResult(
909 final int requestCode, final int resultCode, final Intent data) {
910 handleActivityResult(requestCode, resultCode, data);
911 if (mLauncherCallbacks != null) {
912 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
913 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
915
Adam Cohendb364c32014-05-20 14:23:40 -0700916 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
917 appWidgetId, ItemInfo info) {
918 PendingAddArguments args = new PendingAddArguments();
919 args.requestCode = requestCode;
920 args.intent = data;
921 args.container = info.container;
922 args.screenId = info.screenId;
923 args.cellX = info.cellX;
924 args.cellY = info.cellY;
925 args.appWidgetId = appWidgetId;
926 return args;
927 }
928
929 /**
930 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
931 *
932 * @param screenId the screen id to check
933 * @return the new screen, or screenId if it exists
934 */
935 private long ensurePendingDropLayoutExists(long screenId) {
936 CellLayout dropLayout =
937 (CellLayout) mWorkspace.getScreenWithId(screenId);
938 if (dropLayout == null) {
939 // it's possible that the add screen was removed because it was
940 // empty and a re-bind occurred
941 mWorkspace.addExtraEmptyScreen();
942 return mWorkspace.commitExtraEmptyScreen();
943 } else {
944 return screenId;
945 }
946 }
947
Adam Cohen091440a2015-03-18 14:16:05 -0700948 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700949 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 Runnable onCompleteRunnable = null;
952 int animationType = 0;
953
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 if (resultCode == RESULT_OK) {
956 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
957 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 mPendingAddWidgetInfo);
959 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 onCompleteRunnable = new Runnable() {
961 @Override
962 public void run() {
963 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700965 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
966 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800967 }
968 };
969 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800970 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800971 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700973 if (mDragLayer.getAnimatedView() != null) {
974 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
975 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
976 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700977 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700978 // The animated view may be null in the case of a rotation during widget configuration
979 onCompleteRunnable.run();
980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 protected void onStop() {
985 super.onStop();
986 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.onStop();
990 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700991 }
992
993 @Override
994 protected void onStart() {
995 super.onStart();
996 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700997
998 if (mLauncherCallbacks != null) {
999 mLauncherCallbacks.onStart();
1000 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001001 }
1002
1003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001005 long startTime = 0;
1006 if (DEBUG_RESUME_TIME) {
1007 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001008 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001009 }
Adam Cohen9211d422014-10-07 18:14:53 -07001010
1011 if (mLauncherCallbacks != null) {
1012 mLauncherCallbacks.preOnResume();
1013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001016
Winson Chung4a2afa32012-07-19 14:53:05 -07001017 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001018 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001019 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001020 } else if (mOnResumeState == State.APPS) {
1021 showAppsView(false /* animated */, false /* resetListToTop */);
1022 } else if (mOnResumeState == State.WIDGETS) {
1023 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001024 }
1025 mOnResumeState = State.NONE;
1026
Winson Chungcd99cd32015-04-29 11:03:24 -07001027 // Restore the apps state if we are in all apps
Winson Chung83f59ab2015-05-05 17:21:58 -07001028 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mState == State.APPS) {
Winson Chungcd99cd32015-04-29 11:03:24 -07001029 if (mLauncherCallbacks != null) {
1030 mLauncherCallbacks.onAllAppsShown();
1031 }
1032 }
1033
Craig Mautner360310b2012-10-26 15:13:08 -07001034 // Background was set to gradient in onPause(), restore to black if in all apps.
1035 setWorkspaceBackground(mState == State.WORKSPACE);
1036
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001037 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001038 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001039 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001040 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001042 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001044 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001045 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1046 // execute them here
1047 long startTimeCallbacks = 0;
1048 if (DEBUG_RESUME_TIME) {
1049 startTimeCallbacks = System.currentTimeMillis();
1050 }
1051
Michael Jurka1e2f4652013-07-08 18:03:46 -07001052 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1053 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001054 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001055 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001056 if (DEBUG_RESUME_TIME) {
1057 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1058 (System.currentTimeMillis() - startTimeCallbacks));
1059 }
Michael Jurka447bf842013-05-15 14:52:15 +02001060 }
Michael Jurka54554252013-08-01 12:52:23 +02001061 if (mOnResumeCallbacks.size() > 0) {
1062 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1063 mOnResumeCallbacks.get(i).run();
1064 }
1065 mOnResumeCallbacks.clear();
1066 }
Winson Chunge4e50662012-01-23 14:45:13 -08001067
1068 // Reset the pressed state of icons that were locked in the press state while activities
1069 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001070 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001071 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001072 mWaitingForResume.setStayPressed(false);
1073 }
Winson Chung82963d52013-10-09 11:20:57 -07001074
Adam Cohen06dff352012-06-01 17:17:08 -07001075 // It is possible that widgets can receive updates while launcher is not in the foreground.
1076 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1077 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1078 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001079 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001080 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001081
Michael Jurka447bf842013-05-15 14:52:15 +02001082 if (DEBUG_RESUME_TIME) {
1083 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1084 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001085
1086 if (mWorkspace.getCustomContentCallbacks() != null) {
1087 // If we are resuming and the custom content is the current page, we call onShow().
1088 // It is also poassible that onShow will instead be called slightly after first layout
1089 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1090 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001091 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001092 }
1093 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001094 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001095 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001096
Sunny Goyal756adbc2015-04-16 15:20:51 -07001097 if (!isWorkspaceLoading()) {
1098 // Process any items that were added while Launcher was away.
1099 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1100 }
Adam Cohen9211d422014-10-07 18:14:53 -07001101
1102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onResume();
1104 }
Adam Cohen06dff352012-06-01 17:17:08 -07001105 }
1106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001108 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001109 // Ensure that items added to Launcher are queued until Launcher returns
1110 InstallShortcutReceiver.enableInstallQueue();
1111
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001113 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001114 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001115 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001116
1117 // We call onHide() aggressively. The custom content callbacks should be able to
1118 // debounce excess onHide calls.
1119 if (mWorkspace.getCustomContentCallbacks() != null) {
1120 mWorkspace.getCustomContentCallbacks().onHide();
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Adam Cohen9211d422014-10-07 18:14:53 -07001123 if (mLauncherCallbacks != null) {
1124 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001125 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001126 }
1127
1128 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001129 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1130 // by a onResume or by scrolling otherwise.
1131 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001132
1133 // Custom content is completely hidden
1134 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001135
1136 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1137 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001138
1139 // Indicates whether the user is allowed to scroll away from the custom content.
1140 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001141 }
1142
Adam Cohenc2d6e892014-10-16 09:49:24 -07001143 public interface LauncherOverlay {
1144
1145 /**
1146 * Touch interaction leading to overscroll has begun
1147 */
1148 public void onScrollInteractionBegin();
1149
1150 /**
1151 * Touch interaction related to overscroll has ended
1152 */
1153 public void onScrollInteractionEnd();
1154
1155 /**
1156 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1157 * screen (or in the case of RTL, the rightmost screen).
1158 */
1159 public void onScrollChange(int progress, boolean rtl);
1160
1161 /**
1162 * Screen has stopped scrolling
1163 */
1164 public void onScrollSettled();
1165
1166 /**
1167 * This method can be called by the Launcher in order to force the LauncherOverlay
1168 * to exit fully immersive mode.
1169 */
1170 public void forceExitFullImmersion();
1171 }
1172
Winson Chung0f785722015-04-08 10:27:49 -07001173 public interface LauncherAppsCallbacks {
1174 /**
1175 * Updates launcher to the available space that AllApps can take so as not to overlap with
1176 * any other views.
1177 */
1178 public void onAllAppsBoundsChanged(Rect bounds);
1179
1180 /**
1181 * Called to dismiss all apps if it is showing.
1182 */
1183 public void dismissAllApps();
1184 }
1185
Adam Cohenc2d6e892014-10-16 09:49:24 -07001186 public interface LauncherOverlayCallbacks {
1187 /**
1188 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1189 * however it doesn't modify any state within the launcher.
1190 */
1191 public boolean canEnterFullImmersion();
1192
1193 /**
1194 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1195 * eg. by occupying the full screen and handling all touch events.
1196 *
1197 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1198 * case, Launcher will modify any necessary state and assumes the overlay is
1199 * handling all interaction. If false, the LauncherOverlay should cancel any
1200 *
1201 */
1202 public boolean enterFullImmersion();
1203
1204 /**
1205 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1206 * full control over UI and state.
1207 */
1208 public void exitFullImmersion();
1209 }
1210
1211 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1212
1213 @Override
1214 public boolean canEnterFullImmersion() {
1215 return mState == State.WORKSPACE;
1216 }
1217
1218 @Override
1219 public boolean enterFullImmersion() {
1220 if (mState == State.WORKSPACE) {
1221 // When fully immersed, disregard any touches which fall through.
1222 mDragLayer.setBlockTouch(true);
1223 return true;
1224 }
1225 return false;
1226 }
1227
1228 @Override
1229 public void exitFullImmersion() {
1230 mDragLayer.setBlockTouch(false);
1231 }
1232 }
1233
Jorim Jaggid017f882014-01-14 17:08:48 -08001234 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001235 if (mLauncherCallbacks != null) {
1236 return mLauncherCallbacks.hasSettings();
1237 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001238 return false;
1239 }
1240
Adam Cohen9211d422014-10-07 18:14:53 -07001241 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001242 CustomContentCallbacks callbacks, String description) {
1243 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001244 }
1245
Adam Cohenedb40762013-07-18 16:45:45 -07001246 // The custom content needs to offset its content to account for the QSB
1247 public int getTopOffsetForCustomContent() {
1248 return mWorkspace.getPaddingTop();
1249 }
1250
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001251 @Override
1252 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001254 if (mModel.isCurrentCallbacks(this)) {
1255 mModel.stopLoader();
1256 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001257 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1258
Romain Guy13c2e7b2010-03-10 19:45:00 -08001259 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001260 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001262 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 @Override
1264 public void onWindowFocusChanged(boolean hasFocus) {
1265 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001266 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001267
1268 if (mLauncherCallbacks != null) {
1269 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1270 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 private boolean acceptFilter() {
1274 final InputMethodManager inputManager = (InputMethodManager)
1275 getSystemService(Context.INPUT_METHOD_SERVICE);
1276 return !inputManager.isFullscreenMode();
1277 }
1278
1279 @Override
1280 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001281 final int uniChar = event.getUnicodeChar();
1282 final boolean handled = super.onKeyDown(keyCode, event);
1283 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1284 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1286 keyCode, event);
1287 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001288 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001289 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 // showSearchDialog()
1291 // If there are multiple keystrokes before the search dialog takes focus,
1292 // onSearchRequested() will be called for every keystroke,
1293 // but it is idempotent, so it's fine.
1294 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296 }
1297
Joe Onorato8a9625e2010-01-28 15:55:35 -08001298 // Eat the long press event so the keyboard doesn't come up.
1299 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1300 return true;
1301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 return handled;
1304 }
1305
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 private String getTypedText() {
1307 return mDefaultKeySsb.toString();
1308 }
1309
1310 private void clearTypedText() {
1311 mDefaultKeySsb.clear();
1312 mDefaultKeySsb.clearSpans();
1313 Selection.setSelection(mDefaultKeySsb, 0);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001317 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1318 * State
1319 */
1320 private static State intToState(int stateOrdinal) {
1321 State state = State.WORKSPACE;
1322 final State[] stateValues = State.values();
1323 for (int i = 0; i < stateValues.length; i++) {
1324 if (stateValues[i].ordinal() == stateOrdinal) {
1325 state = stateValues[i];
1326 break;
1327 }
1328 }
1329 return state;
1330 }
1331
1332 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 * Restores the previous state, if it exists.
1334 *
1335 * @param savedState The previous state.
1336 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001337 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 private void restoreState(Bundle savedState) {
1339 if (savedState == null) {
1340 return;
1341 }
1342
Michael Jurka883f55b2010-10-21 15:47:14 -07001343 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001344 if (state == State.APPS || state == State.WIDGETS) {
1345 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001346 }
1347
Adam Cohen21cd0022013-10-09 18:57:02 -07001348 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1349 PagedView.INVALID_RESTORE_PAGE);
1350 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001351 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001355 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1358 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001359 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1361 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001362 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1363 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001364 AppWidgetProviderInfo info = savedState.getParcelable(
1365 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1366 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001367 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001368 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 mRestoring = true;
1370 }
1371
1372 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1373 if (renameFolder) {
1374 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001375 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 mRestoring = true;
1377 }
Winson Chunga12a2502010-12-20 14:41:35 -08001378
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001379 mItemIdToViewId = (HashMap<Integer, Integer>)
1380 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
1383 /**
1384 * Finds all the views we need and configure them properly.
1385 */
1386 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001387 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001388
Craig Mautner360310b2012-10-26 15:13:08 -07001389 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001390 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001391 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1392 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001393 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001394 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001395
John Spurlock77e1f472013-09-11 10:09:51 -04001396 mLauncherView.setSystemUiVisibility(
1397 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001398 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399
Winson Chung4c98d922011-05-31 16:50:48 -07001400 // Setup the drag layer
1401 mDragLayer.setup(this, dragController);
1402
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 // Setup the hotseat
1404 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1405 if (mHotseat != null) {
1406 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001407 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001408 }
1409
Jorim Jaggid017f882014-01-14 17:08:48 -08001410 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001411 View widgetButton = findViewById(R.id.widget_button);
1412 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001413 @Override
1414 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001415 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001416 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001417 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 }
1419 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001420 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1421
1422 View wallpaperButton = findViewById(R.id.wallpaper_button);
1423 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 @Override
1425 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001426 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001427 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001428 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001429 }
1430 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001431 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1432
1433 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001434 if (hasSettings()) {
1435 settingsButton.setOnClickListener(new OnClickListener() {
1436 @Override
1437 public void onClick(View arg0) {
1438 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001439 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001441 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001442 });
1443 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1444 } else {
1445 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 }
1447
Adam Cohendbdff6b2013-09-18 19:09:15 -07001448 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001449
Winson Chung4c98d922011-05-31 16:50:48 -07001450 // Setup the workspace
1451 mWorkspace.setHapticFeedbackEnabled(false);
1452 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001453 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001454 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001455
Winson Chungf0ea4d32011-06-06 14:27:16 -07001456 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001457 mSearchDropTargetBar = (SearchDropTargetBar)
1458 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001459
Winson Chungb745afb2015-03-02 11:51:23 -08001460 // Setup Apps
1461 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001462 if (isAllAppsSearchOverridden()) {
1463 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001464 }
Winson Chungb745afb2015-03-02 11:51:23 -08001465
Winson Chungf0ea4d32011-06-06 14:27:16 -07001466 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001467 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001468
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001470 dragController.setDragScoller(mWorkspace);
1471 dragController.setScrollView(mDragLayer);
1472 dragController.setMoveTarget(mWorkspace);
1473 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001474 if (mSearchDropTargetBar != null) {
1475 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001476 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001477 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001478
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001479 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001480 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001481 mWeightWatcher = new WeightWatcher(this);
1482 mWeightWatcher.setAlpha(0.5f);
1483 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001484 new FrameLayout.LayoutParams(
1485 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001486 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001487 Gravity.BOTTOM)
1488 );
Adam Cohen39a06042013-07-19 14:30:12 -07001489
1490 boolean show = shouldShowWeightWatcher();
1491 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
1495 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001496 * Sets the all apps button. This method is called from {@link Hotseat}.
1497 */
1498 public void setAllAppsButton(View allAppsButton) {
1499 mAllAppsButton = allAppsButton;
1500 }
1501
1502 public View getAllAppsButton() {
1503 return mAllAppsButton;
1504 }
1505
1506 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 * Creates a view representing a shortcut.
1508 *
1509 * @param info The data structure describing the shortcut.
1510 *
1511 * @return A View inflated from R.layout.application.
1512 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001513 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001515 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
1518 /**
1519 * Creates a view representing a shortcut inflated from the specified resource.
1520 *
1521 * @param layoutResId The id of the XML layout used to create the shortcut.
1522 * @param parent The group the shortcut belongs to.
1523 * @param info The data structure describing the shortcut.
1524 *
1525 * @return A View inflated from layoutResId.
1526 */
Adam Cohen59400422014-03-05 18:07:04 -08001527 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001528 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001529 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001531 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 return favorite;
1533 }
1534
1535 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 * Add a shortcut to the workspace.
1537 *
1538 * @param data The intent describing the shortcut.
1539 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001541 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 int cellY) {
1543 int[] cellXY = mTmpAddItemCellCoordinates;
1544 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001546
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001548
Sunny Goyal2350bc92014-10-14 16:42:54 -07001549 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001550 if (info == null) {
1551 return;
1552 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 final View view = createShortcut(info);
1554
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 // First we check if we already know the exact location where we want to add this item.
1556 if (cellX >= 0 && cellY >= 0) {
1557 cellXY[0] = cellX;
1558 cellXY[1] = cellY;
1559 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001560
1561 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001562 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001563 true, null,null)) {
1564 return;
1565 }
1566 DragObject dragObject = new DragObject();
1567 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001568 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1569 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001570 return;
1571 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001572 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001573 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001574 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001575 foundCellSpan = (result != null);
1576 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001577 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001578 }
1579
1580 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001581 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001582 return;
1583 }
1584
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001585 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586
1587 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591 }
1592
Adam Cohen2f093b62012-04-30 18:59:53 -07001593 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1594 int minHeight) {
1595 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001596 // We want to account for the extra amount of padding that we are adding to the widget
1597 // to ensure that it gets the full amount of space that it has requested
1598 int requiredWidth = minWidth + padding.left + padding.right;
1599 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001600 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001601 }
1602
Adam Cohen2f093b62012-04-30 18:59:53 -07001603 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1604 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001605 }
1606
Adam Cohen2f093b62012-04-30 18:59:53 -07001607 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1608 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001609 }
1610
Adam Cohen2f093b62012-04-30 18:59:53 -07001611 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1612 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001613 }
1614
Adam Cohen2f093b62012-04-30 18:59:53 -07001615 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1616 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001617 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001621 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001623 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 */
Adam Cohen091440a2015-03-18 14:16:05 -07001625 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001626 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1627
1628 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001629 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001630 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1631 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Adam Cohen59400422014-03-05 18:07:04 -08001634 if (appWidgetInfo.isCustomWidget) {
1635 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001636 }
1637
Adam Cohen59400422014-03-05 18:07:04 -08001638 LauncherAppWidgetInfo launcherInfo;
1639 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1640 launcherInfo.spanX = info.spanX;
1641 launcherInfo.spanY = info.spanY;
1642 launcherInfo.minSpanX = info.minSpanX;
1643 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001644 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001647 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
1649 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001650 if (hostView == null) {
1651 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001652 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1653 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001654 } else {
1655 // The AppWidgetHostView has already been inflated and instantiated
1656 launcherInfo.hostView = hostView;
1657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001659 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001660 launcherInfo.notifyWidgetSizeChanged(this);
1661
Adam Cohen59400422014-03-05 18:07:04 -08001662 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1663 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001664
1665 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001667 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1671 @Override
1672 public void onReceive(Context context, Intent intent) {
1673 final String action = intent.getAction();
1674 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1675 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001676 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001677 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001678
Winson Chungc100e8e2011-08-09 16:02:43 -07001679 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001680 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001681 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001682 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001683 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001684 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1686 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001687 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1689 mModel.resetLoadedState(false, true);
1690 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1691 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1692 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1693 mModel.resetLoadedState(false, true);
1694 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1695 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1696 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 }
1698 }
1699 };
1700
1701 @Override
1702 public void onAttachedToWindow() {
1703 super.onAttachedToWindow();
1704
1705 // Listen for broadcasts related to user-presence
1706 final IntentFilter filter = new IntentFilter();
1707 filter.addAction(Intent.ACTION_SCREEN_OFF);
1708 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001709 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001710 if (ENABLE_DEBUG_INTENTS) {
1711 filter.addAction(DebugIntents.DELETE_DATABASE);
1712 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1713 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001715 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001716 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001717 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 mVisible = true;
1719 }
1720
Adam Cohen0b395352014-06-09 22:54:36 +00001721 /**
1722 * Sets up transparent navigation and status bars in LMP.
1723 * This method is a no-op for other platform versions.
1724 */
Sunny Goyald0091012015-01-20 15:55:34 -08001725 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001726 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001727 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001728 Window window = getWindow();
1729 window.getAttributes().systemUiVisibility |=
1730 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1731 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1732 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1733 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1734 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1735 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1736 window.setStatusBarColor(Color.TRANSPARENT);
1737 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001738 }
1739 }
1740
Adam Cohended9f8d2010-11-03 13:25:16 -07001741 @Override
1742 public void onDetachedFromWindow() {
1743 super.onDetachedFromWindow();
1744 mVisible = false;
1745
Adam Cohend113e0c2010-11-11 10:48:05 -08001746 if (mAttached) {
1747 unregisterReceiver(mReceiver);
1748 mAttached = false;
1749 }
Winson Chungb745afb2015-03-02 11:51:23 -08001750 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752
1753 public void onWindowVisibilityChanged(int visibility) {
1754 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001755 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001756 // The following code used to be in onResume, but it turns out onResume is called when
1757 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1758 // is a more appropriate event to handle
1759 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001760 if (!mWorkspaceLoading) {
1761 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 // We want to let Launcher draw itself at least once before we force it to build
1763 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001764 // apps is nice and speedy.
1765 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001766 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001767 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 if (mStarted) return;
1769 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001770 // We delay the layer building a bit in order to give
1771 // other message processing a time to run. In particular
1772 // this avoids a delay in hiding the IME if it was
1773 // currently shown, because doing that may involve
1774 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001775 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001776 final ViewTreeObserver.OnDrawListener listener = this;
1777 mWorkspace.post(new Runnable() {
1778 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001779 if (mWorkspace != null &&
1780 mWorkspace.getViewTreeObserver() != null) {
1781 mWorkspace.getViewTreeObserver().
1782 removeOnDrawListener(listener);
1783 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001784 }
1785 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001786 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001787 }
1788 });
1789 }
1790 clearTypedText();
1791 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793
Adam Cohen091440a2015-03-18 14:16:05 -07001794 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 mHandler.removeMessages(ADVANCE_MSG);
1796 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1797 mHandler.sendMessageDelayed(msg, delay);
1798 mAutoAdvanceSentTime = System.currentTimeMillis();
1799 }
1800
Adam Cohen091440a2015-03-18 14:16:05 -07001801 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1803 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1804 mAutoAdvanceRunning = autoAdvanceRunning;
1805 if (autoAdvanceRunning) {
1806 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1807 sendAdvanceMessage(delay);
1808 } else {
1809 if (!mWidgetsToAdvance.isEmpty()) {
1810 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1811 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1812 }
1813 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001814 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 }
1816 }
1817 }
1818
1819 private final Handler mHandler = new Handler() {
1820 @Override
1821 public void handleMessage(Message msg) {
1822 if (msg.what == ADVANCE_MSG) {
1823 int i = 0;
1824 for (View key: mWidgetsToAdvance.keySet()) {
1825 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1826 final int delay = mAdvanceStagger * i;
1827 if (v instanceof Advanceable) {
1828 postDelayed(new Runnable() {
1829 public void run() {
1830 ((Advanceable) v).advance();
1831 }
1832 }, delay);
1833 }
1834 i++;
1835 }
1836 sendAdvanceMessage(mAdvanceInterval);
1837 }
1838 }
1839 };
1840
1841 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001842 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1844 if (v instanceof Advanceable) {
1845 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001846 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001847 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001848 }
1849 }
1850
1851 void removeWidgetToAutoAdvance(View hostView) {
1852 if (mWidgetsToAdvance.containsKey(hostView)) {
1853 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001854 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001855 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001856 }
1857
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 launcherInfo.hostView = null;
1861 }
1862
Hyunyoung Song3f471442015-04-08 19:01:34 -07001863 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001864 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1865 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001866 }
1867
Winson Chunga6945242014-01-08 14:04:34 -08001868 public DragLayer getDragLayer() {
1869 return mDragLayer;
1870 }
1871
Winson Chungb745afb2015-03-02 11:51:23 -08001872 public AppsContainerView getAppsView() {
1873 return mAppsView;
1874 }
1875
Hyunyoung Song3f471442015-04-08 19:01:34 -07001876 public WidgetsContainerView getWidgetsView() {
1877 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001878 }
1879
Winson Chunga6945242014-01-08 14:04:34 -08001880 public Workspace getWorkspace() {
1881 return mWorkspace;
1882 }
1883
1884 public Hotseat getHotseat() {
1885 return mHotseat;
1886 }
1887
Jorim Jaggid017f882014-01-14 17:08:48 -08001888 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001889 return mOverviewPanel;
1890 }
1891
1892 public SearchDropTargetBar getSearchBar() {
1893 return mSearchDropTargetBar;
1894 }
1895
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001896 public LauncherAppWidgetHost getAppWidgetHost() {
1897 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
Romain Guycbb89e42009-06-08 15:52:54 -07001899
Winson Chunga9abd0e2010-10-27 17:18:37 -07001900 public LauncherModel getModel() {
1901 return mModel;
1902 }
1903
Winson Chunga6945242014-01-08 14:04:34 -08001904 protected SharedPreferences getSharedPrefs() {
1905 return mSharedPrefs;
1906 }
1907
Bjorn Bringertc459e522013-06-07 19:36:01 +01001908 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001909 getWindow().closeAllPanels();
1910
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001911 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001912 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001913 }
1914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 @Override
1916 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001917 long startTime = 0;
1918 if (DEBUG_RESUME_TIME) {
1919 startTime = System.currentTimeMillis();
1920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 super.onNewIntent(intent);
1922
1923 // Close the menu
1924 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001925 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001926 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001927
Adam Cohened307df2013-10-02 09:37:31 -07001928 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1929 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1930 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001931
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 if (mWorkspace == null) {
1933 // Can be cases where mWorkspace is null, this prevents a NPE
1934 return;
1935 }
1936 Folder openFolder = mWorkspace.getOpenFolder();
1937 // In all these cases, only animate if we're already on home
1938 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001939
1940 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1941 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001943 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001944 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001945 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001946
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 closeFolder();
1948 exitSpringLoadedDragMode();
1949
1950 // If we are already on home, then just animate back to the workspace,
1951 // otherwise, just wait until onResume to set the state back to Workspace
1952 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001953 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001954 } else {
1955 mOnResumeState = State.WORKSPACE;
1956 }
1957
1958 final View v = getWindow().peekDecorView();
1959 if (v != null && v.getWindowToken() != null) {
1960 InputMethodManager imm = (InputMethodManager)getSystemService(
1961 INPUT_METHOD_SERVICE);
1962 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1963 }
1964
Winson Chungb745afb2015-03-02 11:51:23 -08001965 // Reset the apps view
1966 if (!alreadyOnHome && mAppsView != null) {
1967 mAppsView.scrollToTop();
1968 }
1969
Hyunyoung Song3f471442015-04-08 19:01:34 -07001970 // Reset the widgets view
1971 if (!alreadyOnHome && mWidgetsView != null) {
1972 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001973 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001974
Adam Cohen9211d422014-10-07 18:14:53 -07001975 if (mLauncherCallbacks != null) {
1976 mLauncherCallbacks.onHomeIntent();
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
Adam Cohened307df2013-10-02 09:37:31 -07001979
Michael Jurka447bf842013-05-15 14:52:15 +02001980 if (DEBUG_RESUME_TIME) {
1981 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
Adam Cohen9211d422014-10-07 18:14:53 -07001984 if (mLauncherCallbacks != null) {
1985 mLauncherCallbacks.onNewIntent(intent);
1986 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001987 }
1988
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001990 public void onRestoreInstanceState(Bundle state) {
1991 super.onRestoreInstanceState(state);
1992 for (int page: mSynchronouslyBoundPages) {
1993 mWorkspace.restoreInstanceStateForChild(page);
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 @Override
1998 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001999 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002000 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2001 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002002 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002003 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004
Michael Jurka883f55b2010-10-21 15:47:14 -07002005 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002006 // We close any open folder since it will not be re-opened, and we need to make sure
2007 // this state is reflected.
2008 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009
Adam Cohendcd297f2013-06-18 13:13:40 -07002010 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002011 mWaitingForResult) {
2012 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002013 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002014 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2015 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002016 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2017 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2018 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002019 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
2022 if (mFolderInfo != null && mWaitingForResult) {
2023 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2024 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2025 }
Michael Jurka946ad472010-07-09 18:05:18 -07002026
Hyunyoung Song3f471442015-04-08 19:01:34 -07002027 // Save the current widgets tray?
2028 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002029 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002030
2031 if (mLauncherCallbacks != null) {
2032 mLauncherCallbacks.onSaveInstanceState(outState);
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
2036 @Override
2037 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Winson Chunge7a03942011-08-05 15:05:12 -07002040 // Remove all pending runnables
2041 mHandler.removeMessages(ADVANCE_MSG);
2042 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002043 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002044
Winson Chungcd2b0142011-06-08 16:02:26 -07002045 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002046 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002047
2048 // It's possible to receive onDestroy after a new Launcher activity has
2049 // been created. In this case, don't interfere with the new Launcher.
2050 if (mModel.isCurrentCallbacks(this)) {
2051 mModel.stopLoader();
2052 app.setLauncher(null);
2053 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002056 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002058 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060 mAppWidgetHost = null;
2061
2062 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063
2064 TextKeyListener.getInstance().release();
2065
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002066 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002067 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002068
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002069 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002070 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002071 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002072 mWorkspace = null;
2073 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002074
Michael Jurka2ecf9952012-06-18 12:52:28 -07002075 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002076
2077 if (mLauncherCallbacks != null) {
2078 mLauncherCallbacks.onDestroy();
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Adam Cohena9cf38f2011-05-02 15:36:58 -07002082 public DragController getDragController() {
2083 return mDragController;
2084 }
2085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 @Override
2087 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002088 onStartForResult(requestCode);
2089 super.startActivityForResult(intent, requestCode);
2090 }
2091
2092 @Override
2093 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2094 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2095 onStartForResult(requestCode);
2096 try {
2097 super.startIntentSenderForResult(intent, requestCode,
2098 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2099 } catch (IntentSender.SendIntentException e) {
2100 throw new ActivityNotFoundException();
2101 }
2102 }
2103
2104 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002105 if (requestCode >= 0) {
2106 setWaitingForResult(true);
2107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109
Winson Chung70d72102011-08-12 11:24:35 -07002110 /**
2111 * Indicates that we want global search for this activity by setting the globalSearch
2112 * argument for {@link #startSearch} to true.
2113 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002115 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002117
Karl Rosaen138a0412009-04-23 19:00:21 -07002118 if (initialQuery == null) {
2119 // Use any text typed in the launcher as the initial query
2120 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 if (appSearchData == null) {
2123 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002124 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
Winson Chungadf0c182012-08-23 14:59:07 -07002126 Rect sourceBounds = new Rect();
2127 if (mSearchDropTargetBar != null) {
2128 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2129 }
Romain Guycbb89e42009-06-08 15:52:54 -07002130
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002133 if (clearTextImmediately) {
2134 clearTypedText();
2135 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002136
2137 // We need to show the workspace after starting the search
2138 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 }
2140
Ian Parkinson047036e2014-09-01 15:40:53 +01002141 /**
2142 * Start a text search.
2143 *
2144 * @return {@code true} if the search will start immediately, so any further keypresses
2145 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2146 * to buffer keypresses.
2147 */
2148 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002149 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002150 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2151 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2152 sourceBounds);
2153 }
2154
Michael Jurkaa33411c2012-06-14 16:18:21 -07002155 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002157 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002158 }
2159
2160 /**
2161 * Starts the global search activity. This code is a copied from SearchManager
2162 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002163 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002165 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002166 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2167 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2168 if (globalSearchActivity == null) {
2169 Log.w(TAG, "No global search activity found.");
2170 return;
2171 }
2172 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2173 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2174 intent.setComponent(globalSearchActivity);
2175 // Make sure that we have a Bundle to put source in
2176 if (appSearchData == null) {
2177 appSearchData = new Bundle();
2178 } else {
2179 appSearchData = new Bundle(appSearchData);
2180 }
Adam Cohen9211d422014-10-07 18:14:53 -07002181 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002182 if (!appSearchData.containsKey("source")) {
2183 appSearchData.putString("source", getPackageName());
2184 }
2185 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2186 if (!TextUtils.isEmpty(initialQuery)) {
2187 intent.putExtra(SearchManager.QUERY, initialQuery);
2188 }
2189 if (selectInitialQuery) {
2190 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2191 }
2192 intent.setSourceBounds(sourceBounds);
2193 try {
2194 startActivity(intent);
2195 } catch (ActivityNotFoundException ex) {
2196 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199
Yura4f93ec62014-02-04 14:15:21 +00002200 public boolean isOnCustomContent() {
2201 return mWorkspace.isOnOrMovingToCustomContent();
2202 }
2203
Winson Chung70d72102011-08-12 11:24:35 -07002204 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002205 public boolean onPrepareOptionsMenu(Menu menu) {
2206 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002207 if (!isOnCustomContent()) {
2208 // Close any open folders
2209 closeFolder();
2210 // Stop resizing any widgets
2211 mWorkspace.exitWidgetResizeMode();
2212 if (!mWorkspace.isInOverviewMode()) {
2213 // Show the overview mode
2214 showOverviewMode(true);
2215 } else {
2216 showWorkspace(true);
2217 }
Winson Chunge0298742014-01-17 12:03:00 -08002218 }
Adam Cohen9211d422014-10-07 18:14:53 -07002219 if (mLauncherCallbacks != null) {
2220 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2221 }
2222
Michael Jurkab94f3f82013-09-11 18:08:54 +02002223 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002226 @Override
2227 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002228 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002229 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002230 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002231 }
2232
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 public boolean isWorkspaceLocked() {
2234 return mWorkspaceLoading || mWaitingForResult;
2235 }
2236
Adam Cohen517a7f52014-03-01 12:12:59 -08002237 public boolean isWorkspaceLoading() {
2238 return mWorkspaceLoading;
2239 }
2240
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002241 private void setWorkspaceLoading(boolean value) {
2242 boolean isLocked = isWorkspaceLocked();
2243 mWorkspaceLoading = value;
2244 if (isLocked != isWorkspaceLocked()) {
2245 onWorkspaceLockedChanged();
2246 }
2247 }
2248
2249 private void setWaitingForResult(boolean value) {
2250 boolean isLocked = isWorkspaceLocked();
2251 mWaitingForResult = value;
2252 if (isLocked != isWorkspaceLocked()) {
2253 onWorkspaceLockedChanged();
2254 }
2255 }
2256
Adam Cohen9211d422014-10-07 18:14:53 -07002257 protected void onWorkspaceLockedChanged() {
2258 if (mLauncherCallbacks != null) {
2259 mLauncherCallbacks.onWorkspaceLockedChanged();
2260 }
2261 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002262
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2267 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002268 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002271
Sunny Goyale6b63a32015-01-16 16:14:29 -08002272 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002273 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002274 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2275 }
2276
Sunny Goyale6b63a32015-01-16 16:14:29 -08002277 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002278 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2279 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002280 if (appWidgetInfo.configure != null) {
2281 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002282 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002283
Bjorn Bringert7984c942009-12-09 15:38:25 +00002284 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002285 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2286 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002289 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002290 Runnable onComplete = new Runnable() {
2291 @Override
2292 public void run() {
2293 // Exit spring loaded mode if necessary after adding the widget
2294 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2295 null);
2296 }
2297 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002299 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002300 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
2302 }
Romain Guycbb89e42009-06-08 15:52:54 -07002303
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002304 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002305 // Close any folders that may be open.
2306 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002307 mWorkspace.moveToCustomContentScreen(animate);
2308 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002309
2310 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2311 int[] cell, int spanX, int spanY) {
2312 switch (info.itemType) {
2313 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2314 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2315 int span[] = new int[2];
2316 span[0] = spanX;
2317 span[1] = spanY;
2318 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2319 container, screenId, cell, span);
2320 break;
2321 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2322 processShortcutFromDrop(info.componentName, container, screenId, cell);
2323 break;
2324 default:
2325 throw new IllegalStateException("Unknown item type: " + info.itemType);
2326 }
2327 }
2328
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 /**
2330 * Process a shortcut drop.
2331 *
2332 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002336 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2337 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002338 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002341 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342
2343 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 mPendingAddInfo.cellX = cell[0];
2345 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002347
2348 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2349 createShortcutIntent.setComponent(componentName);
2350 processShortcut(createShortcutIntent);
2351 }
2352
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 /**
2354 * Process a widget drop.
2355 *
2356 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002360 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2361 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002365 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002366 mPendingAddInfo.minSpanX = info.minSpanX;
2367 mPendingAddInfo.minSpanY = info.minSpanY;
2368
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 mPendingAddInfo.cellX = cell[0];
2371 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002372 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002373 if (span != null) {
2374 mPendingAddInfo.spanX = span[0];
2375 mPendingAddInfo.spanY = span[1];
2376 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002377
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 AppWidgetHostView hostView = info.boundWidget;
2379 int appWidgetId;
2380 if (hostView != null) {
2381 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002382 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002383 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002384 // In this case, we either need to start an activity to get permission to bind
2385 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002386 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002387 Bundle options = info.bindOptions;
2388
Sunny Goyalffe83f12014-08-14 17:39:34 -07002389 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2390 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002391 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002392 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002393 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002394 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2396 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2397 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002398 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2399 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002400 // TODO: we need to make sure that this accounts for the options bundle.
2401 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002402 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2403 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002404 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002405 }
2406
Joe Onoratodeb98af2010-02-19 14:59:39 -08002407 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002408 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410
Winson Chung24ab2f12010-09-16 14:10:47 -07002411 void processWallpaper(Intent intent) {
2412 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2413 }
2414
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002416 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002417 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 folderInfo.title = getText(R.string.folder_name);
2419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002421 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2422 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002423 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424
2425 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 FolderIcon newFolder =
2427 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002428 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002430 // Force measure the new folder icon
2431 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2432 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002433 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 }
Romain Guycbb89e42009-06-08 15:52:54 -07002435
Joe Onorato9c1289c2009-08-17 11:03:03 -04002436 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002437 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002438 }
2439
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002440 /**
2441 * Registers various content observers. The current implementation registers
2442 * only a favorites observer to keep track of the favorites applications.
2443 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002444 private void registerContentObservers() {
2445 ContentResolver resolver = getContentResolver();
2446 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2447 true, mWidgetObserver);
2448 }
2449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 @Override
2451 public boolean dispatchKeyEvent(KeyEvent event) {
2452 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2453 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002456 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002457 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002458 dumpState();
2459 return true;
2460 }
2461 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002462 }
2463 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2464 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002465 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 return true;
2467 }
2468 }
2469
2470 return super.dispatchKeyEvent(event);
2471 }
2472
Joe Onorato88ec0992009-11-19 13:16:06 -08002473 @Override
2474 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002475 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2476 return;
2477 }
2478
Winson Chung3d9490a2015-03-24 17:38:42 -07002479 LauncherAccessibilityDelegate delegate =
2480 LauncherAppState.getInstance().getAccessibilityDelegate();
2481 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002482 return;
2483 }
2484
Winson Chungb745afb2015-03-02 11:51:23 -08002485 if (isAppsViewVisible()) {
2486 showWorkspace(true);
2487 } else if (isWidgetsViewVisible()) {
2488 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002489 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002490 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002491 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002492 Folder openFolder = mWorkspace.getOpenFolder();
2493 if (openFolder.isEditingName()) {
2494 openFolder.dismissEditingName();
2495 } else {
2496 closeFolder();
2497 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002498 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002499 mWorkspace.exitWidgetResizeMode();
2500
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002501 // Back button is a no-op here, but give at least some feedback for the button press
2502 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002503 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002504 }
2505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002507 * Re-listen when widgets are reset.
2508 */
Adam Cohen091440a2015-03-18 14:16:05 -07002509 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002510 if (mAppWidgetHost != null) {
2511 mAppWidgetHost.startListening();
2512 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002513 }
2514
2515 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 * Launches the intent referred by the clicked shortcut.
2517 *
2518 * @param v The view representing the clicked shortcut.
2519 */
2520 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2522 // view has detached (it's possible for this to happen if the view is removed mid touch).
2523 if (v.getWindowToken() == null) {
2524 return;
2525 }
2526
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002527 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002528 return;
2529 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002530
Adam Cohen1697b792013-09-17 19:08:21 -07002531 if (v instanceof Workspace) {
2532 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002533 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
2535 return;
2536 }
2537
2538 if (v instanceof CellLayout) {
2539 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002540 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002541 }
2542 }
2543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002545 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002548 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002549 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002550 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002551 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002553 } else if (tag instanceof AppInfo) {
2554 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002555 } else if (tag instanceof LauncherAppWidgetInfo) {
2556 if (v instanceof PendingAppWidgetHostView) {
2557 onClickPendingWidget((PendingAppWidgetHostView) v);
2558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 }
2560 }
2561
Sunny Goyal508da152014-08-14 10:53:27 -07002562 public void onClickPagedViewIcon(View v) {
2563 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002564 if (mLauncherCallbacks != null) {
2565 mLauncherCallbacks.onClickPagedViewIcon(v);
2566 }
Sunny Goyal508da152014-08-14 10:53:27 -07002567 }
2568
Michael Jurka0e260592010-06-30 17:07:39 -07002569 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002570 return false;
2571 }
2572
Michael Jurkaaf442092010-06-10 17:01:57 -07002573 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002574 * Event handler for the app widget view which has not fully restored.
2575 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002577 if (mIsSafeModeEnabled) {
2578 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2579 return;
2580 }
2581
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002582 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002583 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002584 int widgetId = info.appWidgetId;
2585 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2586 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002587 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2588 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002589 mPendingAddInfo.copyFrom(info);
2590 mPendingAddWidgetId = widgetId;
2591
Sunny Goyalffe83f12014-08-14 17:39:34 -07002592 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2593 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 } else if (info.installProgress < 0) {
2596 // The install has not been queued
2597 final String packageName = info.providerName.getPackageName();
2598 showBrokenAppInstallDialog(packageName,
2599 new DialogInterface.OnClickListener() {
2600 public void onClick(DialogInterface dialog, int id) {
2601 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2602 }
2603 });
2604 } else {
2605 // Download has started.
2606 final String packageName = info.providerName.getPackageName();
2607 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002608 }
2609 }
2610
2611 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002612 * Event handler for the "grid" button that appears on the home screen, which
2613 * enters all apps mode.
2614 *
2615 * @param v The view that was clicked.
2616 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 protected void onClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002619 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 showWorkspace(true);
2621 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002622 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 }
2624 }
2625
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002626 private void showBrokenAppInstallDialog(final String packageName,
2627 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002628 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002629 .setTitle(R.string.abandoned_promises_title)
2630 .setMessage(R.string.abandoned_promise_explanation)
2631 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2632 .setNeutralButton(R.string.abandoned_clean_this,
2633 new DialogInterface.OnClickListener() {
2634 public void onClick(DialogInterface dialog, int id) {
2635 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2636 mWorkspace.removeAbandonedPromise(packageName, user);
2637 }
2638 })
2639 .create().show();
2640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 /**
2644 * Event handler for an app shortcut click.
2645 *
2646 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2647 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2650 Object tag = v.getTag();
2651 if (!(tag instanceof ShortcutInfo)) {
2652 throw new IllegalArgumentException("Input must be a Shortcut");
2653 }
2654
2655 // Open shortcut
2656 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002657
2658 if (shortcut.isDisabled != 0) {
2659 int error = R.string.activity_not_available;
2660 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2661 error = R.string.safemode_shortcut_error;
2662 }
2663 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2664 return;
2665 }
2666
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667 final Intent intent = shortcut.intent;
2668
2669 // Check for special shortcuts
2670 if (intent.getComponent() != null) {
2671 final String shortcutClass = intent.getComponent().getClassName();
2672
2673 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2674 MemoryDumpActivity.startDump(this);
2675 return;
2676 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2677 toggleShowWeightWatcher();
2678 return;
2679 }
2680 }
2681
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002683 if ((v instanceof BubbleTextView)
2684 && shortcut.isPromise()
2685 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002686 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002687 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 new DialogInterface.OnClickListener() {
2689 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002690 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 return;
2694 }
2695
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002697 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002698
2699 if (mLauncherCallbacks != null) {
2700 mLauncherCallbacks.onClickAppShortcut(v);
2701 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002702 }
2703
Adam Cohen091440a2015-03-18 14:16:05 -07002704 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002705 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002706 final ShortcutInfo shortcut;
2707 final Intent intent;
2708 if (tag instanceof ShortcutInfo) {
2709 shortcut = (ShortcutInfo) tag;
2710 intent = shortcut.intent;
2711 int[] pos = new int[2];
2712 v.getLocationOnScreen(pos);
2713 intent.setSourceBounds(new Rect(pos[0], pos[1],
2714 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002715
Sunny Goyal508da152014-08-14 10:53:27 -07002716 } else if (tag instanceof AppInfo) {
2717 shortcut = null;
2718 intent = ((AppInfo) tag).intent;
2719 } else {
2720 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2721 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002722
2723 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 mStats.recordLaunch(intent, shortcut);
2725
2726 if (success && v instanceof BubbleTextView) {
2727 mWaitingForResume = (BubbleTextView) v;
2728 mWaitingForResume.setStayPressed(true);
2729 }
2730 }
2731
2732 /**
2733 * Event handler for a folder icon click.
2734 *
2735 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2736 */
2737 protected void onClickFolderIcon(View v) {
2738 if (LOGD) Log.d(TAG, "onClickFolder");
2739 if (!(v instanceof FolderIcon)){
2740 throw new IllegalArgumentException("Input must be a FolderIcon");
2741 }
2742
2743 FolderIcon folderIcon = (FolderIcon) v;
2744 final FolderInfo info = folderIcon.getFolderInfo();
2745 Folder openFolder = mWorkspace.getFolderForTag(info);
2746
2747 // If the folder info reports that the associated folder is open, then verify that
2748 // it is actually opened. There have been a few instances where this gets out of sync.
2749 if (info.opened && openFolder == null) {
2750 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2751 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2752 info.opened = false;
2753 }
2754
2755 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2756 // Close any open folder
2757 closeFolder();
2758 // Open the requested folder
2759 openFolder(folderIcon);
2760 } else {
2761 // Find the open folder...
2762 int folderScreen;
2763 if (openFolder != null) {
2764 folderScreen = mWorkspace.getPageForView(openFolder);
2765 // .. and close it
2766 closeFolder(openFolder);
2767 if (folderScreen != mWorkspace.getCurrentPage()) {
2768 // Close any folder open on the current screen
2769 closeFolder();
2770 // Pull the folder onto this screen
2771 openFolder(folderIcon);
2772 }
2773 }
2774 }
Adam Cohen9211d422014-10-07 18:14:53 -07002775
2776 if (mLauncherCallbacks != null) {
2777 mLauncherCallbacks.onClickFolderIcon(v);
2778 }
Michael Jurka5130e402011-10-13 04:55:35 -07002779 }
2780
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002781 /**
2782 * Event handler for the (Add) Widgets button that appears after a long press
2783 * on the home screen.
2784 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002785 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002787 if (mIsSafeModeEnabled) {
2788 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2789 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002790 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onClickAddWidgetButton(view);
2793 }
Adam Cohen9211d422014-10-07 18:14:53 -07002794 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002795 }
2796
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 /**
2798 * Event handler for the wallpaper picker button that appears after a long press
2799 * on the home screen.
2800 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002801 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002803 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2804 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002805
2806 if (mLauncherCallbacks != null) {
2807 mLauncherCallbacks.onClickWallpaperPicker(v);
2808 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 }
2810
2811 /**
2812 * Event handler for a click on the settings button that appears after a long press
2813 * on the home screen.
2814 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002815 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002816 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002817 if (mLauncherCallbacks != null) {
2818 mLauncherCallbacks.onClickSettingsButton(v);
2819 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002820 }
2821
Michael Jurka5130e402011-10-13 04:55:35 -07002822 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002823 // Provide the same haptic feedback that the system offers for virtual keys.
2824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002825 }
2826
Adam Cohen61f560d2013-09-30 15:58:20 -07002827 public void performHapticFeedbackOnTouchDown(View v) {
2828 // Provide the same haptic feedback that the system offers for virtual keys.
2829 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2830 }
2831
2832 public View.OnTouchListener getHapticFeedbackTouchListener() {
2833 if (mHapticFeedbackTouchListener == null) {
2834 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2835 @Override
2836 public boolean onTouch(View v, MotionEvent event) {
2837 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2838 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2839 }
2840 return false;
2841 }
2842 };
2843 }
2844 return mHapticFeedbackTouchListener;
2845 }
2846
Adam Cohen9211d422014-10-07 18:14:53 -07002847 public void onDragStarted(View view) {
2848 if (isOnCustomContent()) {
2849 // Custom content screen doesn't participate in drag and drop. If on custom
2850 // content screen, move to default.
2851 moveWorkspaceToDefaultScreen();
2852 }
2853
2854 if (mLauncherCallbacks != null) {
2855 mLauncherCallbacks.onDragStarted(view);
2856 }
2857 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002858
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002859 /**
2860 * Called when the user stops interacting with the launcher.
2861 * This implies that the user is now on the homescreen and is not doing housekeeping.
2862 */
Adam Cohen9211d422014-10-07 18:14:53 -07002863 protected void onInteractionEnd() {
2864 if (mLauncherCallbacks != null) {
2865 mLauncherCallbacks.onInteractionEnd();
2866 }
2867 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868
2869 /**
2870 * Called when the user starts interacting with the launcher.
2871 * The possible interactions are:
2872 * - open all apps
2873 * - reorder an app shortcut, or a widget
2874 * - open the overview mode.
2875 * This is a good time to stop doing things that only make sense
2876 * when the user is on the homescreen and not doing housekeeping.
2877 */
Adam Cohen9211d422014-10-07 18:14:53 -07002878 protected void onInteractionBegin() {
2879 if (mLauncherCallbacks != null) {
2880 mLauncherCallbacks.onInteractionBegin();
2881 }
2882 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002883
Winson Chungcd99cd32015-04-29 11:03:24 -07002884 /** Updates the interaction state. */
2885 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002886 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002887 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002888 boolean fromStateWithOverlay;
2889 boolean toStateWithOverlay;
2890 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2891 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2892 toStateWithOverlay = toState != Workspace.State.NORMAL;
2893 } else {
2894 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2895 fromState != Workspace.State.NORMAL_HIDDEN;
2896 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2897 toState != Workspace.State.NORMAL_HIDDEN;
2898 }
2899 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002900 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002901 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002902 onInteractionEnd();
2903 }
2904 }
2905
Kenny Guyf07af7b2014-07-31 11:39:16 +01002906 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002907 try {
2908 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002909 launcherApps.showAppDetailsForProfile(componentName, user);
2910 } catch (SecurityException e) {
2911 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2912 Log.e(TAG, "Launcher does not have permission to launch settings");
2913 } catch (ActivityNotFoundException e) {
2914 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2915 Log.e(TAG, "Unable to launch settings");
2916 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002917 }
2918
Michael Jurka1e2f4652013-07-08 18:03:46 -07002919 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002920 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2921 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002922 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002923 // System applications cannot be installed. For now, show a toast explaining that.
2924 // We may give them the option of disabling apps this way.
2925 int messageId = R.string.uninstall_system_app_text;
2926 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002927 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002928 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002929 String packageName = componentName.getPackageName();
2930 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002931 Intent intent = new Intent(
2932 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002933 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2934 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002935 if (user != null) {
2936 user.addToIntent(intent, Intent.EXTRA_USER);
2937 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002938 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002939 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002940 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002941 }
2942
Michael Jurka86a720e2012-05-09 11:23:23 -07002943 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002944 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002946 // Only launch using the new animation if the shortcut has not opted out (this is a
2947 // private contract between launcher and may be ignored in the future).
2948 boolean useLaunchAnimation = (v != null) &&
2949 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002950 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2951 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002952
Kenny Guy1317e2d2014-05-08 18:52:50 +01002953 UserHandleCompat user = null;
2954 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2955 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2956 user = userManager.getUserForSerialNumber(serialNumber);
2957 }
2958
2959 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002960 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002961 ActivityOptions opts = null;
2962 if (sClipRevealMethod != null) {
2963 // TODO: call method directly when Launcher3 can depend on M APIs
2964 int left = 0, top = 0;
2965 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2966 if (v instanceof TextView) {
2967 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002968 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2969 if (icon != null) {
2970 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002971 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002972 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002973 width = bounds.width();
2974 height = bounds.height();
2975 }
2976 }
2977 try {
2978 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2979 left, top, width, height);
2980 } catch (IllegalAccessException e) {
2981 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2982 sClipRevealMethod = null;
2983 } catch (InvocationTargetException e) {
2984 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2985 sClipRevealMethod = null;
2986 }
2987 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002988 if (opts == null && !Utilities.isLmpOrAbove()) {
2989 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002990 v.getMeasuredWidth(), v.getMeasuredHeight());
2991 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002992 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002993 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002994
2995 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2996 // Could be launching some bookkeeping activity
2997 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002998 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002999 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003000 launcherApps.startActivityForProfile(intent.getComponent(), user,
3001 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003002 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003003 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 } catch (SecurityException e) {
3005 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003006 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003008 "or use the exported attribute for this activity. "
3009 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003011 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003013
Michael Jurka86a720e2012-05-09 11:23:23 -07003014 boolean startActivitySafely(View v, Intent intent, Object tag) {
3015 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003016 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3017 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3018 return false;
3019 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003020 try {
3021 success = startActivity(v, intent, tag);
3022 } catch (ActivityNotFoundException e) {
3023 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3024 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3025 }
3026 return success;
3027 }
3028
Adam Cohen268c4752012-06-06 17:47:33 -07003029 /**
3030 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3031 * in the DragLayer in the exact absolute location of the original FolderIcon.
3032 */
3033 private void copyFolderIconToImage(FolderIcon fi) {
3034 final int width = fi.getMeasuredWidth();
3035 final int height = fi.getMeasuredHeight();
3036
3037 // Lazy load ImageView, Bitmap and Canvas
3038 if (mFolderIconImageView == null) {
3039 mFolderIconImageView = new ImageView(this);
3040 }
3041 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3042 mFolderIconBitmap.getHeight() != height) {
3043 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3044 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3045 }
3046
3047 DragLayer.LayoutParams lp;
3048 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3049 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3050 } else {
3051 lp = new DragLayer.LayoutParams(width, height);
3052 }
3053
Adam Cohen307fe232012-08-16 17:55:58 -07003054 // The layout from which the folder is being opened may be scaled, adjust the starting
3055 // view size by this scale factor.
3056 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003057 lp.customPosition = true;
3058 lp.x = mRectForFolderAnimation.left;
3059 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003060 lp.width = (int) (scale * width);
3061 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003062
3063 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3064 fi.draw(mFolderIconCanvas);
3065 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003066 if (fi.getFolder() != null) {
3067 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3068 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003069 }
Adam Cohen268c4752012-06-06 17:47:33 -07003070 // Just in case this image view is still in the drag layer from a previous animation,
3071 // we remove it and re-add it.
3072 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3073 mDragLayer.removeView(mFolderIconImageView);
3074 }
3075 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003076 if (fi.getFolder() != null) {
3077 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003078 }
Adam Cohen268c4752012-06-06 17:47:33 -07003079 }
3080
Adam Cohen2801caf2011-05-13 20:57:39 -07003081 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003082 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003083 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3084 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3085 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3086
Adam Cohenc51934b2011-07-26 21:07:43 -07003087 FolderInfo info = (FolderInfo) fi.getTag();
3088 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3089 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003090 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3091 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003092 }
3093
Adam Cohen268c4752012-06-06 17:47:33 -07003094 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3095 copyFolderIconToImage(fi);
3096 fi.setVisibility(View.INVISIBLE);
3097
Michael Jurka2ecf9952012-06-18 12:52:28 -07003098 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003099 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003100 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003101 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3102 }
3103 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003104 oa.start();
3105 }
3106
Adam Cohen268c4752012-06-06 17:47:33 -07003107 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003108 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003109 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3110 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3111 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3112
Adam Cohen268c4752012-06-06 17:47:33 -07003113 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003114
Adam Cohen268c4752012-06-06 17:47:33 -07003115 // We remove and re-draw the FolderIcon in-case it has changed
3116 mDragLayer.removeView(mFolderIconImageView);
3117 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003118 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003119 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003120 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003121 oa.addListener(new AnimatorListenerAdapter() {
3122 @Override
3123 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003124 if (cl != null) {
3125 cl.clearFolderLeaveBehind();
3126 // Remove the ImageView copy of the FolderIcon and make the original visible.
3127 mDragLayer.removeView(mFolderIconImageView);
3128 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003129 }
3130 }
3131 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003132 oa.start();
3133 }
3134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003136 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 * is animated relative to the specified View. If the View is null, no animation
3138 * is played.
3139 *
3140 * @param folderInfo The FolderInfo describing the folder to open.
3141 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003142 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003143 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003144 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3145 if (openFolder != null && openFolder != folder) {
3146 // Close any open folder before opening a folder.
3147 closeFolder();
3148 }
3149
Adam Cohena9cf38f2011-05-02 15:36:58 -07003150 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151
Adam Cohena9cf38f2011-05-02 15:36:58 -07003152 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153
Sunny Goyalf4066152015-04-15 09:42:19 -07003154 // While the folder is open, the position of the icon cannot change.
3155 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3156
Adam Cohen4554ee12011-08-03 16:13:21 -07003157 // Just verify that the folder hasn't already been added to the DragLayer.
3158 // There was a one-off crash where the folder had a parent already.
3159 if (folder.getParent() == null) {
3160 mDragLayer.addView(folder);
3161 mDragController.addDropTarget((DropTarget) folder);
3162 } else {
3163 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3164 folder.getParent() + ").");
3165 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003166 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003167 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003168
3169 // Notify the accessibility manager that this folder "window" has appeared and occluded
3170 // the workspace items
3171 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3172 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003173 }
3174
3175 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003176 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003177 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003178 if (folder.isEditingName()) {
3179 folder.dismissEditingName();
3180 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003181 closeFolder(folder);
3182 }
3183 }
3184
3185 void closeFolder(Folder folder) {
3186 folder.getInfo().opened = false;
3187
3188 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3189 if (parent != null) {
3190 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3191 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003192 if (fi != null) {
3193 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3194 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003195 }
3196 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003197
3198 // Notify the accessibility manager that this folder "window" has disappeard and no
3199 // longer occludeds the workspace items
3200 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 }
3202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003204 if (!isDraggingEnabled()) return false;
3205 if (isWorkspaceLocked()) return false;
3206 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207
Adam Cohen1697b792013-09-17 19:08:21 -07003208 if (v instanceof Workspace) {
3209 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003210 if (!mWorkspace.isTouchActive()) {
3211 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3214 return true;
3215 } else {
3216 return false;
3217 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003218 } else {
3219 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003220 }
Adam Cohen1697b792013-09-17 19:08:21 -07003221 }
3222
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003223 CellLayout.CellInfo longClickCellInfo = null;
3224 View itemUnderLongClick = null;
3225 if (v.getTag() instanceof ItemInfo) {
3226 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003227 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003228 itemUnderLongClick = longClickCellInfo.cell;
3229 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 }
3231
Winson Chung3d503fb2011-07-13 17:25:49 -07003232 // The hotseat touch handling does not go through Workspace, and we always allow long press
3233 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003234 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003235 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003236 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003237 // User long pressed on empty space
3238 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3239 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003240 if (mWorkspace.isInOverviewMode()) {
3241 mWorkspace.startReordering(v);
3242 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003243 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003246 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3247 mHotseat.getOrderInHotseat(
3248 longClickCellInfo.cellX,
3249 longClickCellInfo.cellY));
3250 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003252 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 }
3254 }
3255 }
3256 return true;
3257 }
3258
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 boolean isHotseatLayout(View layout) {
3260 return mHotseat != null && layout != null &&
3261 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3262 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003263
Winson Chung3d503fb2011-07-13 17:25:49 -07003264 /**
3265 * Returns the CellLayout of the specified container at the specified screen.
3266 */
Sunny Goyal92820592015-03-02 11:31:35 -08003267 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003268 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3269 if (mHotseat != null) {
3270 return mHotseat.getLayout();
3271 } else {
3272 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003273 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003274 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003275 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003276 }
3277 }
3278
Winson Chungb745afb2015-03-02 11:51:23 -08003279 /**
3280 * For overridden classes.
3281 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003282 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003283 return isAppsViewVisible();
3284 }
3285
3286 public boolean isAppsViewVisible() {
3287 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3288 }
3289
3290 public boolean isWidgetsViewVisible() {
3291 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003292 }
3293
Craig Mautner360310b2012-10-26 15:13:08 -07003294 private void setWorkspaceBackground(boolean workspace) {
3295 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003296 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003297 }
3298
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003299 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003300 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3301 int curflags = getWindow().getAttributes().flags
3302 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3303 if (wpflags != curflags) {
3304 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3305 }
Craig Mautner360310b2012-10-26 15:13:08 -07003306 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003307 }
3308
Michael Jurkae326f182011-11-21 14:05:46 -08003309 @Override
3310 public void onTrimMemory(int level) {
3311 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003312 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3313 // The widget preview db can result in holding onto over
3314 // 3MB of memory for caching which isn't necessary.
3315 SQLiteDatabase.releaseMemory();
3316
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003317 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003318 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003319 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003320 if (mLauncherCallbacks != null) {
3321 mLauncherCallbacks.onTrimMemory(level);
3322 }
Michael Jurkae326f182011-11-21 14:05:46 -08003323 }
3324
Winson Chungb745afb2015-03-02 11:51:23 -08003325 @Override
3326 public void onStateTransitionHideSearchBar() {
3327 // Hide the search bar
3328 if (mSearchDropTargetBar != null) {
3329 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3330 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003331 }
3332
Winson Chungb745afb2015-03-02 11:51:23 -08003333 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003334 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3335 true);
Adam Cohened307df2013-10-02 09:37:31 -07003336 }
3337
Winson Chungdc61c4d2015-04-20 18:26:57 -07003338 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3339 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3340 onCompleteRunnable, true);
3341 }
3342
3343 protected void showWorkspace(int snapToPage, boolean animated) {
3344 showWorkspace(snapToPage, animated, null, true);
3345 }
3346
3347 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3348 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003349 boolean changed = mState != State.WORKSPACE ||
3350 mWorkspace.getState() != Workspace.State.NORMAL;
3351 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003352 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003354 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003355 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356
Winson Chungc7d2b602012-05-16 17:02:20 -07003357 // Show the search bar (only animate if we were showing the drop target bar in spring
3358 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003359 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003360 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003361 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003362
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363 // Set focus to the AppsCustomize button
3364 if (mAllAppsButton != null) {
3365 mAllAppsButton.requestFocus();
3366 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003367 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003368
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003369 // Change the state *after* we've called all the transition code
3370 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003371
Winson Chung5fb63472011-02-02 17:03:37 -08003372 // Resume the auto-advance of widgets
3373 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003374 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003375
Winson Chung0f785722015-04-08 10:27:49 -07003376 if (changed) {
3377 // Send an accessibility event to announce the context change
3378 getWindow().getDecorView()
3379 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003380 if (notifyLauncherCallbacks) {
3381 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003382 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003383 mLauncherCallbacks.onAllAppsHidden();
3384 }
3385 }
Winson Chung0f785722015-04-08 10:27:49 -07003386 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003387 }
3388
Adam Cohened307df2013-10-02 09:37:31 -07003389 void showOverviewMode(boolean animated) {
3390 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003391 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003392 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3393 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003394 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003395 }
3396
Winson Chungb745afb2015-03-02 11:51:23 -08003397 /**
3398 * Shows the apps view.
3399 */
3400 void showAppsView(boolean animated, boolean resetListToTop) {
3401 if (resetListToTop) {
3402 mAppsView.scrollToTop();
3403 }
3404 showAppsOrWidgets(animated, State.APPS);
3405 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406
Winson Chungb745afb2015-03-02 11:51:23 -08003407 /**
3408 * Shows the widgets view.
3409 */
3410 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003411 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003412 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003413 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003414 }
Winson Chungb745afb2015-03-02 11:51:23 -08003415 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416
3417 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003418 @Override
3419 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003421 }
3422 });
Winson Chungb745afb2015-03-02 11:51:23 -08003423 }
3424
3425 /**
3426 * Sets up the transition to show the apps/widgets view.
3427 */
3428 private void showAppsOrWidgets(boolean animated, State toState) {
3429 if (mState != State.WORKSPACE) return;
3430 if (toState != State.APPS && toState != State.WIDGETS) return;
3431
3432 if (toState == State.APPS) {
3433 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003434 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003435 mLauncherCallbacks.onAllAppsShown();
3436 }
Winson Chungb745afb2015-03-02 11:51:23 -08003437 } else {
3438 mStateTransitionAnimation.startAnimationToWidgets(animated);
3439 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440
Michael Jurkab3e22d92011-10-31 15:58:33 -07003441 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003442 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003443
3444 // Pause the auto-advance of widgets until we are out of AllApps
3445 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003446 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447 closeFolder();
3448
3449 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003450 getWindow().getDecorView()
3451 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452 }
3453
Winson Chungcd99cd32015-04-29 11:03:24 -07003454 /**
3455 * Updates the workspace and interaction state on state change, and return the animation to this
3456 * new state.
3457 */
3458 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3459 boolean animated, HashMap<View, Integer> layerViews) {
3460 Workspace.State fromState = mWorkspace.getState();
3461 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3462 updateInteraction(fromState, toState);
3463 return anim;
3464 }
3465
Hyunyoung Song3f471442015-04-08 19:01:34 -07003466 public void enterSpringLoadedDragMode() {
3467 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3468 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003469 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3470 mState == State.WIDGETS_SPRING_LOADED) {
3471 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003472 }
Winson Chungb745afb2015-03-02 11:51:23 -08003473
3474 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003475 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3476 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003477 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003478 }
Adam Cohen7777d962011-08-18 18:58:38 -07003479
Hyunyoung Song3f471442015-04-08 19:01:34 -07003480 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003481 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003482 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003483
Winson Chungcd99cd32015-04-29 11:03:24 -07003484 if (successfulDrop) {
3485 // We need to trigger all apps hidden to notify search to update itself before the
3486 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003487 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003488 mLauncherCallbacks.onAllAppsHidden();
3489 }
3490 }
3491
Winson Chunge7a03942011-08-05 15:05:12 -07003492 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003493 @Override
3494 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003495 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003496 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3497 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003498 // Before we show workspace, hide all apps again because
3499 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3500 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003501 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003502 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003503 } else {
3504 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003505 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003506 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003507 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003508 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003509
Michael Jurkad3ef3062010-11-23 16:23:58 -08003510 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003511 if (mState == State.APPS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003512 showAppsView(true, false);
Winson Chungb745afb2015-03-02 11:51:23 -08003513 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003514 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003515 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003516 }
3517
Michael Jurkab3e22d92011-10-31 15:58:33 -07003518 void lockAllApps() {
3519 // TODO
3520 }
3521
3522 void unlockAllApps() {
3523 // TODO
3524 }
3525
Sunny Goyal594d76d2014-11-06 10:12:54 -08003526 protected void disableVoiceButtonProxy(boolean disable) {
3527 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003528 }
3529
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003530 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003531 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3532 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003533 }
3534
Adam Cohen24ce0b32014-01-14 16:18:14 -08003535 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003536 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3537 if (searchProvider == null) {
3538 return null;
3539 }
3540
3541 Bundle opts = new Bundle();
3542 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003543 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003544
3545 SharedPreferences sp = getSharedPreferences(
3546 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3547 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003548 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003549 if (!searchProvider.provider.flattenToString().equals(
3550 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003551 || (widgetInfo == null)
3552 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003553 // A valid widget is not already bound.
3554 if (widgetId > -1) {
3555 mAppWidgetHost.deleteAppWidgetId(widgetId);
3556 widgetId = -1;
3557 }
3558
3559 // Try to bind a new widget
3560 widgetId = mAppWidgetHost.allocateAppWidgetId();
3561
3562 if (!AppWidgetManagerCompat.getInstance(this)
3563 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3564 mAppWidgetHost.deleteAppWidgetId(widgetId);
3565 widgetId = -1;
3566 }
3567
3568 sp.edit()
3569 .putInt(QSB_WIDGET_ID, widgetId)
3570 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3571 .commit();
3572 }
3573
3574 if (widgetId != -1) {
3575 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3576 mQsb.updateAppWidgetOptions(opts);
3577 mQsb.setPadding(0, 0, 0, 0);
3578 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003579 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003580 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003581 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003582 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003583 }
3584
Sunny Goyal22235bc2015-04-03 09:23:43 -07003585 private void reinflateQSBIfNecessary() {
3586 if (mQsb instanceof LauncherAppWidgetHostView &&
3587 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3588 mSearchDropTargetBar.removeView(mQsb);
3589 mQsb = null;
3590 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3591 }
3592 }
3593
Michael Jurkad7c28052012-04-27 15:43:36 -07003594 @Override
3595 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003596 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003597 final List<CharSequence> text = event.getText();
3598 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003599 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003600 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003601 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003602 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003603 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003604 } else {
3605 text.add(getString(R.string.all_apps_home_button_label));
3606 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003607 return result;
3608 }
3609
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003610 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003611 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003612 */
Adam Cohen091440a2015-03-18 14:16:05 -07003613 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003614 @Override
3615 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003616 closeSystemDialogs();
3617 }
3618 }
3619
3620 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003621 * Receives notifications whenever the appwidgets are reset.
3622 */
3623 private class AppWidgetResetObserver extends ContentObserver {
3624 public AppWidgetResetObserver() {
3625 super(new Handler());
3626 }
3627
3628 @Override
3629 public void onChange(boolean selfChange) {
3630 onAppWidgetReset();
3631 }
3632 }
3633
3634 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003635 * If the activity is currently paused, signal that we need to run the passed Runnable
3636 * in onResume.
3637 *
3638 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003639 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3640 * wrong when we're not running, and if the activity comes back to what the configuration was
3641 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 *
3643 * Implementation of the method from LauncherModel.Callbacks.
3644 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003645 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003646 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003647 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 if (mPaused) {
3649 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003650 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003651 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003652 }
3653 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003654 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 return true;
3656 } else {
3657 return false;
3658 }
3659 }
3660
Michael Jurkac402cd92013-05-20 15:49:32 +02003661 private boolean waitUntilResume(Runnable run) {
3662 return waitUntilResume(run, false);
3663 }
3664
Michael Jurka1e2f4652013-07-08 18:03:46 -07003665 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003666 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003667 }
3668
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003670 * If the activity is currently paused, signal that we need to re-run the loader
3671 * in onResume.
3672 *
3673 * This needs to be called from incoming places where resources might have been loaded
3674 * while we are paused. That is becaues the Configuration might be wrong
3675 * when we're not running, and if it comes back to what it was when we
3676 * were paused, we are not restarted.
3677 *
3678 * Implementation of the method from LauncherModel.Callbacks.
3679 *
3680 * @return true if we are currently paused. The caller might be able to
3681 * skip some work in that case since we will come back again.
3682 */
3683 public boolean setLoadOnResume() {
3684 if (mPaused) {
3685 Log.i(TAG, "setLoadOnResume");
3686 mOnResumeNeedsLoad = true;
3687 return true;
3688 } else {
3689 return false;
3690 }
3691 }
3692
3693 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003695 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003697 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003698 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003699 } else {
3700 return SCREEN_COUNT / 2;
3701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003703
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 /**
3705 * Refreshes the shortcuts shown on the workspace.
3706 *
3707 * Implementation of the method from LauncherModel.Callbacks.
3708 */
3709 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003710 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003711
Michael Jurka7607c2f2013-04-03 14:33:19 -07003712 // If we're starting binding all over again, clear any bind calls we'd postponed in
3713 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3714 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003715 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003716
Winson Chungd64d1762013-08-20 14:37:16 -07003717 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003718 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003719 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003720
Michael Jurka05bf644e2011-11-30 20:28:53 -08003721 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003722 if (mHotseat != null) {
3723 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003724 }
3725 }
3726
Adam Cohendcd297f2013-06-18 13:13:40 -07003727 @Override
3728 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003729 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003730
Adam Cohen5084cba2013-09-03 12:01:16 -07003731 // If there are no screens, we need to have an empty screen
3732 if (orderedScreenIds.size() == 0) {
3733 mWorkspace.addExtraEmptyScreen();
3734 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003735
3736 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003737 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003738 if (hasCustomContentToLeft()) {
3739 mWorkspace.createCustomContentContainer();
3740 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003741 }
Winson Chung64359a52013-07-08 17:17:08 -07003742 }
3743
3744 @Override
3745 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003746 // Log to disk
3747 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3748 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3749 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003750 int count = orderedScreenIds.size();
3751 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003752 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003753 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003754 }
3755
Adam Cohen39a06042013-07-19 14:30:12 -07003756 private boolean shouldShowWeightWatcher() {
3757 String spKey = LauncherAppState.getSharedPreferencesKey();
3758 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003759 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003760
3761 return show;
3762 }
3763
3764 private void toggleShowWeightWatcher() {
3765 String spKey = LauncherAppState.getSharedPreferencesKey();
3766 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3767 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3768
3769 show = !show;
3770
3771 SharedPreferences.Editor editor = sp.edit();
3772 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3773 editor.commit();
3774
3775 if (mWeightWatcher != null) {
3776 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3777 }
3778 }
3779
Winson Chungd64d1762013-08-20 14:37:16 -07003780 public void bindAppsAdded(final ArrayList<Long> newScreens,
3781 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003782 final ArrayList<ItemInfo> addAnimated,
3783 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 Runnable r = new Runnable() {
3785 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003786 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003787 }
3788 };
3789 if (waitUntilResume(r)) {
3790 return;
3791 }
3792
Winson Chungd64d1762013-08-20 14:37:16 -07003793 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003794 if (newScreens != null) {
3795 bindAddScreens(newScreens);
3796 }
Winson Chungd64d1762013-08-20 14:37:16 -07003797
3798 // We add the items without animation on non-visible pages, and with
3799 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003800 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003801 bindItems(addNotAnimated, 0,
3802 addNotAnimated.size(), false);
3803 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003804 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003805 bindItems(addAnimated, 0,
3806 addAnimated.size(), true);
3807 }
Winson Chungc58497e2013-09-03 17:48:37 -07003808
Winson Chung87412982013-10-03 18:34:14 -07003809 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003810 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003811
Winson Chungb745afb2015-03-02 11:51:23 -08003812 if (addedApps != null && mAppsView != null) {
3813 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003814 }
Winson Chungd64d1762013-08-20 14:37:16 -07003815 }
3816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003817 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 * Bind the items start-end from the list.
3819 *
3820 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 */
Winson Chung64359a52013-07-08 17:17:08 -07003822 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3823 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003824 Runnable r = new Runnable() {
3825 public void run() {
3826 bindItems(shortcuts, start, end, forceAnimateIcons);
3827 }
3828 };
3829 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003830 return;
3831 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003832
Winson Chungf0c6ae02012-03-21 16:10:31 -07003833 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003834 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3835 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003836 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003837 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003838 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003839 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003841
3842 // Short circuit if we are loading dock items for a configuration which has no dock
3843 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3844 mHotseat == null) {
3845 continue;
3846 }
3847
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 switch (item.itemType) {
3849 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3850 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003851 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003852 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003853
Winson Chung64359a52013-07-08 17:17:08 -07003854 /*
3855 * TODO: FIX collision case
3856 */
Winson Chungce376632013-07-11 16:12:41 -07003857 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3858 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3859 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003860 View v = cl.getChildAt(item.cellX, item.cellY);
3861 Object tag = v.getTag();
3862 String desc = "Collision while binding workspace item: " + item
3863 + ". Collides with " + tag;
3864 if (LauncherAppState.isDogfoodBuild()) {
3865 throw (new RuntimeException(desc));
3866 } else {
3867 Log.d(TAG, desc);
3868 }
Winson Chungce376632013-07-11 16:12:41 -07003869 }
Winson Chung64359a52013-07-08 17:17:08 -07003870 }
3871
Adam Cohendcd297f2013-06-18 13:13:40 -07003872 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3873 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003874 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003875 // Animate all the applications up now
3876 shortcut.setAlpha(0f);
3877 shortcut.setScaleX(0f);
3878 shortcut.setScaleY(0f);
3879 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003880 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003882 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003883 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003884 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003885 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003886 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003887 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3888 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003890 default:
3891 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003893 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003894
Winson Chung997a9232013-07-24 15:33:46 -07003895 if (animateIcons) {
3896 // Animate to the correct page
3897 if (newShortcutsScreenId > -1) {
3898 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003899 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003900 final Runnable startBounceAnimRunnable = new Runnable() {
3901 public void run() {
3902 anim.playTogether(bounceAnims);
3903 anim.start();
3904 }
3905 };
Winson Chung997a9232013-07-24 15:33:46 -07003906 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003907 // We post the animation slightly delayed to prevent slowdowns
3908 // when we are loading right after we return to launcher.
3909 mWorkspace.postDelayed(new Runnable() {
3910 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003911 if (mWorkspace != null) {
3912 mWorkspace.snapToPage(newScreenIndex);
3913 mWorkspace.postDelayed(startBounceAnimRunnable,
3914 NEW_APPS_ANIMATION_DELAY);
3915 }
Winson Chung94d67682013-09-25 16:29:40 -07003916 }
3917 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003918 } else {
3919 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003920 }
3921 }
Winson Chung64359a52013-07-08 17:17:08 -07003922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003924 }
3925
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926 /**
3927 * Implementation of the method from LauncherModel.Callbacks.
3928 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003929 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003930 Runnable r = new Runnable() {
3931 public void run() {
3932 bindFolders(folders);
3933 }
3934 };
3935 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003936 return;
3937 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003938 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003940
3941 /**
3942 * Add the views for a widget to the workspace.
3943 *
3944 * Implementation of the method from LauncherModel.Callbacks.
3945 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003946 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003947 Runnable r = new Runnable() {
3948 public void run() {
3949 bindAppWidget(item);
3950 }
3951 };
3952 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003953 return;
3954 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003955
Daniel Sandler843e8602010-06-07 14:59:01 -04003956 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "bindAppWidget: " + item);
3959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 final Workspace workspace = mWorkspace;
3961
Adam Cohen59400422014-03-05 18:07:04 -08003962 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003963 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003964
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003965 if (!mIsSafeModeEnabled
3966 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3967 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003968 if (appWidgetInfo == null) {
3969 if (DEBUG_WIDGETS) {
3970 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3971 + " belongs to component " + item.providerName
3972 + ", as the povider is null");
3973 }
3974 LauncherModel.deleteItemFromDatabase(this, item);
3975 return;
3976 }
3977 // Note: This assumes that the id remap broadcast is received before this step.
3978 // If that is not the case, the id remap will be ignored and user may see the
3979 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003980 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003981 pendingInfo.spanX = item.spanX;
3982 pendingInfo.spanY = item.spanY;
3983 pendingInfo.minSpanX = item.minSpanX;
3984 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003985 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003986 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003987
Sunny Goyalff572272014-07-23 13:58:07 -07003988 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003989 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3990 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003991
3992 // TODO consider showing a permission dialog when the widget is clicked.
3993 if (!success) {
3994 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3995 if (DEBUG_WIDGETS) {
3996 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3997 + " belongs to component " + item.providerName
3998 + ", as the launcher is unable to bing a new widget id");
3999 }
4000 LauncherModel.deleteItemFromDatabase(this, item);
4001 return;
4002 }
4003
4004 item.appWidgetId = newWidgetId;
4005
4006 // If the widget has a configure activity, it is still needs to set it up, otherwise
4007 // the widget is ready to go.
4008 item.restoreStatus = (appWidgetInfo.configure == null)
4009 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4010 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4011
4012 LauncherModel.updateItemInDatabase(this, item);
4013 }
4014
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004015 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004016 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004017 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004018 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4019 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004020 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004021
Sunny Goyal651077b2014-06-30 14:15:31 -07004022 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4023 } else {
4024 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004025 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4026 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004027 view.updateIcon(mIconCache);
4028 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004029 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004030 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032
Joe Onorato9c1289c2009-08-17 11:03:03 -04004033 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004034 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035
Adam Cohendcd297f2013-06-18 13:13:40 -07004036 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004038 if (!item.isCustomWidget()) {
4039 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4040 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004041
Joe Onorato9c1289c2009-08-17 11:03:03 -04004042 workspace.requestLayout();
4043
Daniel Sandler843e8602010-06-07 14:59:01 -04004044 if (DEBUG_WIDGETS) {
4045 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4046 + (SystemClock.uptimeMillis()-start) + "ms");
4047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 }
4049
Sunny Goyalff572272014-07-23 13:58:07 -07004050 /**
4051 * Restores a pending widget.
4052 *
4053 * @param appWidgetId The app widget id
4054 * @param cellInfo The position on screen where to create the widget.
4055 */
4056 private void completeRestoreAppWidget(final int appWidgetId) {
4057 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4058 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4059 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4060 return;
4061 }
4062
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004063 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004064 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4065
4066 mWorkspace.reinflateWidgetsIfNecessary();
4067 LauncherModel.updateItemInDatabase(this, info);
4068 }
4069
Adam Cohen1462de32012-07-24 22:34:36 -07004070 public void onPageBoundSynchronously(int page) {
4071 mSynchronouslyBoundPages.add(page);
4072 }
4073
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 /**
4075 * Callback saying that there aren't any more items to bind.
4076 *
4077 * Implementation of the method from LauncherModel.Callbacks.
4078 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004079 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004080 Runnable r = new Runnable() {
4081 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004082 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004083 }
4084 };
4085 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004086 return;
4087 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 if (mSavedState != null) {
4089 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004090 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004092 mSavedState = null;
4093 }
4094
Adam Cohen1462de32012-07-24 22:34:36 -07004095 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004097 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004098 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004099
4100 // If we received the result of any pending adds while the loader was running (e.g. the
4101 // widget configuration forced an orientation change), process them now.
4102 if (sPendingAddItem != null) {
4103 final long screenId = completeAdd(sPendingAddItem);
4104
4105 // TODO: this moves the user to the page where the pending item was added. Ideally,
4106 // the screen would be guaranteed to exist after bind, and the page would be set through
4107 // the workspace restore process.
4108 mWorkspace.post(new Runnable() {
4109 @Override
4110 public void run() {
4111 mWorkspace.snapToScreenId(screenId);
4112 }
4113 });
4114 sPendingAddItem = null;
4115 }
4116
Sunny Goyal756adbc2015-04-16 15:20:51 -07004117 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004118
4119 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004120 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004121 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004122 }
4123
Adam Cohen3ed316a2014-07-23 18:21:20 -07004124 private void sendLoadingCompleteBroadcastIfNecessary() {
4125 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4126 String permission =
4127 getResources().getString(R.string.receive_first_load_broadcast_permission);
4128 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4129 sendBroadcast(intent, permission);
4130 SharedPreferences.Editor editor = mSharedPrefs.edit();
4131 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4132 editor.apply();
4133 }
4134 }
4135
Winson Chunga0b7e862013-09-05 16:03:15 -07004136 public boolean isAllAppsButtonRank(int rank) {
4137 if (mHotseat != null) {
4138 return mHotseat.isAllAppsButtonRank(rank);
4139 }
4140 return false;
4141 }
4142
Winson Chunga2413752012-04-03 14:22:34 -07004143 private boolean canRunNewAppsAnimation() {
4144 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4145 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4146 }
4147
Winson Chungc9168342013-06-26 14:54:55 -07004148 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4149 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4150 PropertyValuesHolder.ofFloat("alpha", 1f),
4151 PropertyValuesHolder.ofFloat("scaleX", 1f),
4152 PropertyValuesHolder.ofFloat("scaleY", 1f));
4153 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4154 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4155 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4156 return bounceAnim;
4157 }
4158
Adam Cohen27772732013-11-11 14:00:56 +00004159 public boolean useVerticalBarLayout() {
4160 return LauncherAppState.getInstance().getDynamicGrid().
4161 getDeviceProfile().isVerticalBarLayout();
4162 }
4163
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004164 protected Rect getSearchBarBounds() {
4165 return LauncherAppState.getInstance().getDynamicGrid().
4166 getDeviceProfile().getSearchBarBounds();
4167 }
4168
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004169 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004170 if (mSearchDropTargetBar == null) {
4171 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004172 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004173 if (mQsb != null) {
4174 mSearchDropTargetBar.removeView(mQsb);
4175 mQsb = null;
4176 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004177 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004178 }
4179
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004180 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004181 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4182 * multiple calls to bind the same list.)
4183 */
4184 @Thunk ArrayList<AppInfo> mTmpAppsList;
4185 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4186 public void run() {
4187 bindAllApplications(mTmpAppsList);
4188 mTmpAppsList = null;
4189 }
4190 };
4191
4192 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004193 * Add the icons for all apps.
4194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004197 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004198 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4199 mTmpAppsList = apps;
4200 return;
4201 }
4202
Winson Chungb745afb2015-03-02 11:51:23 -08004203 if (mAppsView != null) {
4204 mAppsView.setApps(apps);
4205 }
Adam Cohen9211d422014-10-07 18:14:53 -07004206 if (mLauncherCallbacks != null) {
4207 mLauncherCallbacks.bindAllApplications(apps);
4208 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 }
4210
4211 /**
4212 * A package was updated.
4213 *
4214 * Implementation of the method from LauncherModel.Callbacks.
4215 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004216 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004217 Runnable r = new Runnable() {
4218 public void run() {
4219 bindAppsUpdated(apps);
4220 }
4221 };
4222 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004223 return;
4224 }
4225
Winson Chungb745afb2015-03-02 11:51:23 -08004226 if (mAppsView != null) {
4227 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004229 }
4230
Sunny Goyal4390ace2014-10-13 11:33:11 -07004231 @Override
4232 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4233 Runnable r = new Runnable() {
4234 public void run() {
4235 bindWidgetsRestored(widgets);
4236 }
4237 };
4238 if (waitUntilResume(r)) {
4239 return;
4240 }
4241 mWorkspace.widgetsRestored(widgets);
4242 }
4243
Joe Onorato9c1289c2009-08-17 11:03:03 -04004244 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004245 * Some shortcuts were updated in the background.
4246 *
4247 * Implementation of the method from LauncherModel.Callbacks.
4248 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004249 @Override
4250 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4251 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004252 Runnable r = new Runnable() {
4253 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004254 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004255 }
4256 };
4257 if (waitUntilResume(r)) {
4258 return;
4259 }
4260
Sunny Goyal4390ace2014-10-13 11:33:11 -07004261 if (!updated.isEmpty()) {
4262 mWorkspace.updateShortcuts(updated);
4263 }
4264
4265 if (!removed.isEmpty()) {
4266 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4267 for (ShortcutInfo si : removed) {
4268 removedComponents.add(si.getTargetComponent());
4269 }
4270 mWorkspace.removeItemsByComponentName(removedComponents, user);
4271 // Notify the drag controller
4272 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004273 }
4274 }
4275
4276 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004277 * Update the state of a package, typically related to install state.
4278 *
4279 * Implementation of the method from LauncherModel.Callbacks.
4280 */
Sunny Goyale755d462014-07-22 13:48:29 -07004281 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004282 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4283 Runnable r = new Runnable() {
4284 public void run() {
4285 bindRestoreItemsChange(updates);
4286 }
4287 };
4288 if (waitUntilResume(r)) {
4289 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004290 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004291
Sunny Goyal756adbc2015-04-16 15:20:51 -07004292 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004293 }
4294
4295 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004296 * A package was uninstalled. We take both the super set of packageNames
4297 * in addition to specific applications to remove, the reason being that
4298 * this can be called when a package is updated as well. In that scenario,
4299 * we only remove specific components from the workspace, where as
4300 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004302 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004303 * Implementation of the method from LauncherModel.Callbacks.
4304 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004305 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004306 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004307 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004308 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004309 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004310 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004311 }
Winson Chungd64d1762013-08-20 14:37:16 -07004312 };
4313 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004314 return;
4315 }
4316
Sunny Goyal1a745e82014-10-02 15:58:31 -07004317 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004318 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4319 for (AppInfo info : appInfos) {
4320 removedComponents.add(info.componentName);
4321 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004322 if (!packageNames.isEmpty()) {
4323 mWorkspace.removeItemsByPackageName(packageNames, user);
4324 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004325 if (!removedComponents.isEmpty()) {
4326 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004327 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004328 // Notify the drag controller
4329 mDragController.onAppsRemoved(packageNames, removedComponents);
4330
Sunny Goyal1a745e82014-10-02 15:58:31 -07004331 } else {
4332 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004333 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004334
Winson Chungdf95eb12013-10-16 14:57:07 -07004335 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004336 if (mAppsView != null) {
4337 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339 }
Joe Onoratobe386092009-11-17 17:32:16 -08004340
Adam Cohen091440a2015-03-18 14:16:05 -07004341 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004342 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004343 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004344 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004345 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004346 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004347
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004348 @Override
4349 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004350 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4351 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004352 return;
4353 }
4354
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004355 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4356 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4357 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004358 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004359 }
4360
Winson Chung400438b2011-01-16 17:53:48 -08004361 private int mapConfigurationOriActivityInfoOri(int configOri) {
4362 final Display d = getWindowManager().getDefaultDisplay();
4363 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4364 switch (d.getRotation()) {
4365 case Surface.ROTATION_0:
4366 case Surface.ROTATION_180:
4367 // We are currently in the same basic orientation as the natural orientation
4368 naturalOri = configOri;
4369 break;
4370 case Surface.ROTATION_90:
4371 case Surface.ROTATION_270:
4372 // We are currently in the other basic orientation to the natural orientation
4373 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4374 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4375 break;
4376 }
4377
4378 int[] oriMap = {
4379 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4380 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4381 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4382 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4383 };
4384 // Since the map starts at portrait, we need to offset if this device's natural orientation
4385 // is landscape.
4386 int indexOffset = 0;
4387 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4388 indexOffset = 1;
4389 }
4390 return oriMap[(d.getRotation() + indexOffset) % 4];
4391 }
Adam Cohen446e9402011-09-15 18:21:21 -07004392
Winson Chung4b919f82012-05-01 10:44:08 -07004393 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004394 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004395 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4396 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4397 .getConfiguration().orientation));
4398 } else {
4399 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4400 }
Winson Chung4b919f82012-05-01 10:44:08 -07004401 }
4402 }
4403 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004404 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004405 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004406 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004407 } else {
4408 mHandler.postDelayed(new Runnable() {
4409 public void run() {
4410 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4411 }
4412 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004413 }
Winson Chung4b919f82012-05-01 10:44:08 -07004414 }
Winson Chung400438b2011-01-16 17:53:48 -08004415 }
4416
Winson Chunge43a1e72014-01-15 10:33:02 -08004417 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004418 if (mLauncherCallbacks != null) {
4419 return mLauncherCallbacks.isLauncherPreinstalled();
4420 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004421 PackageManager pm = getPackageManager();
4422 try {
4423 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4424 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4425 return true;
4426 } else {
4427 return false;
4428 }
4429 } catch (NameNotFoundException e) {
4430 e.printStackTrace();
4431 return false;
4432 }
4433 }
4434
Adam Cohenea90f832014-05-21 15:03:34 -07004435 /**
4436 * This method indicates whether or not we should suggest default wallpaper dimensions
4437 * when our wallpaper cropper was not yet used to set a wallpaper.
4438 */
4439 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004440 if (mLauncherCallbacks != null) {
4441 return mLauncherCallbacks.overrideWallpaperDimensions();
4442 }
Adam Cohenea90f832014-05-21 15:03:34 -07004443 return true;
4444 }
4445
Adam Cohen432609a2014-03-13 17:03:22 -07004446 /**
4447 * To be overridden by subclasses to indicate that there is an activity to launch
4448 * before showing the standard launcher experience.
4449 */
4450 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004451 if (mLauncherCallbacks != null) {
4452 return mLauncherCallbacks.hasFirstRunActivity();
4453 }
Adam Cohen432609a2014-03-13 17:03:22 -07004454 return false;
4455 }
4456
4457 /**
4458 * To be overridden by subclasses to launch any first run activity
4459 */
4460 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004461 if (mLauncherCallbacks != null) {
4462 return mLauncherCallbacks.getFirstRunActivity();
4463 }
Adam Cohen432609a2014-03-13 17:03:22 -07004464 return null;
4465 }
4466
Winson Chung2826a402015-04-17 16:18:53 -07004467 /**
4468 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004469 */
4470 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004471 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4472 return false;
4473 }
4474
Winson Chung2826a402015-04-17 16:18:53 -07004475 if (mLauncherCallbacks != null) {
4476 return mLauncherCallbacks.overrideAllAppsSearch();
4477 }
4478 return false;
4479 }
4480
Winson Chunga6945242014-01-08 14:04:34 -08004481 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004482 return !ActivityManager.isRunningInTestHarness() &&
4483 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004484 }
4485
Adam Cohen4a9423d2014-03-28 14:22:31 -07004486 protected boolean hasRunFirstRunActivity() {
4487 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4488 }
4489
Adam Cohen432609a2014-03-13 17:03:22 -07004490 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004491 if (shouldRunFirstRunActivity() &&
4492 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004493 Intent firstRunIntent = getFirstRunActivity();
4494 if (firstRunIntent != null) {
4495 startActivity(firstRunIntent);
4496 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004497 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004498 }
4499 }
Adam Cohen432609a2014-03-13 17:03:22 -07004500 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004501 }
4502
4503 private void markFirstRunActivityShown() {
4504 SharedPreferences.Editor editor = mSharedPrefs.edit();
4505 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4506 editor.apply();
4507 }
4508
Adam Cohen432609a2014-03-13 17:03:22 -07004509 /**
4510 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4511 * screen that must be displayed and dismissed.
4512 */
4513 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004514 if (mLauncherCallbacks != null) {
4515 return mLauncherCallbacks.hasDismissableIntroScreen();
4516 }
Adam Cohen432609a2014-03-13 17:03:22 -07004517 return false;
4518 }
4519
4520 /**
4521 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4522 */
4523 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004524 if (mLauncherCallbacks != null) {
4525 return mLauncherCallbacks.getIntroScreen();
4526 }
Adam Cohen432609a2014-03-13 17:03:22 -07004527 return null;
4528 }
4529
4530 /**
4531 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4532 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4533 */
4534 private boolean shouldShowIntroScreen() {
4535 return hasDismissableIntroScreen() &&
4536 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4537 }
4538
4539 protected void showIntroScreen() {
4540 View introScreen = getIntroScreen();
4541 changeWallpaperVisiblity(false);
4542 if (introScreen != null) {
4543 mDragLayer.showOverlayView(introScreen);
4544 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004545 if (mLauncherOverlayContainer != null) {
4546 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 }
4549
4550 public void dismissIntroScreen() {
4551 markIntroScreenDismissed();
4552 if (showFirstRunActivity()) {
4553 // We delay hiding the intro view until the first run activity is showing. This
4554 // avoids a blip.
4555 mWorkspace.postDelayed(new Runnable() {
4556 @Override
4557 public void run() {
4558 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004559 if (mLauncherOverlayContainer != null) {
4560 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4561 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004562 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004563 }
4564 }, ACTIVITY_START_DELAY);
4565 } else {
4566 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004567 if (mLauncherOverlayContainer != null) {
4568 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4569 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004570 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004571 }
4572 changeWallpaperVisiblity(true);
4573 }
4574
4575 private void markIntroScreenDismissed() {
4576 SharedPreferences.Editor editor = mSharedPrefs.edit();
4577 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4578 editor.apply();
4579 }
4580
Adam Cohen091440a2015-03-18 14:16:05 -07004581 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004582 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4583 // on the device, then we always show the first run cling experience (or if there is no
4584 // launcher2). Otherwise, we prompt the user upon started for migration
4585 LauncherClings launcherClings = new LauncherClings(this);
4586 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4587 if (mModel.canMigrateFromOldLauncherDb(this)) {
4588 launcherClings.showMigrationCling();
4589 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004590 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004591 }
4592 }
4593 }
4594
Winson Chunga6945242014-01-08 14:04:34 -08004595 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004596 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4597 if (mHotseat != null) mHotseat.setAlpha(1f);
4598 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4599 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004600 }
4601
4602 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004603 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4604 if (mHotseat != null) mHotseat.setAlpha(0f);
4605 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4606 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004607 }
4608
Mathew Inwood72fbec12013-11-19 15:45:07 +00004609 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004610 // Called from search suggestion, not supported in other profiles.
4611 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4612 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4613 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4614 myUser);
4615 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004616 return null;
4617 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004618 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004619 }
4620
4621 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4622 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004623 // Called from search suggestion, not supported in other profiles.
4624 return createShortcutDragInfo(shortcutIntent, caption, icon,
4625 UserHandleCompat.myUserHandle());
4626 }
4627
4628 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4629 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004630 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004631 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004632 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004633 }
4634
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004635 protected void moveWorkspaceToDefaultScreen() {
4636 mWorkspace.moveToDefaultScreen(false);
4637 }
4638
Mathew Inwood72fbec12013-11-19 15:45:07 +00004639 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4640 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004641 mWorkspace.onExternalDragStartedWithItem(dragView);
4642 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004643 }
4644
Anjali Koppalf5d29132014-02-28 13:33:27 -08004645 @Override
4646 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004647 if (mLauncherCallbacks != null) {
4648 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4649 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004650 }
4651
Winson Chung80baf5a2010-08-09 16:03:15 -07004652 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004653 * Prints out out state for debugging.
4654 */
4655 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004656 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004657 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004658 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4659 Log.d(TAG, "mRestoring=" + mRestoring);
4660 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4661 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004662 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004663 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004664 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004665
Daniel Sandler325dc232013-06-05 22:57:57 -04004666 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004667 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004668
4669 @Override
4670 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4671 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004672 synchronized (sDumpLogs) {
4673 writer.println(" ");
4674 writer.println("Debug logs: ");
4675 for (int i = 0; i < sDumpLogs.size(); i++) {
4676 writer.println(" " + sDumpLogs.get(i));
4677 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004678 }
Adam Cohen9211d422014-10-07 18:14:53 -07004679 if (mLauncherCallbacks != null) {
4680 mLauncherCallbacks.dump(prefix, fd, writer, args);
4681 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004682 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004683
4684 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004685 if (DEBUG_DUMP_LOG) {
4686 synchronized (sDumpLogs) {
4687 Log.d(TAG, "");
4688 Log.d(TAG, "*********************");
4689 Log.d(TAG, "Launcher debug logs: ");
4690 for (int i = 0; i < sDumpLogs.size(); i++) {
4691 Log.d(TAG, " " + sDumpLogs.get(i));
4692 }
4693 Log.d(TAG, "*********************");
4694 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004695 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004696 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004697 }
4698
4699 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004700 addDumpLog(tag, log, null, debugLog);
4701 }
4702
4703 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004704 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004705 if (e != null) {
4706 Log.d(tag, log, e);
4707 } else {
4708 Log.d(tag, log);
4709 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004710 }
Winson Chungede41292013-09-19 16:27:36 -07004711 if (DEBUG_DUMP_LOG) {
4712 sDateStamp.setTime(System.currentTimeMillis());
4713 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004714 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4715 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004716 }
Winson Chungede41292013-09-19 16:27:36 -07004717 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004718 }
4719
Adam Cohen59400422014-03-05 18:07:04 -08004720 public static CustomAppWidget getCustomAppWidget(String name) {
4721 return sCustomAppWidgets.get(name);
4722 }
4723
4724 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4725 return sCustomAppWidgets;
4726 }
4727
Winson Chungede41292013-09-19 16:27:36 -07004728 public void dumpLogsToLocalData() {
4729 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004730 new AsyncTask<Void, Void, Void>() {
4731 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004732 boolean success = false;
4733 sDateStamp.setTime(sRunStart);
4734 String FILENAME = sDateStamp.getMonth() + "-"
4735 + sDateStamp.getDay() + "_"
4736 + sDateStamp.getHours() + "-"
4737 + sDateStamp.getMinutes() + "_"
4738 + sDateStamp.getSeconds() + ".txt";
4739
4740 FileOutputStream fos = null;
4741 File outFile = null;
4742 try {
4743 outFile = new File(getFilesDir(), FILENAME);
4744 outFile.createNewFile();
4745 fos = new FileOutputStream(outFile);
4746 } catch (Exception e) {
4747 e.printStackTrace();
4748 }
4749 if (fos != null) {
4750 PrintWriter writer = new PrintWriter(fos);
4751
4752 writer.println(" ");
4753 writer.println("Debug logs: ");
4754 synchronized (sDumpLogs) {
4755 for (int i = 0; i < sDumpLogs.size(); i++) {
4756 writer.println(" " + sDumpLogs.get(i));
4757 }
4758 }
4759 writer.close();
4760 }
4761 try {
4762 if (fos != null) {
4763 fos.close();
4764 success = true;
4765 }
4766 } catch (IOException e) {
4767 e.printStackTrace();
4768 }
Michael Jurka43467462013-11-14 12:03:58 +01004769 return null;
Winson Chungede41292013-09-19 16:27:36 -07004770 }
Michael Jurka43467462013-11-14 12:03:58 +01004771 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004772 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004774}
Michael Jurka2763be32011-02-24 11:19:57 -08004775
Michael Jurkaabded662011-03-04 12:06:57 -08004776interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004777 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004778 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004779 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004780 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004781 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004782}
Dan Sandlerd5024042014-01-09 15:01:33 -05004783
4784interface DebugIntents {
4785 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4786 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004787}