blob: b17a92f4722aefa56b5fe23778da3a34ead00d42 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700103import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700105import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700106import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700107import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700108
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.DataInputStream;
110import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800117import java.lang.reflect.InvocationTargetException;
118import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700121import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700124import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700125import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000126import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
134 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Winson Chung83f59ab2015-05-05 17:21:58 -0700138 // Temporary flag
139 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700142 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
181 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: boolean
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
189 // Type: long
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800218 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000233 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
234 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
235
Winson Chunga2413752012-04-03 14:22:34 -0700236 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700237 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
238 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700240
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800241 private final BroadcastReceiver mCloseSystemDialogsReceiver
242 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private LayoutInflater mInflater;
246
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700248 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800249 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700250 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800251 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700252 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Sunny Goyalffe83f12014-08-14 17:39:34 -0700254 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Adam Cohen091440a2015-03-18 14:16:05 -0700257 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800258 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800259 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700260
Michael Jurka0280c3b2010-09-17 15:00:07 -0700261 private int[] mTmpAddItemCellCoordinates = new int[2];
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private FolderInfo mFolderInfo;
264
Winson Chung3d503fb2011-07-13 17:25:49 -0700265 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800266 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700267
Michael Jurka838a4ca2011-02-07 13:33:06 -0800268 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700269
Winson Chungc51db6a2011-10-05 11:44:49 -0700270 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
272 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700273 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the widget tray screen.
276 private WidgetsContainerView mWidgetsView;
277
Winson Chungf0ea4d32011-06-06 14:27:16 -0700278 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800279 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
283 // scroll issues (because the workspace may not have been measured yet) and extra work.
284 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
285 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
287 private SpannableStringBuilder mDefaultKeySsb = null;
288
Adam Cohen091440a2015-03-18 14:16:05 -0700289 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800291 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private boolean mRestoring;
293 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700294 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700297 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 private Bundle mSavedInstanceState;
300
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700305 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800306 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309
Sunny Goyale2df0622015-04-24 11:27:00 -0700310 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
Adam Cohen61f560d2013-09-30 15:58:20 -0700312 private View.OnTouchListener mHapticFeedbackTouchListener;
313
Adam Cohended9f8d2010-11-03 13:25:16 -0700314 // Related to the auto-advancing of widgets
315 private final int ADVANCE_MSG = 1;
316 private final int mAdvanceInterval = 20000;
317 private final int mAdvanceStagger = 250;
318 private long mAutoAdvanceSentTime;
319 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700320 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700321 new HashMap<View, AppWidgetProviderInfo>();
322
Winson Chung400438b2011-01-16 17:53:48 -0800323 // Determines how long to wait after a rotation before restoring the screen orientation to
324 // match the sensor state.
325 private final int mRestoreScreenOrientationDelay = 500;
326
Adam Cohen091440a2015-03-18 14:16:05 -0700327 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 // Holds the page that we need to animate to, and the icon views that we need to animate up
344 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700345 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700346 private Bitmap mFolderIconBitmap;
347 private Canvas mFolderIconCanvas;
348 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700350 private DeviceProfile mDeviceProfile;
351
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 private BubbleTextView mWaitingForResume;
353
Adam Cohen59400422014-03-05 18:07:04 -0800354 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
355 new HashMap<String, CustomAppWidget>();
356
357 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
358 static {
359 if (ENABLE_CUSTOM_WIDGET_TEST) {
360 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
361 }
362 }
363
Chet Haasea8f996d2015-02-17 12:56:04 -0800364 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
365 private static Method sClipRevealMethod = null;
366 static {
367 Class<?> activityOptionsClass = ActivityOptions.class;
368 try {
369 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
370 View.class, int.class, int.class, int.class, int.class);
371 } catch (Exception e) {
372 // Earlier version
373 }
374 }
375
Adam Cohen091440a2015-03-18 14:16:05 -0700376 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700377 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700378 if (mWorkspace != null) {
379 mWorkspace.buildPageHardwareLayers();
380 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700381 }
382 };
383
Adam Cohendb364c32014-05-20 14:23:40 -0700384 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800385
Adam Cohen091440a2015-03-18 14:16:05 -0700386 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800387 int requestCode;
388 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700389 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700390 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391 int cellX;
392 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700393 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800394 }
395
Daniel Sandlerff02d492013-08-05 02:12:05 -0400396 private Stats mStats;
397
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700398 FocusIndicatorView mFocusHandler;
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 @Override
401 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700402 if (DEBUG_STRICT_MODE) {
403 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
404 .detectDiskReads()
405 .detectDiskWrites()
406 .detectNetwork() // or .detectAll() for all detectable problems
407 .penaltyLog()
408 .build());
409 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
410 .detectLeakedSqlLiteObjects()
411 .detectLeakedClosableObjects()
412 .penaltyLog()
413 .penaltyDeath()
414 .build());
415 }
416
Adam Cohen9211d422014-10-07 18:14:53 -0700417 if (mLauncherCallbacks != null) {
418 mLauncherCallbacks.preOnCreate();
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400422
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400423 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400424 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700425 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700426
Winson Chung5f8afe62013-08-12 16:19:28 -0700427 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700428 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700429
430 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400431 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700432 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700433 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800434 mModel = app.setLauncher(this);
435 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700436 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400437 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800439 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Daniel Sandlerff02d492013-08-05 02:12:05 -0400441 mStats = new Stats(this);
442
Sunny Goyalffe83f12014-08-14 17:39:34 -0700443 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700444
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
446 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700447
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700448 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
449 // this also ensures that any synchronous binding below doesn't re-trigger another
450 // LauncherModel load.
451 mPaused = false;
452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200454 android.os.Debug.startMethodTracing(
455 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457
458 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700462 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800464 registerContentObservers();
465
Joe Onorato7c312c12009-08-13 21:36:53 -0700466 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 mSavedState = savedInstanceState;
469 restoreState(mSavedState);
470
471 if (PROFILE_STARTUP) {
472 android.os.Debug.stopMethodTracing();
473 }
474
475 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700476 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 // If the user leaves launcher, then we should just load items asynchronously when
478 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500479 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700480 } else {
481 // We only load the page synchronously if the user rotates (or triggers a
482 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800483 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 }
486
487 // For handling default keys
488 mDefaultKeySsb = new SpannableStringBuilder();
489 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800490
491 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
492 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800493
Adam Cohenf9426d52012-06-04 17:26:21 -0700494 // On large interfaces, we want the screen to auto-rotate based on the current orientation
495 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700496
Adam Cohen9211d422014-10-07 18:14:53 -0700497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700499 if (mLauncherCallbacks.hasLauncherOverlay()) {
500 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700501 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
502 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
503 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700504 mWorkspace.setLauncherOverlay(mLauncherOverlay);
505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700507
508 if (shouldShowIntroScreen()) {
509 showIntroScreen();
510 } else {
511 showFirstRunActivity();
512 showFirstRunClings();
513 }
Adam Cohen9211d422014-10-07 18:14:53 -0700514 }
515
516 private LauncherCallbacks mLauncherCallbacks;
517
518 public void onPostCreate(Bundle savedInstanceState) {
519 super.onPostCreate(savedInstanceState);
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onPostCreate(savedInstanceState);
522 }
523 }
524
525 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
526 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700527 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
528 @Override
529 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700530 if (LOGD) {
531 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
532 }
Winson Chung94804152015-05-08 13:06:44 -0700533 mAppsView.setFixedBounds(bounds);
534 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700535 }
536
537 @Override
538 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700539 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
540 // Dismiss All Apps if we aren't already paused/invisible
541 if (!mPaused) {
542 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
543 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
544 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700545 }
Winson Chung0f785722015-04-08 10:27:49 -0700546 }
547 });
Adam Cohen9211d422014-10-07 18:14:53 -0700548 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 }
550
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700551 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700552 public void onLauncherProviderChange() {
553 if (mLauncherCallbacks != null) {
554 mLauncherCallbacks.onLauncherProviderChange();
555 }
556 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700557
Adam Cohen9211d422014-10-07 18:14:53 -0700558 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700559 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700560 if (mLauncherCallbacks != null) {
561 return mLauncherCallbacks.hasCustomContentToLeft();
562 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700563 return false;
564 }
565
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500567 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600568 * {@link #addToCustomContentPage}. This will only be invoked if
569 * {@link #hasCustomContentToLeft()} is {@code true}.
570 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500571 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700572 if (mLauncherCallbacks != null) {
573 mLauncherCallbacks.populateCustomContentContainer();
574 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600575 }
576
577 /**
578 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
579 * ensure the custom content page is added or removed if necessary.
580 */
581 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600582 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600583 // Not bound yet, wait for bindScreens to be called.
584 return;
585 }
586
587 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
588 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500589 mWorkspace.createCustomContentContainer();
590 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600591 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
592 mWorkspace.removeCustomContentPage();
593 }
594 }
595
Adam Cohen091440a2015-03-18 14:16:05 -0700596 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 if (sLocaleConfiguration == null) {
598 new AsyncTask<Void, Void, LocaleConfiguration>() {
599 @Override
600 protected LocaleConfiguration doInBackground(Void... unused) {
601 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
602 readConfiguration(Launcher.this, localeConfiguration);
603 return localeConfiguration;
604 }
605
606 @Override
607 protected void onPostExecute(LocaleConfiguration result) {
608 sLocaleConfiguration = result;
609 checkForLocaleChange(); // recursive, but now with a locale configuration
610 }
611 }.execute();
612 return;
613 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 final Configuration configuration = getResources().getConfiguration();
616
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700617 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 final String locale = configuration.locale.toString();
619
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 final int mcc = configuration.mcc;
622
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 final int mnc = configuration.mnc;
625
Romain Guy84f296c2009-11-04 15:00:44 -0800626 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627
Romain Guy84f296c2009-11-04 15:00:44 -0800628 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700629 sLocaleConfiguration.locale = locale;
630 sLocaleConfiguration.mcc = mcc;
631 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700632
Joe Onorato0589f0f2010-02-08 13:44:00 -0800633 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700634
635 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100636 new AsyncTask<Void, Void, Void>() {
637 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700638 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100639 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700640 }
Michael Jurka43467462013-11-14 12:03:58 +0100641 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700642 }
643 }
644
Adam Cohen091440a2015-03-18 14:16:05 -0700645 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700646 public String locale;
647 public int mcc = -1;
648 public int mnc = -1;
649 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700650
Adam Cohen091440a2015-03-18 14:16:05 -0700651 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700652 DataInputStream in = null;
653 try {
Helena Josol28db2802014-10-09 17:04:09 +0100654 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700655 configuration.locale = in.readUTF();
656 configuration.mcc = in.readInt();
657 configuration.mnc = in.readInt();
658 } catch (FileNotFoundException e) {
659 // Ignore
660 } catch (IOException e) {
661 // Ignore
662 } finally {
663 if (in != null) {
664 try {
665 in.close();
666 } catch (IOException e) {
667 // Ignore
668 }
669 }
670 }
671 }
672
Adam Cohen091440a2015-03-18 14:16:05 -0700673 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700674 DataOutputStream out = null;
675 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100676 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100677 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700678 out.writeUTF(configuration.locale);
679 out.writeInt(configuration.mcc);
680 out.writeInt(configuration.mnc);
681 out.flush();
682 } catch (FileNotFoundException e) {
683 // Ignore
684 } catch (IOException e) {
685 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100686 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700687 } finally {
688 if (out != null) {
689 try {
690 out.close();
691 } catch (IOException e) {
692 // Ignore
693 }
694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696 }
697
Anton Hanssona2f665f2013-09-26 12:18:32 +0100698 public Stats getStats() {
699 return mStats;
700 }
701
Bjorn Bringertc459e522013-06-07 19:36:01 +0100702 public LayoutInflater getInflater() {
703 return mInflater;
704 }
705
Hyunyoung Song3f471442015-04-08 19:01:34 -0700706 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700707 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
708 // that is subsequently removed from the workspace in startBinding().
709 return !mModel.isLoadingWorkspace();
710 }
711
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800712 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000713 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100714 if (Build.VERSION.SDK_INT >= 17) {
715 return View.generateViewId();
716 } else {
717 // View.generateViewId() is not available. The following fallback logic is a copy
718 // of its implementation.
719 for (;;) {
720 final int result = sNextGeneratedId.get();
721 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
722 int newValue = result + 1;
723 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
724 if (sNextGeneratedId.compareAndSet(result, newValue)) {
725 return result;
726 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000727 }
728 }
729 }
730
731 public int getViewIdForItem(ItemInfo info) {
732 // This cast is safe given the > 2B range for int.
733 int itemId = (int) info.id;
734 if (mItemIdToViewId.containsKey(itemId)) {
735 return mItemIdToViewId.get(itemId);
736 }
737 int viewId = generateViewId();
738 mItemIdToViewId.put(itemId, viewId);
739 return viewId;
740 }
741
742 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700743 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
744 * a configuration step, this allows the proper animations to run after other transitions.
745 */
Adam Cohendb364c32014-05-20 14:23:40 -0700746 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700747 long screenId = args.screenId;
748 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
749 // When the screen id represents an actual screen (as opposed to a rank) we make sure
750 // that the drop page actually exists.
751 screenId = ensurePendingDropLayoutExists(args.screenId);
752 }
Adam Cohendb364c32014-05-20 14:23:40 -0700753
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700756 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700757 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700760 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700761 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700762 case REQUEST_RECONFIGURE_APPWIDGET:
763 completeRestoreAppWidget(args.appWidgetId);
764 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800765 }
Michael Jurka27614d22012-04-02 06:40:22 -0700766 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
767 // if you turned the screen off and then back while in All Apps, Launcher would not
768 // return to the workspace. Clearing mAddInfo.container here fixes this issue
769 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700770 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800771 }
772
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800773 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700775 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700776 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800778 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700779
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 Runnable exitSpringLoaded = new Runnable() {
781 @Override
782 public void run() {
783 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
784 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
785 }
786 };
787
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
791 if (resultCode == RESULT_CANCELED) {
792 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700793 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700795 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800796 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700798 }
799 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200800 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
801 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700802 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200803 }
804 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700805 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200806
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
808 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700809
Adam Cohendb364c32014-05-20 14:23:40 -0700810 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 // We have special handling for widgets
812 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800813 final int appWidgetId;
814 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
815 : -1;
816 if (widgetId < 0) {
817 appWidgetId = pendingAddWidgetId;
818 } else {
819 appWidgetId = widgetId;
820 }
821
Adam Cohenad4e15c2013-10-17 16:21:35 -0700822 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800823 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700824 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
825 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 result = RESULT_CANCELED;
827 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700828 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 @Override
830 public void run() {
831 exitSpringLoadedDragModeDelayed(false, 0, null);
832 }
833 };
Adam Cohendb364c32014-05-20 14:23:40 -0700834 if (workspaceLocked) {
835 // No need to remove the empty screen if we're mid-binding, as the
836 // the bind will not add the empty screen.
837 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
838 } else {
839 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
840 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
841 }
Winson Chung5aaab772012-04-27 15:24:02 -0700842 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700843 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700844 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
845 // When the screen id represents an actual screen (as opposed to a rank)
846 // we make sure that the drop page actually exists.
847 mPendingAddInfo.screenId =
848 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
849 }
Adam Cohendb364c32014-05-20 14:23:40 -0700850 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
851
852 dropLayout.setDropPending(true);
853 final Runnable onComplete = new Runnable() {
854 @Override
855 public void run() {
856 completeTwoStageWidgetDrop(resultCode, appWidgetId);
857 dropLayout.setDropPending(false);
858 }
859 };
860 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
861 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
862 } else {
863 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
864 mPendingAddInfo);
865 sPendingAddItem = args;
866 }
Winson Chung5aaab772012-04-27 15:24:02 -0700867 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 return;
869 }
870
Sunny Goyalff572272014-07-23 13:58:07 -0700871 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
872 if (resultCode == RESULT_OK) {
873 // Update the widget view.
874 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
875 pendingAddWidgetId, mPendingAddInfo);
876 if (workspaceLocked) {
877 sPendingAddItem = args;
878 } else {
879 completeAdd(args);
880 }
881 }
882 // Leave the widget in the pending state if the user canceled the configure.
883 return;
884 }
885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 // The pattern used here is that a user PICKs a specific application,
887 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
890 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700891 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700892 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
893 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800894 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700895 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800896 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700897 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700898 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
899 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
Adam Cohen00074722013-10-11 17:46:09 -0700901 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700902 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700903 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800906
907 }
908
909 @Override
910 protected void onActivityResult(
911 final int requestCode, final int resultCode, final Intent data) {
912 handleActivityResult(requestCode, resultCode, data);
913 if (mLauncherCallbacks != null) {
914 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
915 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 }
917
Adam Cohendb364c32014-05-20 14:23:40 -0700918 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
919 appWidgetId, ItemInfo info) {
920 PendingAddArguments args = new PendingAddArguments();
921 args.requestCode = requestCode;
922 args.intent = data;
923 args.container = info.container;
924 args.screenId = info.screenId;
925 args.cellX = info.cellX;
926 args.cellY = info.cellY;
927 args.appWidgetId = appWidgetId;
928 return args;
929 }
930
931 /**
932 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
933 *
934 * @param screenId the screen id to check
935 * @return the new screen, or screenId if it exists
936 */
937 private long ensurePendingDropLayoutExists(long screenId) {
938 CellLayout dropLayout =
939 (CellLayout) mWorkspace.getScreenWithId(screenId);
940 if (dropLayout == null) {
941 // it's possible that the add screen was removed because it was
942 // empty and a re-bind occurred
943 mWorkspace.addExtraEmptyScreen();
944 return mWorkspace.commitExtraEmptyScreen();
945 } else {
946 return screenId;
947 }
948 }
949
Adam Cohen091440a2015-03-18 14:16:05 -0700950 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700951 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 Runnable onCompleteRunnable = null;
954 int animationType = 0;
955
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800957 if (resultCode == RESULT_OK) {
958 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
959 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700960 mPendingAddWidgetInfo);
961 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800962 onCompleteRunnable = new Runnable() {
963 @Override
964 public void run() {
965 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700966 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700967 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
968 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800969 }
970 };
971 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800972 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800973 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800974 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700975 if (mDragLayer.getAnimatedView() != null) {
976 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
977 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
978 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700979 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700980 // The animated view may be null in the case of a rotation during widget configuration
981 onCompleteRunnable.run();
982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 }
984
985 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700986 protected void onStop() {
987 super.onStop();
988 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700989
990 if (mLauncherCallbacks != null) {
991 mLauncherCallbacks.onStop();
992 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700993 }
994
995 @Override
996 protected void onStart() {
997 super.onStart();
998 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700999
1000 if (mLauncherCallbacks != null) {
1001 mLauncherCallbacks.onStart();
1002 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001003 }
1004
1005 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001007 long startTime = 0;
1008 if (DEBUG_RESUME_TIME) {
1009 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001010 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001011 }
Adam Cohen9211d422014-10-07 18:14:53 -07001012
1013 if (mLauncherCallbacks != null) {
1014 mLauncherCallbacks.preOnResume();
1015 }
1016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001018
Winson Chung4a2afa32012-07-19 14:53:05 -07001019 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001020 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001021 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001022 } else if (mOnResumeState == State.APPS) {
1023 showAppsView(false /* animated */, false /* resetListToTop */);
1024 } else if (mOnResumeState == State.WIDGETS) {
1025 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001026 }
1027 mOnResumeState = State.NONE;
1028
Winson Chungcd99cd32015-04-29 11:03:24 -07001029 // Restore the apps state if we are in all apps
Winson Chung83f59ab2015-05-05 17:21:58 -07001030 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mState == State.APPS) {
Winson Chungcd99cd32015-04-29 11:03:24 -07001031 if (mLauncherCallbacks != null) {
1032 mLauncherCallbacks.onAllAppsShown();
1033 }
1034 }
1035
Craig Mautner360310b2012-10-26 15:13:08 -07001036 // Background was set to gradient in onPause(), restore to black if in all apps.
1037 setWorkspaceBackground(mState == State.WORKSPACE);
1038
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001039 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001040 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001041 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001042 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001044 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001046 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001047 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1048 // execute them here
1049 long startTimeCallbacks = 0;
1050 if (DEBUG_RESUME_TIME) {
1051 startTimeCallbacks = System.currentTimeMillis();
1052 }
1053
Michael Jurka1e2f4652013-07-08 18:03:46 -07001054 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1055 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001056 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001057 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001058 if (DEBUG_RESUME_TIME) {
1059 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1060 (System.currentTimeMillis() - startTimeCallbacks));
1061 }
Michael Jurka447bf842013-05-15 14:52:15 +02001062 }
Michael Jurka54554252013-08-01 12:52:23 +02001063 if (mOnResumeCallbacks.size() > 0) {
1064 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1065 mOnResumeCallbacks.get(i).run();
1066 }
1067 mOnResumeCallbacks.clear();
1068 }
Winson Chunge4e50662012-01-23 14:45:13 -08001069
1070 // Reset the pressed state of icons that were locked in the press state while activities
1071 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001072 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001073 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001074 mWaitingForResume.setStayPressed(false);
1075 }
Winson Chung82963d52013-10-09 11:20:57 -07001076
Adam Cohen06dff352012-06-01 17:17:08 -07001077 // It is possible that widgets can receive updates while launcher is not in the foreground.
1078 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1079 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1080 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001081 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001082 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001083
Michael Jurka447bf842013-05-15 14:52:15 +02001084 if (DEBUG_RESUME_TIME) {
1085 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1086 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 if (mWorkspace.getCustomContentCallbacks() != null) {
1089 // If we are resuming and the custom content is the current page, we call onShow().
1090 // It is also poassible that onShow will instead be called slightly after first layout
1091 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1092 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001093 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001094 }
1095 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001096 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001097 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001098
Sunny Goyal756adbc2015-04-16 15:20:51 -07001099 if (!isWorkspaceLoading()) {
1100 // Process any items that were added while Launcher was away.
1101 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1102 }
Adam Cohen9211d422014-10-07 18:14:53 -07001103
1104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onResume();
1106 }
Adam Cohen06dff352012-06-01 17:17:08 -07001107 }
1108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001111 // Ensure that items added to Launcher are queued until Launcher returns
1112 InstallShortcutReceiver.enableInstallQueue();
1113
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001115 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001116 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001117 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001118
1119 // We call onHide() aggressively. The custom content callbacks should be able to
1120 // debounce excess onHide calls.
1121 if (mWorkspace.getCustomContentCallbacks() != null) {
1122 mWorkspace.getCustomContentCallbacks().onHide();
1123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Adam Cohen9211d422014-10-07 18:14:53 -07001125 if (mLauncherCallbacks != null) {
1126 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001128 }
1129
1130 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001131 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1132 // by a onResume or by scrolling otherwise.
1133 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001134
1135 // Custom content is completely hidden
1136 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001137
1138 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1139 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001140
1141 // Indicates whether the user is allowed to scroll away from the custom content.
1142 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001143 }
1144
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 public interface LauncherOverlay {
1146
1147 /**
1148 * Touch interaction leading to overscroll has begun
1149 */
1150 public void onScrollInteractionBegin();
1151
1152 /**
1153 * Touch interaction related to overscroll has ended
1154 */
1155 public void onScrollInteractionEnd();
1156
1157 /**
1158 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1159 * screen (or in the case of RTL, the rightmost screen).
1160 */
1161 public void onScrollChange(int progress, boolean rtl);
1162
1163 /**
1164 * Screen has stopped scrolling
1165 */
1166 public void onScrollSettled();
1167
1168 /**
1169 * This method can be called by the Launcher in order to force the LauncherOverlay
1170 * to exit fully immersive mode.
1171 */
1172 public void forceExitFullImmersion();
1173 }
1174
Winson Chung0f785722015-04-08 10:27:49 -07001175 public interface LauncherAppsCallbacks {
1176 /**
1177 * Updates launcher to the available space that AllApps can take so as not to overlap with
1178 * any other views.
1179 */
1180 public void onAllAppsBoundsChanged(Rect bounds);
1181
1182 /**
1183 * Called to dismiss all apps if it is showing.
1184 */
1185 public void dismissAllApps();
1186 }
1187
Adam Cohenc2d6e892014-10-16 09:49:24 -07001188 public interface LauncherOverlayCallbacks {
1189 /**
1190 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1191 * however it doesn't modify any state within the launcher.
1192 */
1193 public boolean canEnterFullImmersion();
1194
1195 /**
1196 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1197 * eg. by occupying the full screen and handling all touch events.
1198 *
1199 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1200 * case, Launcher will modify any necessary state and assumes the overlay is
1201 * handling all interaction. If false, the LauncherOverlay should cancel any
1202 *
1203 */
1204 public boolean enterFullImmersion();
1205
1206 /**
1207 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1208 * full control over UI and state.
1209 */
1210 public void exitFullImmersion();
1211 }
1212
1213 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1214
1215 @Override
1216 public boolean canEnterFullImmersion() {
1217 return mState == State.WORKSPACE;
1218 }
1219
1220 @Override
1221 public boolean enterFullImmersion() {
1222 if (mState == State.WORKSPACE) {
1223 // When fully immersed, disregard any touches which fall through.
1224 mDragLayer.setBlockTouch(true);
1225 return true;
1226 }
1227 return false;
1228 }
1229
1230 @Override
1231 public void exitFullImmersion() {
1232 mDragLayer.setBlockTouch(false);
1233 }
1234 }
1235
Jorim Jaggid017f882014-01-14 17:08:48 -08001236 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001237 if (mLauncherCallbacks != null) {
1238 return mLauncherCallbacks.hasSettings();
1239 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001240 return false;
1241 }
1242
Adam Cohen9211d422014-10-07 18:14:53 -07001243 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001244 CustomContentCallbacks callbacks, String description) {
1245 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001246 }
1247
Adam Cohenedb40762013-07-18 16:45:45 -07001248 // The custom content needs to offset its content to account for the QSB
1249 public int getTopOffsetForCustomContent() {
1250 return mWorkspace.getPaddingTop();
1251 }
1252
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001253 @Override
1254 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001256 if (mModel.isCurrentCallbacks(this)) {
1257 mModel.stopLoader();
1258 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001259 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1260
Romain Guy13c2e7b2010-03-10 19:45:00 -08001261 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001262 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001263
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001264 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001265 @Override
1266 public void onWindowFocusChanged(boolean hasFocus) {
1267 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001268 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001269
1270 if (mLauncherCallbacks != null) {
1271 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1272 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 private boolean acceptFilter() {
1276 final InputMethodManager inputManager = (InputMethodManager)
1277 getSystemService(Context.INPUT_METHOD_SERVICE);
1278 return !inputManager.isFullscreenMode();
1279 }
1280
1281 @Override
1282 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001283 final int uniChar = event.getUnicodeChar();
1284 final boolean handled = super.onKeyDown(keyCode, event);
1285 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1286 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1288 keyCode, event);
1289 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001291 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001292 // showSearchDialog()
1293 // If there are multiple keystrokes before the search dialog takes focus,
1294 // onSearchRequested() will be called for every keystroke,
1295 // but it is idempotent, so it's fine.
1296 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298 }
1299
Joe Onorato8a9625e2010-01-28 15:55:35 -08001300 // Eat the long press event so the keyboard doesn't come up.
1301 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1302 return true;
1303 }
1304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 return handled;
1306 }
1307
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 private String getTypedText() {
1309 return mDefaultKeySsb.toString();
1310 }
1311
1312 private void clearTypedText() {
1313 mDefaultKeySsb.clear();
1314 mDefaultKeySsb.clearSpans();
1315 Selection.setSelection(mDefaultKeySsb, 0);
1316 }
1317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001319 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1320 * State
1321 */
1322 private static State intToState(int stateOrdinal) {
1323 State state = State.WORKSPACE;
1324 final State[] stateValues = State.values();
1325 for (int i = 0; i < stateValues.length; i++) {
1326 if (stateValues[i].ordinal() == stateOrdinal) {
1327 state = stateValues[i];
1328 break;
1329 }
1330 }
1331 return state;
1332 }
1333
1334 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 * Restores the previous state, if it exists.
1336 *
1337 * @param savedState The previous state.
1338 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001339 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 private void restoreState(Bundle savedState) {
1341 if (savedState == null) {
1342 return;
1343 }
1344
Michael Jurka883f55b2010-10-21 15:47:14 -07001345 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001346 if (state == State.APPS || state == State.WIDGETS) {
1347 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001348 }
1349
Adam Cohen21cd0022013-10-09 18:57:02 -07001350 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1351 PagedView.INVALID_RESTORE_PAGE);
1352 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001353 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1360 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001361 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1363 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001364 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1365 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001366 AppWidgetProviderInfo info = savedState.getParcelable(
1367 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1368 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001369 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001370 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 mRestoring = true;
1372 }
1373
1374 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1375 if (renameFolder) {
1376 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001377 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 mRestoring = true;
1379 }
Winson Chunga12a2502010-12-20 14:41:35 -08001380
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001381 mItemIdToViewId = (HashMap<Integer, Integer>)
1382 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
1385 /**
1386 * Finds all the views we need and configure them properly.
1387 */
1388 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001389 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001390
Craig Mautner360310b2012-10-26 15:13:08 -07001391 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001392 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001393 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1394 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001395 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001396 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001397
John Spurlock77e1f472013-09-11 10:09:51 -04001398 mLauncherView.setSystemUiVisibility(
1399 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001400 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Winson Chung4c98d922011-05-31 16:50:48 -07001402 // Setup the drag layer
1403 mDragLayer.setup(this, dragController);
1404
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 // Setup the hotseat
1406 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1407 if (mHotseat != null) {
1408 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001409 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 }
1411
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001413 View widgetButton = findViewById(R.id.widget_button);
1414 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415 @Override
1416 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001417 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001418 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001419 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 }
1421 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001422 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 View wallpaperButton = findViewById(R.id.wallpaper_button);
1425 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001426 @Override
1427 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001428 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001429 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001430 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431 }
1432 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001433 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1434
1435 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 if (hasSettings()) {
1437 settingsButton.setOnClickListener(new OnClickListener() {
1438 @Override
1439 public void onClick(View arg0) {
1440 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001441 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001442 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001443 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001444 });
1445 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1446 } else {
1447 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 }
1449
Adam Cohendbdff6b2013-09-18 19:09:15 -07001450 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001451
Winson Chung4c98d922011-05-31 16:50:48 -07001452 // Setup the workspace
1453 mWorkspace.setHapticFeedbackEnabled(false);
1454 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001455 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001456 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001457
Winson Chungf0ea4d32011-06-06 14:27:16 -07001458 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001459 mSearchDropTargetBar = (SearchDropTargetBar)
1460 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001461
Winson Chungb745afb2015-03-02 11:51:23 -08001462 // Setup Apps
1463 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001464 if (isAllAppsSearchOverridden()) {
1465 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001466 }
Winson Chungb745afb2015-03-02 11:51:23 -08001467
Winson Chungf0ea4d32011-06-06 14:27:16 -07001468 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001469 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001470
Winson Chung3d503fb2011-07-13 17:25:49 -07001471 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001472 dragController.setDragScoller(mWorkspace);
1473 dragController.setScrollView(mDragLayer);
1474 dragController.setMoveTarget(mWorkspace);
1475 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001476 if (mSearchDropTargetBar != null) {
1477 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001478 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001479 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001480
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001481 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001482 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001483 mWeightWatcher = new WeightWatcher(this);
1484 mWeightWatcher.setAlpha(0.5f);
1485 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 new FrameLayout.LayoutParams(
1487 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001488 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001489 Gravity.BOTTOM)
1490 );
Adam Cohen39a06042013-07-19 14:30:12 -07001491
1492 boolean show = shouldShowWeightWatcher();
1493 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001498 * Sets the all apps button. This method is called from {@link Hotseat}.
1499 */
1500 public void setAllAppsButton(View allAppsButton) {
1501 mAllAppsButton = allAppsButton;
1502 }
1503
1504 public View getAllAppsButton() {
1505 return mAllAppsButton;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Creates a view representing a shortcut.
1510 *
1511 * @param info The data structure describing the shortcut.
1512 *
1513 * @return A View inflated from R.layout.application.
1514 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001515 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001516 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
1518
1519 /**
1520 * Creates a view representing a shortcut inflated from the specified resource.
1521 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 * @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 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001527 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1528 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1529 parent, false);
1530 favorite.applyFromShortcutInfo(info, mIconCache);
1531 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001533 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 return favorite;
1535 }
1536
1537 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 * Add a shortcut to the workspace.
1539 *
1540 * @param data The intent describing the shortcut.
1541 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 int cellY) {
1545 int[] cellXY = mTmpAddItemCellCoordinates;
1546 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Michael Jurkad3ef3062010-11-23 16:23:58 -08001549 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550
Sunny Goyal2350bc92014-10-14 16:42:54 -07001551 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001552 if (info == null) {
1553 return;
1554 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001555 final View view = createShortcut(info);
1556
Adam Cohenfbba09b2011-07-18 21:43:05 -07001557 // First we check if we already know the exact location where we want to add this item.
1558 if (cellX >= 0 && cellY >= 0) {
1559 cellXY[0] = cellX;
1560 cellXY[1] = cellY;
1561 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001562
1563 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001564 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001565 true, null,null)) {
1566 return;
1567 }
1568 DragObject dragObject = new DragObject();
1569 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001570 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1571 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001572 return;
1573 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001575 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001576 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001577 foundCellSpan = (result != null);
1578 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001579 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001580 }
1581
1582 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001583 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 return;
1585 }
1586
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001587 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588
1589 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001591 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593 }
1594
Adam Cohen2f093b62012-04-30 18:59:53 -07001595 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1596 int minHeight) {
1597 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001598 // We want to account for the extra amount of padding that we are adding to the widget
1599 // to ensure that it gets the full amount of space that it has requested
1600 int requiredWidth = minWidth + padding.left + padding.right;
1601 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001602 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001603 }
1604
Adam Cohen2f093b62012-04-30 18:59:53 -07001605 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1606 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001607 }
1608
Adam Cohen2f093b62012-04-30 18:59:53 -07001609 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1610 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001611 }
1612
Adam Cohen2f093b62012-04-30 18:59:53 -07001613 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1614 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001615 }
1616
Adam Cohen2f093b62012-04-30 18:59:53 -07001617 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1618 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001619 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001623 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001625 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 */
Adam Cohen091440a2015-03-18 14:16:05 -07001627 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001628 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1629
1630 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001632 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1633 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 }
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Adam Cohen59400422014-03-05 18:07:04 -08001636 if (appWidgetInfo.isCustomWidget) {
1637 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001638 }
1639
Adam Cohen59400422014-03-05 18:07:04 -08001640 LauncherAppWidgetInfo launcherInfo;
1641 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1642 launcherInfo.spanX = info.spanX;
1643 launcherInfo.spanY = info.spanY;
1644 launcherInfo.minSpanX = info.minSpanX;
1645 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001646 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001649 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
1651 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001652 if (hostView == null) {
1653 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001654 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1655 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001656 } else {
1657 // The AppWidgetHostView has already been inflated and instantiated
1658 launcherInfo.hostView = hostView;
1659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001661 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001662 launcherInfo.notifyWidgetSizeChanged(this);
1663
Adam Cohen59400422014-03-05 18:07:04 -08001664 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1665 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001666
1667 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001669 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
Romain Guycbb89e42009-06-08 15:52:54 -07001671
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1673 @Override
1674 public void onReceive(Context context, Intent intent) {
1675 final String action = intent.getAction();
1676 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1677 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001678 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001679 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001680
Winson Chungc100e8e2011-08-09 16:02:43 -07001681 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001682 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001683 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001684 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001685 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1688 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001689 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001690 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1691 mModel.resetLoadedState(false, true);
1692 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1693 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1694 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1695 mModel.resetLoadedState(false, true);
1696 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1697 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1698 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 }
1700 }
1701 };
1702
1703 @Override
1704 public void onAttachedToWindow() {
1705 super.onAttachedToWindow();
1706
1707 // Listen for broadcasts related to user-presence
1708 final IntentFilter filter = new IntentFilter();
1709 filter.addAction(Intent.ACTION_SCREEN_OFF);
1710 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001711 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001712 if (ENABLE_DEBUG_INTENTS) {
1713 filter.addAction(DebugIntents.DELETE_DATABASE);
1714 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1715 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001717 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001718 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001719 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 mVisible = true;
1721 }
1722
Adam Cohen0b395352014-06-09 22:54:36 +00001723 /**
1724 * Sets up transparent navigation and status bars in LMP.
1725 * This method is a no-op for other platform versions.
1726 */
Sunny Goyald0091012015-01-20 15:55:34 -08001727 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001728 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001729 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001730 Window window = getWindow();
1731 window.getAttributes().systemUiVisibility |=
1732 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1733 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1734 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1735 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1736 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1737 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1738 window.setStatusBarColor(Color.TRANSPARENT);
1739 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001740 }
1741 }
1742
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 @Override
1744 public void onDetachedFromWindow() {
1745 super.onDetachedFromWindow();
1746 mVisible = false;
1747
Adam Cohend113e0c2010-11-11 10:48:05 -08001748 if (mAttached) {
1749 unregisterReceiver(mReceiver);
1750 mAttached = false;
1751 }
Winson Chungb745afb2015-03-02 11:51:23 -08001752 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754
1755 public void onWindowVisibilityChanged(int visibility) {
1756 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001757 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001758 // The following code used to be in onResume, but it turns out onResume is called when
1759 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1760 // is a more appropriate event to handle
1761 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 if (!mWorkspaceLoading) {
1763 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001764 // We want to let Launcher draw itself at least once before we force it to build
1765 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001766 // apps is nice and speedy.
1767 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001769 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001770 if (mStarted) return;
1771 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001772 // We delay the layer building a bit in order to give
1773 // other message processing a time to run. In particular
1774 // this avoids a delay in hiding the IME if it was
1775 // currently shown, because doing that may involve
1776 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001777 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001778 final ViewTreeObserver.OnDrawListener listener = this;
1779 mWorkspace.post(new Runnable() {
1780 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001781 if (mWorkspace != null &&
1782 mWorkspace.getViewTreeObserver() != null) {
1783 mWorkspace.getViewTreeObserver().
1784 removeOnDrawListener(listener);
1785 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001786 }
1787 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001788 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001789 }
1790 });
1791 }
1792 clearTypedText();
1793 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 }
1795
Adam Cohen091440a2015-03-18 14:16:05 -07001796 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 mHandler.removeMessages(ADVANCE_MSG);
1798 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1799 mHandler.sendMessageDelayed(msg, delay);
1800 mAutoAdvanceSentTime = System.currentTimeMillis();
1801 }
1802
Adam Cohen091440a2015-03-18 14:16:05 -07001803 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1805 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1806 mAutoAdvanceRunning = autoAdvanceRunning;
1807 if (autoAdvanceRunning) {
1808 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1809 sendAdvanceMessage(delay);
1810 } else {
1811 if (!mWidgetsToAdvance.isEmpty()) {
1812 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1813 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1814 }
1815 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001816 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001817 }
1818 }
1819 }
1820
1821 private final Handler mHandler = new Handler() {
1822 @Override
1823 public void handleMessage(Message msg) {
1824 if (msg.what == ADVANCE_MSG) {
1825 int i = 0;
1826 for (View key: mWidgetsToAdvance.keySet()) {
1827 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1828 final int delay = mAdvanceStagger * i;
1829 if (v instanceof Advanceable) {
1830 postDelayed(new Runnable() {
1831 public void run() {
1832 ((Advanceable) v).advance();
1833 }
1834 }, delay);
1835 }
1836 i++;
1837 }
1838 sendAdvanceMessage(mAdvanceInterval);
1839 }
1840 }
1841 };
1842
1843 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001844 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001845 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1846 if (v instanceof Advanceable) {
1847 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001848 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001849 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 }
1851 }
1852
1853 void removeWidgetToAutoAdvance(View hostView) {
1854 if (mWidgetsToAdvance.containsKey(hostView)) {
1855 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001856 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001857 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001858 }
1859
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001861 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 launcherInfo.hostView = null;
1863 }
1864
Hyunyoung Song3f471442015-04-08 19:01:34 -07001865 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001866 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1867 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001868 }
1869
Winson Chunga6945242014-01-08 14:04:34 -08001870 public DragLayer getDragLayer() {
1871 return mDragLayer;
1872 }
1873
Winson Chungb745afb2015-03-02 11:51:23 -08001874 public AppsContainerView getAppsView() {
1875 return mAppsView;
1876 }
1877
Hyunyoung Song3f471442015-04-08 19:01:34 -07001878 public WidgetsContainerView getWidgetsView() {
1879 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001880 }
1881
Winson Chunga6945242014-01-08 14:04:34 -08001882 public Workspace getWorkspace() {
1883 return mWorkspace;
1884 }
1885
1886 public Hotseat getHotseat() {
1887 return mHotseat;
1888 }
1889
Jorim Jaggid017f882014-01-14 17:08:48 -08001890 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001891 return mOverviewPanel;
1892 }
1893
1894 public SearchDropTargetBar getSearchBar() {
1895 return mSearchDropTargetBar;
1896 }
1897
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001898 public LauncherAppWidgetHost getAppWidgetHost() {
1899 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
Romain Guycbb89e42009-06-08 15:52:54 -07001901
Winson Chunga9abd0e2010-10-27 17:18:37 -07001902 public LauncherModel getModel() {
1903 return mModel;
1904 }
1905
Winson Chunga6945242014-01-08 14:04:34 -08001906 protected SharedPreferences getSharedPrefs() {
1907 return mSharedPrefs;
1908 }
1909
Bjorn Bringertc459e522013-06-07 19:36:01 +01001910 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 getWindow().closeAllPanels();
1912
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001913 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001914 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001915 }
1916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 @Override
1918 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001919 long startTime = 0;
1920 if (DEBUG_RESUME_TIME) {
1921 startTime = System.currentTimeMillis();
1922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 super.onNewIntent(intent);
1924
1925 // Close the menu
1926 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001927 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001928 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001929
Adam Cohened307df2013-10-02 09:37:31 -07001930 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1931 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1932 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001933
Adam Cohen6fecd412013-10-02 17:41:50 -07001934 if (mWorkspace == null) {
1935 // Can be cases where mWorkspace is null, this prevents a NPE
1936 return;
1937 }
1938 Folder openFolder = mWorkspace.getOpenFolder();
1939 // In all these cases, only animate if we're already on home
1940 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001941
1942 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1943 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001944 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001945 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001947 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001948
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 closeFolder();
1950 exitSpringLoadedDragMode();
1951
1952 // If we are already on home, then just animate back to the workspace,
1953 // otherwise, just wait until onResume to set the state back to Workspace
1954 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001955 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001956 } else {
1957 mOnResumeState = State.WORKSPACE;
1958 }
1959
1960 final View v = getWindow().peekDecorView();
1961 if (v != null && v.getWindowToken() != null) {
1962 InputMethodManager imm = (InputMethodManager)getSystemService(
1963 INPUT_METHOD_SERVICE);
1964 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1965 }
1966
Winson Chungb745afb2015-03-02 11:51:23 -08001967 // Reset the apps view
1968 if (!alreadyOnHome && mAppsView != null) {
1969 mAppsView.scrollToTop();
1970 }
1971
Hyunyoung Song3f471442015-04-08 19:01:34 -07001972 // Reset the widgets view
1973 if (!alreadyOnHome && mWidgetsView != null) {
1974 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001975 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001976
Adam Cohen9211d422014-10-07 18:14:53 -07001977 if (mLauncherCallbacks != null) {
1978 mLauncherCallbacks.onHomeIntent();
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
Adam Cohened307df2013-10-02 09:37:31 -07001981
Michael Jurka447bf842013-05-15 14:52:15 +02001982 if (DEBUG_RESUME_TIME) {
1983 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985
Adam Cohen9211d422014-10-07 18:14:53 -07001986 if (mLauncherCallbacks != null) {
1987 mLauncherCallbacks.onNewIntent(intent);
1988 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001992 public void onRestoreInstanceState(Bundle state) {
1993 super.onRestoreInstanceState(state);
1994 for (int page: mSynchronouslyBoundPages) {
1995 mWorkspace.restoreInstanceStateForChild(page);
1996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
1999 @Override
2000 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002001 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002002 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2003 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002004 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002005 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006
Michael Jurka883f55b2010-10-21 15:47:14 -07002007 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002008 // We close any open folder since it will not be re-opened, and we need to make sure
2009 // this state is reflected.
2010 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002013 mWaitingForResult) {
2014 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002016 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2017 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002018 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2019 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2020 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002021 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 if (mFolderInfo != null && mWaitingForResult) {
2025 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2026 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2027 }
Michael Jurka946ad472010-07-09 18:05:18 -07002028
Hyunyoung Song3f471442015-04-08 19:01:34 -07002029 // Save the current widgets tray?
2030 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002031 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002032
2033 if (mLauncherCallbacks != null) {
2034 mLauncherCallbacks.onSaveInstanceState(outState);
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
2038 @Override
2039 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002041
Winson Chunge7a03942011-08-05 15:05:12 -07002042 // Remove all pending runnables
2043 mHandler.removeMessages(ADVANCE_MSG);
2044 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002045 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002046
Winson Chungcd2b0142011-06-08 16:02:26 -07002047 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002048 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002049
2050 // It's possible to receive onDestroy after a new Launcher activity has
2051 // been created. In this case, don't interfere with the new Launcher.
2052 if (mModel.isCurrentCallbacks(this)) {
2053 mModel.stopLoader();
2054 app.setLauncher(null);
2055 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002058 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002060 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062 mAppWidgetHost = null;
2063
2064 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065
2066 TextKeyListener.getInstance().release();
2067
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002068 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002069 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002070
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002071 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002072 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002073 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002074 mWorkspace = null;
2075 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002076
Michael Jurka2ecf9952012-06-18 12:52:28 -07002077 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002078
2079 if (mLauncherCallbacks != null) {
2080 mLauncherCallbacks.onDestroy();
2081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
2083
Adam Cohena9cf38f2011-05-02 15:36:58 -07002084 public DragController getDragController() {
2085 return mDragController;
2086 }
2087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 @Override
2089 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002090 onStartForResult(requestCode);
2091 super.startActivityForResult(intent, requestCode);
2092 }
2093
2094 @Override
2095 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2096 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2097 onStartForResult(requestCode);
2098 try {
2099 super.startIntentSenderForResult(intent, requestCode,
2100 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2101 } catch (IntentSender.SendIntentException e) {
2102 throw new ActivityNotFoundException();
2103 }
2104 }
2105
2106 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002107 if (requestCode >= 0) {
2108 setWaitingForResult(true);
2109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111
Winson Chung70d72102011-08-12 11:24:35 -07002112 /**
2113 * Indicates that we want global search for this activity by setting the globalSearch
2114 * argument for {@link #startSearch} to true.
2115 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002117 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002119
Karl Rosaen138a0412009-04-23 19:00:21 -07002120 if (initialQuery == null) {
2121 // Use any text typed in the launcher as the initial query
2122 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 if (appSearchData == null) {
2125 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002126 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 }
Winson Chungadf0c182012-08-23 14:59:07 -07002128 Rect sourceBounds = new Rect();
2129 if (mSearchDropTargetBar != null) {
2130 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2131 }
Romain Guycbb89e42009-06-08 15:52:54 -07002132
Ian Parkinson047036e2014-09-01 15:40:53 +01002133 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002134 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002135 if (clearTextImmediately) {
2136 clearTypedText();
2137 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002138
2139 // We need to show the workspace after starting the search
2140 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 }
2142
Ian Parkinson047036e2014-09-01 15:40:53 +01002143 /**
2144 * Start a text search.
2145 *
2146 * @return {@code true} if the search will start immediately, so any further keypresses
2147 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2148 * to buffer keypresses.
2149 */
2150 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002151 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002152 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2153 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2154 sourceBounds);
2155 }
2156
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002158 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002159 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002160 }
2161
2162 /**
2163 * Starts the global search activity. This code is a copied from SearchManager
2164 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002165 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002166 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002167 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002168 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2169 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2170 if (globalSearchActivity == null) {
2171 Log.w(TAG, "No global search activity found.");
2172 return;
2173 }
2174 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2175 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2176 intent.setComponent(globalSearchActivity);
2177 // Make sure that we have a Bundle to put source in
2178 if (appSearchData == null) {
2179 appSearchData = new Bundle();
2180 } else {
2181 appSearchData = new Bundle(appSearchData);
2182 }
Adam Cohen9211d422014-10-07 18:14:53 -07002183 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002184 if (!appSearchData.containsKey("source")) {
2185 appSearchData.putString("source", getPackageName());
2186 }
2187 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2188 if (!TextUtils.isEmpty(initialQuery)) {
2189 intent.putExtra(SearchManager.QUERY, initialQuery);
2190 }
2191 if (selectInitialQuery) {
2192 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2193 }
2194 intent.setSourceBounds(sourceBounds);
2195 try {
2196 startActivity(intent);
2197 } catch (ActivityNotFoundException ex) {
2198 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
2201
Yura4f93ec62014-02-04 14:15:21 +00002202 public boolean isOnCustomContent() {
2203 return mWorkspace.isOnOrMovingToCustomContent();
2204 }
2205
Winson Chung70d72102011-08-12 11:24:35 -07002206 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002207 public boolean onPrepareOptionsMenu(Menu menu) {
2208 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002209 if (!isOnCustomContent()) {
2210 // Close any open folders
2211 closeFolder();
2212 // Stop resizing any widgets
2213 mWorkspace.exitWidgetResizeMode();
2214 if (!mWorkspace.isInOverviewMode()) {
2215 // Show the overview mode
2216 showOverviewMode(true);
2217 } else {
2218 showWorkspace(true);
2219 }
Winson Chunge0298742014-01-17 12:03:00 -08002220 }
Adam Cohen9211d422014-10-07 18:14:53 -07002221 if (mLauncherCallbacks != null) {
2222 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2223 }
2224
Michael Jurkab94f3f82013-09-11 18:08:54 +02002225 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002228 @Override
2229 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002230 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002231 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002232 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002233 }
2234
Joe Onorato9c1289c2009-08-17 11:03:03 -04002235 public boolean isWorkspaceLocked() {
2236 return mWorkspaceLoading || mWaitingForResult;
2237 }
2238
Adam Cohen517a7f52014-03-01 12:12:59 -08002239 public boolean isWorkspaceLoading() {
2240 return mWorkspaceLoading;
2241 }
2242
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002243 private void setWorkspaceLoading(boolean value) {
2244 boolean isLocked = isWorkspaceLocked();
2245 mWorkspaceLoading = value;
2246 if (isLocked != isWorkspaceLocked()) {
2247 onWorkspaceLockedChanged();
2248 }
2249 }
2250
2251 private void setWaitingForResult(boolean value) {
2252 boolean isLocked = isWorkspaceLocked();
2253 mWaitingForResult = value;
2254 if (isLocked != isWorkspaceLocked()) {
2255 onWorkspaceLockedChanged();
2256 }
2257 }
2258
Adam Cohen9211d422014-10-07 18:14:53 -07002259 protected void onWorkspaceLockedChanged() {
2260 if (mLauncherCallbacks != null) {
2261 mLauncherCallbacks.onWorkspaceLockedChanged();
2262 }
2263 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002264
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2269 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002270 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002273
Sunny Goyale6b63a32015-01-16 16:14:29 -08002274 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002275 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002276 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2277 }
2278
Sunny Goyale6b63a32015-01-16 16:14:29 -08002279 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002280 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2281 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002282 if (appWidgetInfo.configure != null) {
2283 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002284 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002285
Bjorn Bringert7984c942009-12-09 15:38:25 +00002286 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002287 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2288 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002291 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002292 Runnable onComplete = new Runnable() {
2293 @Override
2294 public void run() {
2295 // Exit spring loaded mode if necessary after adding the widget
2296 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2297 null);
2298 }
2299 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002300 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002301 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002302 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304 }
Romain Guycbb89e42009-06-08 15:52:54 -07002305
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002306 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002307 // Close any folders that may be open.
2308 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002309 mWorkspace.moveToCustomContentScreen(animate);
2310 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002311
2312 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2313 int[] cell, int spanX, int spanY) {
2314 switch (info.itemType) {
2315 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2316 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2317 int span[] = new int[2];
2318 span[0] = spanX;
2319 span[1] = spanY;
2320 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2321 container, screenId, cell, span);
2322 break;
2323 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2324 processShortcutFromDrop(info.componentName, container, screenId, cell);
2325 break;
2326 default:
2327 throw new IllegalStateException("Unknown item type: " + info.itemType);
2328 }
2329 }
2330
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 /**
2332 * Process a shortcut drop.
2333 *
2334 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002335 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002338 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2339 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002340 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002343 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344
2345 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 mPendingAddInfo.cellX = cell[0];
2347 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002349
2350 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2351 createShortcutIntent.setComponent(componentName);
2352 processShortcut(createShortcutIntent);
2353 }
2354
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 /**
2356 * Process a widget drop.
2357 *
2358 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002359 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002362 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2363 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002364 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002366 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002367 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002368 mPendingAddInfo.minSpanX = info.minSpanX;
2369 mPendingAddInfo.minSpanY = info.minSpanY;
2370
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002372 mPendingAddInfo.cellX = cell[0];
2373 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002374 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002375 if (span != null) {
2376 mPendingAddInfo.spanX = span[0];
2377 mPendingAddInfo.spanY = span[1];
2378 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379
Adam Cohened66b2b2012-01-23 17:28:51 -08002380 AppWidgetHostView hostView = info.boundWidget;
2381 int appWidgetId;
2382 if (hostView != null) {
2383 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002384 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002385 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 // In this case, we either need to start an activity to get permission to bind
2387 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002388 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002389 Bundle options = info.bindOptions;
2390
Sunny Goyalffe83f12014-08-14 17:39:34 -07002391 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2392 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002393 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002394 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002396 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002397 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2398 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2399 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002400 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2401 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002402 // TODO: we need to make sure that this accounts for the options bundle.
2403 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002404 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2405 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002406 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002407 }
2408
Joe Onoratodeb98af2010-02-19 14:59:39 -08002409 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002410 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
2412
Winson Chung24ab2f12010-09-16 14:10:47 -07002413 void processWallpaper(Intent intent) {
2414 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2415 }
2416
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002419 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 folderInfo.title = getText(R.string.folder_name);
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002423 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2424 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002425 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426
2427 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 FolderIcon newFolder =
2429 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002430 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002432 // Force measure the new folder icon
2433 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2434 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002435 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 }
Romain Guycbb89e42009-06-08 15:52:54 -07002437
Joe Onorato9c1289c2009-08-17 11:03:03 -04002438 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002439 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002440 }
2441
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002442 /**
2443 * Registers various content observers. The current implementation registers
2444 * only a favorites observer to keep track of the favorites applications.
2445 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002446 private void registerContentObservers() {
2447 ContentResolver resolver = getContentResolver();
2448 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2449 true, mWidgetObserver);
2450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 @Override
2453 public boolean dispatchKeyEvent(KeyEvent event) {
2454 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2455 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002457 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002458 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002459 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002460 dumpState();
2461 return true;
2462 }
2463 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002464 }
2465 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2466 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002467 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 return true;
2469 }
2470 }
2471
2472 return super.dispatchKeyEvent(event);
2473 }
2474
Joe Onorato88ec0992009-11-19 13:16:06 -08002475 @Override
2476 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002477 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2478 return;
2479 }
2480
Winson Chung3d9490a2015-03-24 17:38:42 -07002481 LauncherAccessibilityDelegate delegate =
2482 LauncherAppState.getInstance().getAccessibilityDelegate();
2483 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002484 return;
2485 }
2486
Winson Chungb745afb2015-03-02 11:51:23 -08002487 if (isAppsViewVisible()) {
2488 showWorkspace(true);
2489 } else if (isWidgetsViewVisible()) {
2490 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002492 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002493 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002494 Folder openFolder = mWorkspace.getOpenFolder();
2495 if (openFolder.isEditingName()) {
2496 openFolder.dismissEditingName();
2497 } else {
2498 closeFolder();
2499 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002500 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002501 mWorkspace.exitWidgetResizeMode();
2502
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002503 // Back button is a no-op here, but give at least some feedback for the button press
2504 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002505 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002509 * Re-listen when widgets are reset.
2510 */
Adam Cohen091440a2015-03-18 14:16:05 -07002511 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002512 if (mAppWidgetHost != null) {
2513 mAppWidgetHost.startListening();
2514 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002515 }
2516
2517 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 * Launches the intent referred by the clicked shortcut.
2519 *
2520 * @param v The view representing the clicked shortcut.
2521 */
2522 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002523 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2524 // view has detached (it's possible for this to happen if the view is removed mid touch).
2525 if (v.getWindowToken() == null) {
2526 return;
2527 }
2528
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002529 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002530 return;
2531 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002532
Adam Cohen1697b792013-09-17 19:08:21 -07002533 if (v instanceof Workspace) {
2534 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002535 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002536 }
2537 return;
2538 }
2539
2540 if (v instanceof CellLayout) {
2541 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002542 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002543 }
2544 }
2545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002547 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002550 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002552 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002553 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002554 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002555 } else if (tag instanceof AppInfo) {
2556 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002557 } else if (tag instanceof LauncherAppWidgetInfo) {
2558 if (v instanceof PendingAppWidgetHostView) {
2559 onClickPendingWidget((PendingAppWidgetHostView) v);
2560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 }
2562 }
2563
Sunny Goyal508da152014-08-14 10:53:27 -07002564 public void onClickPagedViewIcon(View v) {
2565 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002566 if (mLauncherCallbacks != null) {
2567 mLauncherCallbacks.onClickPagedViewIcon(v);
2568 }
Sunny Goyal508da152014-08-14 10:53:27 -07002569 }
2570
Michael Jurka0e260592010-06-30 17:07:39 -07002571 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002572 return false;
2573 }
2574
Michael Jurkaaf442092010-06-10 17:01:57 -07002575 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002576 * Event handler for the app widget view which has not fully restored.
2577 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002579 if (mIsSafeModeEnabled) {
2580 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2581 return;
2582 }
2583
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002585 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002586 int widgetId = info.appWidgetId;
2587 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2588 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002589 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2590 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002591 mPendingAddInfo.copyFrom(info);
2592 mPendingAddWidgetId = widgetId;
2593
Sunny Goyalffe83f12014-08-14 17:39:34 -07002594 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2595 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002596 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002597 } else if (info.installProgress < 0) {
2598 // The install has not been queued
2599 final String packageName = info.providerName.getPackageName();
2600 showBrokenAppInstallDialog(packageName,
2601 new DialogInterface.OnClickListener() {
2602 public void onClick(DialogInterface dialog, int id) {
2603 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2604 }
2605 });
2606 } else {
2607 // Download has started.
2608 final String packageName = info.providerName.getPackageName();
2609 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002610 }
2611 }
2612
2613 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002614 * Event handler for the "grid" button that appears on the home screen, which
2615 * enters all apps mode.
2616 *
2617 * @param v The view that was clicked.
2618 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 protected void onClickAllAppsButton(View v) {
2620 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002621 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 showWorkspace(true);
2623 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002624 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 }
2626 }
2627
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 private void showBrokenAppInstallDialog(final String packageName,
2629 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002630 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002631 .setTitle(R.string.abandoned_promises_title)
2632 .setMessage(R.string.abandoned_promise_explanation)
2633 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2634 .setNeutralButton(R.string.abandoned_clean_this,
2635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
2637 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2638 mWorkspace.removeAbandonedPromise(packageName, user);
2639 }
2640 })
2641 .create().show();
2642 return;
2643 }
2644
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 /**
2646 * Event handler for an app shortcut click.
2647 *
2648 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2649 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2652 Object tag = v.getTag();
2653 if (!(tag instanceof ShortcutInfo)) {
2654 throw new IllegalArgumentException("Input must be a Shortcut");
2655 }
2656
2657 // Open shortcut
2658 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002659
2660 if (shortcut.isDisabled != 0) {
2661 int error = R.string.activity_not_available;
2662 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2663 error = R.string.safemode_shortcut_error;
2664 }
2665 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2666 return;
2667 }
2668
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669 final Intent intent = shortcut.intent;
2670
2671 // Check for special shortcuts
2672 if (intent.getComponent() != null) {
2673 final String shortcutClass = intent.getComponent().getClassName();
2674
2675 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2676 MemoryDumpActivity.startDump(this);
2677 return;
2678 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2679 toggleShowWeightWatcher();
2680 return;
2681 }
2682 }
2683
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002685 if ((v instanceof BubbleTextView)
2686 && shortcut.isPromise()
2687 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002688 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002689 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 new DialogInterface.OnClickListener() {
2691 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002692 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 return;
2696 }
2697
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002699 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002700
2701 if (mLauncherCallbacks != null) {
2702 mLauncherCallbacks.onClickAppShortcut(v);
2703 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002704 }
2705
Adam Cohen091440a2015-03-18 14:16:05 -07002706 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002707 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002708 final ShortcutInfo shortcut;
2709 final Intent intent;
2710 if (tag instanceof ShortcutInfo) {
2711 shortcut = (ShortcutInfo) tag;
2712 intent = shortcut.intent;
2713 int[] pos = new int[2];
2714 v.getLocationOnScreen(pos);
2715 intent.setSourceBounds(new Rect(pos[0], pos[1],
2716 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002717
Sunny Goyal508da152014-08-14 10:53:27 -07002718 } else if (tag instanceof AppInfo) {
2719 shortcut = null;
2720 intent = ((AppInfo) tag).intent;
2721 } else {
2722 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2723 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724
2725 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 mStats.recordLaunch(intent, shortcut);
2727
2728 if (success && v instanceof BubbleTextView) {
2729 mWaitingForResume = (BubbleTextView) v;
2730 mWaitingForResume.setStayPressed(true);
2731 }
2732 }
2733
2734 /**
2735 * Event handler for a folder icon click.
2736 *
2737 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2738 */
2739 protected void onClickFolderIcon(View v) {
2740 if (LOGD) Log.d(TAG, "onClickFolder");
2741 if (!(v instanceof FolderIcon)){
2742 throw new IllegalArgumentException("Input must be a FolderIcon");
2743 }
2744
2745 FolderIcon folderIcon = (FolderIcon) v;
2746 final FolderInfo info = folderIcon.getFolderInfo();
2747 Folder openFolder = mWorkspace.getFolderForTag(info);
2748
2749 // If the folder info reports that the associated folder is open, then verify that
2750 // it is actually opened. There have been a few instances where this gets out of sync.
2751 if (info.opened && openFolder == null) {
2752 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2753 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2754 info.opened = false;
2755 }
2756
2757 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2758 // Close any open folder
2759 closeFolder();
2760 // Open the requested folder
2761 openFolder(folderIcon);
2762 } else {
2763 // Find the open folder...
2764 int folderScreen;
2765 if (openFolder != null) {
2766 folderScreen = mWorkspace.getPageForView(openFolder);
2767 // .. and close it
2768 closeFolder(openFolder);
2769 if (folderScreen != mWorkspace.getCurrentPage()) {
2770 // Close any folder open on the current screen
2771 closeFolder();
2772 // Pull the folder onto this screen
2773 openFolder(folderIcon);
2774 }
2775 }
2776 }
Adam Cohen9211d422014-10-07 18:14:53 -07002777
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onClickFolderIcon(v);
2780 }
Michael Jurka5130e402011-10-13 04:55:35 -07002781 }
2782
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002783 /**
2784 * Event handler for the (Add) Widgets button that appears after a long press
2785 * on the home screen.
2786 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002787 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002788 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002789 if (mIsSafeModeEnabled) {
2790 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2791 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002792 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002793 if (mLauncherCallbacks != null) {
2794 mLauncherCallbacks.onClickAddWidgetButton(view);
2795 }
Adam Cohen9211d422014-10-07 18:14:53 -07002796 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002797 }
2798
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002799 /**
2800 * Event handler for the wallpaper picker button that appears after a long press
2801 * on the home screen.
2802 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002803 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002805 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2806 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002807
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onClickWallpaperPicker(v);
2810 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 }
2812
2813 /**
2814 * Event handler for a click on the settings button that appears after a long press
2815 * on the home screen.
2816 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002817 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onClickSettingsButton(v);
2821 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002822 }
2823
Michael Jurka5130e402011-10-13 04:55:35 -07002824 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002825 // Provide the same haptic feedback that the system offers for virtual keys.
2826 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002827 }
2828
Adam Cohen61f560d2013-09-30 15:58:20 -07002829 public void performHapticFeedbackOnTouchDown(View v) {
2830 // Provide the same haptic feedback that the system offers for virtual keys.
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2832 }
2833
2834 public View.OnTouchListener getHapticFeedbackTouchListener() {
2835 if (mHapticFeedbackTouchListener == null) {
2836 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2837 @Override
2838 public boolean onTouch(View v, MotionEvent event) {
2839 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2840 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2841 }
2842 return false;
2843 }
2844 };
2845 }
2846 return mHapticFeedbackTouchListener;
2847 }
2848
Adam Cohen9211d422014-10-07 18:14:53 -07002849 public void onDragStarted(View view) {
2850 if (isOnCustomContent()) {
2851 // Custom content screen doesn't participate in drag and drop. If on custom
2852 // content screen, move to default.
2853 moveWorkspaceToDefaultScreen();
2854 }
2855
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onDragStarted(view);
2858 }
2859 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002860
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861 /**
2862 * Called when the user stops interacting with the launcher.
2863 * This implies that the user is now on the homescreen and is not doing housekeeping.
2864 */
Adam Cohen9211d422014-10-07 18:14:53 -07002865 protected void onInteractionEnd() {
2866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onInteractionEnd();
2868 }
2869 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002870
2871 /**
2872 * Called when the user starts interacting with the launcher.
2873 * The possible interactions are:
2874 * - open all apps
2875 * - reorder an app shortcut, or a widget
2876 * - open the overview mode.
2877 * This is a good time to stop doing things that only make sense
2878 * when the user is on the homescreen and not doing housekeeping.
2879 */
Adam Cohen9211d422014-10-07 18:14:53 -07002880 protected void onInteractionBegin() {
2881 if (mLauncherCallbacks != null) {
2882 mLauncherCallbacks.onInteractionBegin();
2883 }
2884 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002885
Winson Chungcd99cd32015-04-29 11:03:24 -07002886 /** Updates the interaction state. */
2887 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002888 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002889 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002890 boolean fromStateWithOverlay;
2891 boolean toStateWithOverlay;
2892 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2893 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2894 toStateWithOverlay = toState != Workspace.State.NORMAL;
2895 } else {
2896 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2897 fromState != Workspace.State.NORMAL_HIDDEN;
2898 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2899 toState != Workspace.State.NORMAL_HIDDEN;
2900 }
2901 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002902 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002903 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002904 onInteractionEnd();
2905 }
2906 }
2907
Kenny Guyf07af7b2014-07-31 11:39:16 +01002908 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002909 try {
2910 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002911 launcherApps.showAppDetailsForProfile(componentName, user);
2912 } catch (SecurityException e) {
2913 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2914 Log.e(TAG, "Launcher does not have permission to launch settings");
2915 } catch (ActivityNotFoundException e) {
2916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2917 Log.e(TAG, "Unable to launch settings");
2918 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002919 }
2920
Michael Jurka1e2f4652013-07-08 18:03:46 -07002921 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002922 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2923 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002924 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002925 // System applications cannot be installed. For now, show a toast explaining that.
2926 // We may give them the option of disabling apps this way.
2927 int messageId = R.string.uninstall_system_app_text;
2928 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002929 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002930 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002931 String packageName = componentName.getPackageName();
2932 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002933 Intent intent = new Intent(
2934 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002935 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2936 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002937 if (user != null) {
2938 user.addToIntent(intent, Intent.EXTRA_USER);
2939 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002940 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002941 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002942 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002943 }
2944
Michael Jurka86a720e2012-05-09 11:23:23 -07002945 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002946 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002948 // Only launch using the new animation if the shortcut has not opted out (this is a
2949 // private contract between launcher and may be ignored in the future).
2950 boolean useLaunchAnimation = (v != null) &&
2951 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002952 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2953 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002954
Kenny Guy1317e2d2014-05-08 18:52:50 +01002955 UserHandleCompat user = null;
2956 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2957 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2958 user = userManager.getUserForSerialNumber(serialNumber);
2959 }
2960
2961 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002962 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002963 ActivityOptions opts = null;
2964 if (sClipRevealMethod != null) {
2965 // TODO: call method directly when Launcher3 can depend on M APIs
2966 int left = 0, top = 0;
2967 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2968 if (v instanceof TextView) {
2969 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002970 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2971 if (icon != null) {
2972 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002973 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002974 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002975 width = bounds.width();
2976 height = bounds.height();
2977 }
2978 }
2979 try {
2980 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2981 left, top, width, height);
2982 } catch (IllegalAccessException e) {
2983 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2984 sClipRevealMethod = null;
2985 } catch (InvocationTargetException e) {
2986 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2987 sClipRevealMethod = null;
2988 }
2989 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002990 if (opts == null && !Utilities.isLmpOrAbove()) {
2991 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002992 v.getMeasuredWidth(), v.getMeasuredHeight());
2993 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002994 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002995 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002996
2997 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2998 // Could be launching some bookkeeping activity
2999 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003000 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003001 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003002 launcherApps.startActivityForProfile(intent.getComponent(), user,
3003 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003004 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003005 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 } catch (SecurityException e) {
3007 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003008 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003010 "or use the exported attribute for this activity. "
3011 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003013 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003015
Michael Jurka86a720e2012-05-09 11:23:23 -07003016 boolean startActivitySafely(View v, Intent intent, Object tag) {
3017 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003018 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3019 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3020 return false;
3021 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003022 try {
3023 success = startActivity(v, intent, tag);
3024 } catch (ActivityNotFoundException e) {
3025 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3026 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3027 }
3028 return success;
3029 }
3030
Adam Cohen268c4752012-06-06 17:47:33 -07003031 /**
3032 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3033 * in the DragLayer in the exact absolute location of the original FolderIcon.
3034 */
3035 private void copyFolderIconToImage(FolderIcon fi) {
3036 final int width = fi.getMeasuredWidth();
3037 final int height = fi.getMeasuredHeight();
3038
3039 // Lazy load ImageView, Bitmap and Canvas
3040 if (mFolderIconImageView == null) {
3041 mFolderIconImageView = new ImageView(this);
3042 }
3043 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3044 mFolderIconBitmap.getHeight() != height) {
3045 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3046 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3047 }
3048
3049 DragLayer.LayoutParams lp;
3050 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3051 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3052 } else {
3053 lp = new DragLayer.LayoutParams(width, height);
3054 }
3055
Adam Cohen307fe232012-08-16 17:55:58 -07003056 // The layout from which the folder is being opened may be scaled, adjust the starting
3057 // view size by this scale factor.
3058 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003059 lp.customPosition = true;
3060 lp.x = mRectForFolderAnimation.left;
3061 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003062 lp.width = (int) (scale * width);
3063 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003064
3065 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3066 fi.draw(mFolderIconCanvas);
3067 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003068 if (fi.getFolder() != null) {
3069 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3070 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003071 }
Adam Cohen268c4752012-06-06 17:47:33 -07003072 // Just in case this image view is still in the drag layer from a previous animation,
3073 // we remove it and re-add it.
3074 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3075 mDragLayer.removeView(mFolderIconImageView);
3076 }
3077 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003078 if (fi.getFolder() != null) {
3079 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003080 }
Adam Cohen268c4752012-06-06 17:47:33 -07003081 }
3082
Adam Cohen2801caf2011-05-13 20:57:39 -07003083 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003084 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003085 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3086 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3087 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3088
Adam Cohenc51934b2011-07-26 21:07:43 -07003089 FolderInfo info = (FolderInfo) fi.getTag();
3090 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3091 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003092 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3093 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003094 }
3095
Adam Cohen268c4752012-06-06 17:47:33 -07003096 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3097 copyFolderIconToImage(fi);
3098 fi.setVisibility(View.INVISIBLE);
3099
Michael Jurka2ecf9952012-06-18 12:52:28 -07003100 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003101 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003102 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003103 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3104 }
3105 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003106 oa.start();
3107 }
3108
Adam Cohen268c4752012-06-06 17:47:33 -07003109 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003110 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003111 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3112 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3113 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3114
Adam Cohen268c4752012-06-06 17:47:33 -07003115 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003116
Adam Cohen268c4752012-06-06 17:47:33 -07003117 // We remove and re-draw the FolderIcon in-case it has changed
3118 mDragLayer.removeView(mFolderIconImageView);
3119 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003120 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003121 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003122 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003123 oa.addListener(new AnimatorListenerAdapter() {
3124 @Override
3125 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003126 if (cl != null) {
3127 cl.clearFolderLeaveBehind();
3128 // Remove the ImageView copy of the FolderIcon and make the original visible.
3129 mDragLayer.removeView(mFolderIconImageView);
3130 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003131 }
3132 }
3133 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003134 oa.start();
3135 }
3136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003138 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 * is animated relative to the specified View. If the View is null, no animation
3140 * is played.
3141 *
3142 * @param folderInfo The FolderInfo describing the folder to open.
3143 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003144 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003145 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003146 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3147 if (openFolder != null && openFolder != folder) {
3148 // Close any open folder before opening a folder.
3149 closeFolder();
3150 }
3151
Adam Cohena9cf38f2011-05-02 15:36:58 -07003152 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153
Adam Cohena9cf38f2011-05-02 15:36:58 -07003154 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155
Sunny Goyalf4066152015-04-15 09:42:19 -07003156 // While the folder is open, the position of the icon cannot change.
3157 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3158
Adam Cohen4554ee12011-08-03 16:13:21 -07003159 // Just verify that the folder hasn't already been added to the DragLayer.
3160 // There was a one-off crash where the folder had a parent already.
3161 if (folder.getParent() == null) {
3162 mDragLayer.addView(folder);
3163 mDragController.addDropTarget((DropTarget) folder);
3164 } else {
3165 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3166 folder.getParent() + ").");
3167 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003168 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003169 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003170
3171 // Notify the accessibility manager that this folder "window" has appeared and occluded
3172 // the workspace items
3173 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3174 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003175 }
3176
3177 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003178 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003179 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003180 if (folder.isEditingName()) {
3181 folder.dismissEditingName();
3182 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003183 closeFolder(folder);
3184 }
3185 }
3186
3187 void closeFolder(Folder folder) {
3188 folder.getInfo().opened = false;
3189
3190 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3191 if (parent != null) {
3192 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3193 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003194 if (fi != null) {
3195 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3196 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003197 }
3198 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003199
3200 // Notify the accessibility manager that this folder "window" has disappeard and no
3201 // longer occludeds the workspace items
3202 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
3204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003206 if (!isDraggingEnabled()) return false;
3207 if (isWorkspaceLocked()) return false;
3208 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209
Adam Cohen1697b792013-09-17 19:08:21 -07003210 if (v instanceof Workspace) {
3211 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003212 if (!mWorkspace.isTouchActive()) {
3213 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003214 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3215 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3216 return true;
3217 } else {
3218 return false;
3219 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003220 } else {
3221 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003222 }
Adam Cohen1697b792013-09-17 19:08:21 -07003223 }
3224
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003225 CellLayout.CellInfo longClickCellInfo = null;
3226 View itemUnderLongClick = null;
3227 if (v.getTag() instanceof ItemInfo) {
3228 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003229 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003230 itemUnderLongClick = longClickCellInfo.cell;
3231 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 }
3233
Winson Chung3d503fb2011-07-13 17:25:49 -07003234 // The hotseat touch handling does not go through Workspace, and we always allow long press
3235 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003236 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003237 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003238 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003239 // User long pressed on empty space
3240 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3241 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003242 if (mWorkspace.isInOverviewMode()) {
3243 mWorkspace.startReordering(v);
3244 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003245 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003248 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3249 mHotseat.getOrderInHotseat(
3250 longClickCellInfo.cellX,
3251 longClickCellInfo.cellY));
3252 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003254 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 }
3256 }
3257 }
3258 return true;
3259 }
3260
Winson Chung3d503fb2011-07-13 17:25:49 -07003261 boolean isHotseatLayout(View layout) {
3262 return mHotseat != null && layout != null &&
3263 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3264 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003265
Winson Chung3d503fb2011-07-13 17:25:49 -07003266 /**
3267 * Returns the CellLayout of the specified container at the specified screen.
3268 */
Sunny Goyal92820592015-03-02 11:31:35 -08003269 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003270 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3271 if (mHotseat != null) {
3272 return mHotseat.getLayout();
3273 } else {
3274 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003275 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003276 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003277 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003278 }
3279 }
3280
Winson Chungb745afb2015-03-02 11:51:23 -08003281 /**
3282 * For overridden classes.
3283 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003284 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003285 return isAppsViewVisible();
3286 }
3287
3288 public boolean isAppsViewVisible() {
3289 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3290 }
3291
3292 public boolean isWidgetsViewVisible() {
3293 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003294 }
3295
Craig Mautner360310b2012-10-26 15:13:08 -07003296 private void setWorkspaceBackground(boolean workspace) {
3297 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003298 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003299 }
3300
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003301 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003302 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3303 int curflags = getWindow().getAttributes().flags
3304 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3305 if (wpflags != curflags) {
3306 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3307 }
Craig Mautner360310b2012-10-26 15:13:08 -07003308 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003309 }
3310
Michael Jurkae326f182011-11-21 14:05:46 -08003311 @Override
3312 public void onTrimMemory(int level) {
3313 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003314 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3315 // The widget preview db can result in holding onto over
3316 // 3MB of memory for caching which isn't necessary.
3317 SQLiteDatabase.releaseMemory();
3318
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003319 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003321 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003322 if (mLauncherCallbacks != null) {
3323 mLauncherCallbacks.onTrimMemory(level);
3324 }
Michael Jurkae326f182011-11-21 14:05:46 -08003325 }
3326
Winson Chungb745afb2015-03-02 11:51:23 -08003327 @Override
3328 public void onStateTransitionHideSearchBar() {
3329 // Hide the search bar
3330 if (mSearchDropTargetBar != null) {
3331 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3332 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003333 }
3334
Winson Chungb745afb2015-03-02 11:51:23 -08003335 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003336 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3337 true);
Adam Cohened307df2013-10-02 09:37:31 -07003338 }
3339
Winson Chungdc61c4d2015-04-20 18:26:57 -07003340 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3341 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3342 onCompleteRunnable, true);
3343 }
3344
3345 protected void showWorkspace(int snapToPage, boolean animated) {
3346 showWorkspace(snapToPage, animated, null, true);
3347 }
3348
3349 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3350 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003351 boolean changed = mState != State.WORKSPACE ||
3352 mWorkspace.getState() != Workspace.State.NORMAL;
3353 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003354 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003356 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003357 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358
Winson Chungc7d2b602012-05-16 17:02:20 -07003359 // Show the search bar (only animate if we were showing the drop target bar in spring
3360 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003361 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003362 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003363 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003364
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365 // Set focus to the AppsCustomize button
3366 if (mAllAppsButton != null) {
3367 mAllAppsButton.requestFocus();
3368 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003369 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003370
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003371 // Change the state *after* we've called all the transition code
3372 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003373
Winson Chung5fb63472011-02-02 17:03:37 -08003374 // Resume the auto-advance of widgets
3375 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003376 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003377
Winson Chung0f785722015-04-08 10:27:49 -07003378 if (changed) {
3379 // Send an accessibility event to announce the context change
3380 getWindow().getDecorView()
3381 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003382 if (notifyLauncherCallbacks) {
3383 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003384 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003385 mLauncherCallbacks.onAllAppsHidden();
3386 }
3387 }
Winson Chung0f785722015-04-08 10:27:49 -07003388 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003389 }
3390
Adam Cohened307df2013-10-02 09:37:31 -07003391 void showOverviewMode(boolean animated) {
3392 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003393 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003394 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3395 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003396 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003397 }
3398
Winson Chungb745afb2015-03-02 11:51:23 -08003399 /**
3400 * Shows the apps view.
3401 */
3402 void showAppsView(boolean animated, boolean resetListToTop) {
3403 if (resetListToTop) {
3404 mAppsView.scrollToTop();
3405 }
3406 showAppsOrWidgets(animated, State.APPS);
3407 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408
Winson Chungb745afb2015-03-02 11:51:23 -08003409 /**
3410 * Shows the widgets view.
3411 */
3412 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003413 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003414 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003415 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003416 }
Winson Chungb745afb2015-03-02 11:51:23 -08003417 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418
3419 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003420 @Override
3421 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003422 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003423 }
3424 });
Winson Chungb745afb2015-03-02 11:51:23 -08003425 }
3426
3427 /**
3428 * Sets up the transition to show the apps/widgets view.
3429 */
3430 private void showAppsOrWidgets(boolean animated, State toState) {
3431 if (mState != State.WORKSPACE) return;
3432 if (toState != State.APPS && toState != State.WIDGETS) return;
3433
3434 if (toState == State.APPS) {
3435 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003436 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003437 mLauncherCallbacks.onAllAppsShown();
3438 }
Winson Chungb745afb2015-03-02 11:51:23 -08003439 } else {
3440 mStateTransitionAnimation.startAnimationToWidgets(animated);
3441 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003442
Michael Jurkab3e22d92011-10-31 15:58:33 -07003443 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003444 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003445
3446 // Pause the auto-advance of widgets until we are out of AllApps
3447 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003448 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003449 closeFolder();
3450
3451 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003452 getWindow().getDecorView()
3453 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 }
3455
Winson Chungcd99cd32015-04-29 11:03:24 -07003456 /**
3457 * Updates the workspace and interaction state on state change, and return the animation to this
3458 * new state.
3459 */
3460 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3461 boolean animated, HashMap<View, Integer> layerViews) {
3462 Workspace.State fromState = mWorkspace.getState();
3463 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3464 updateInteraction(fromState, toState);
3465 return anim;
3466 }
3467
Hyunyoung Song3f471442015-04-08 19:01:34 -07003468 public void enterSpringLoadedDragMode() {
3469 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3470 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003471 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3472 mState == State.WIDGETS_SPRING_LOADED) {
3473 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003474 }
Winson Chungb745afb2015-03-02 11:51:23 -08003475
3476 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003477 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3478 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003479 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003480 }
Adam Cohen7777d962011-08-18 18:58:38 -07003481
Hyunyoung Song3f471442015-04-08 19:01:34 -07003482 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003483 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003484 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003485
Winson Chungcd99cd32015-04-29 11:03:24 -07003486 if (successfulDrop) {
3487 // We need to trigger all apps hidden to notify search to update itself before the
3488 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003489 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003490 mLauncherCallbacks.onAllAppsHidden();
3491 }
3492 }
3493
Winson Chunge7a03942011-08-05 15:05:12 -07003494 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 @Override
3496 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003497 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003498 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3499 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003500 // Before we show workspace, hide all apps again because
3501 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3502 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003503 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003504 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003505 } else {
3506 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003507 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003508 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003509 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003510 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003511
Michael Jurkad3ef3062010-11-23 16:23:58 -08003512 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003513 if (mState == State.APPS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003514 showAppsView(true, false);
Winson Chungb745afb2015-03-02 11:51:23 -08003515 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003516 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003518 }
3519
Michael Jurkab3e22d92011-10-31 15:58:33 -07003520 void lockAllApps() {
3521 // TODO
3522 }
3523
3524 void unlockAllApps() {
3525 // TODO
3526 }
3527
Sunny Goyal594d76d2014-11-06 10:12:54 -08003528 protected void disableVoiceButtonProxy(boolean disable) {
3529 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003530 }
3531
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003532 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003533 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3534 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003535 }
3536
Adam Cohen24ce0b32014-01-14 16:18:14 -08003537 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003538 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3539 if (searchProvider == null) {
3540 return null;
3541 }
3542
3543 Bundle opts = new Bundle();
3544 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003545 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003546
3547 SharedPreferences sp = getSharedPreferences(
3548 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3549 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003550 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003551 if (!searchProvider.provider.flattenToString().equals(
3552 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003553 || (widgetInfo == null)
3554 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 // A valid widget is not already bound.
3556 if (widgetId > -1) {
3557 mAppWidgetHost.deleteAppWidgetId(widgetId);
3558 widgetId = -1;
3559 }
3560
3561 // Try to bind a new widget
3562 widgetId = mAppWidgetHost.allocateAppWidgetId();
3563
3564 if (!AppWidgetManagerCompat.getInstance(this)
3565 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3566 mAppWidgetHost.deleteAppWidgetId(widgetId);
3567 widgetId = -1;
3568 }
3569
3570 sp.edit()
3571 .putInt(QSB_WIDGET_ID, widgetId)
3572 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3573 .commit();
3574 }
3575
3576 if (widgetId != -1) {
3577 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3578 mQsb.updateAppWidgetOptions(opts);
3579 mQsb.setPadding(0, 0, 0, 0);
3580 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003581 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003583 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003584 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003585 }
3586
Sunny Goyal22235bc2015-04-03 09:23:43 -07003587 private void reinflateQSBIfNecessary() {
3588 if (mQsb instanceof LauncherAppWidgetHostView &&
3589 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3590 mSearchDropTargetBar.removeView(mQsb);
3591 mQsb = null;
3592 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3593 }
3594 }
3595
Michael Jurkad7c28052012-04-27 15:43:36 -07003596 @Override
3597 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003598 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003599 final List<CharSequence> text = event.getText();
3600 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003601 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003602 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003603 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003604 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003605 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003606 } else {
3607 text.add(getString(R.string.all_apps_home_button_label));
3608 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003609 return result;
3610 }
3611
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003612 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003613 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003614 */
Adam Cohen091440a2015-03-18 14:16:05 -07003615 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003616 @Override
3617 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003618 closeSystemDialogs();
3619 }
3620 }
3621
3622 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003623 * Receives notifications whenever the appwidgets are reset.
3624 */
3625 private class AppWidgetResetObserver extends ContentObserver {
3626 public AppWidgetResetObserver() {
3627 super(new Handler());
3628 }
3629
3630 @Override
3631 public void onChange(boolean selfChange) {
3632 onAppWidgetReset();
3633 }
3634 }
3635
3636 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003637 * If the activity is currently paused, signal that we need to run the passed Runnable
3638 * in onResume.
3639 *
3640 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003641 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3642 * wrong when we're not running, and if the activity comes back to what the configuration was
3643 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003644 *
3645 * Implementation of the method from LauncherModel.Callbacks.
3646 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003647 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003649 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003650 if (mPaused) {
3651 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003652 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003653 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003654 }
3655 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003656 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003657 return true;
3658 } else {
3659 return false;
3660 }
3661 }
3662
Michael Jurkac402cd92013-05-20 15:49:32 +02003663 private boolean waitUntilResume(Runnable run) {
3664 return waitUntilResume(run, false);
3665 }
3666
Michael Jurka1e2f4652013-07-08 18:03:46 -07003667 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003668 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003669 }
3670
Michael Jurka7607c2f2013-04-03 14:33:19 -07003671 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003672 * If the activity is currently paused, signal that we need to re-run the loader
3673 * in onResume.
3674 *
3675 * This needs to be called from incoming places where resources might have been loaded
3676 * while we are paused. That is becaues the Configuration might be wrong
3677 * when we're not running, and if it comes back to what it was when we
3678 * were paused, we are not restarted.
3679 *
3680 * Implementation of the method from LauncherModel.Callbacks.
3681 *
3682 * @return true if we are currently paused. The caller might be able to
3683 * skip some work in that case since we will come back again.
3684 */
3685 public boolean setLoadOnResume() {
3686 if (mPaused) {
3687 Log.i(TAG, "setLoadOnResume");
3688 mOnResumeNeedsLoad = true;
3689 return true;
3690 } else {
3691 return false;
3692 }
3693 }
3694
3695 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003697 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003699 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003700 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003701 } else {
3702 return SCREEN_COUNT / 2;
3703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003705
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706 /**
3707 * Refreshes the shortcuts shown on the workspace.
3708 *
3709 * Implementation of the method from LauncherModel.Callbacks.
3710 */
3711 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003712 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003713
Michael Jurka7607c2f2013-04-03 14:33:19 -07003714 // If we're starting binding all over again, clear any bind calls we'd postponed in
3715 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3716 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003717 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003718
Winson Chungd64d1762013-08-20 14:37:16 -07003719 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003720 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003721 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003722
Michael Jurka05bf6442011-11-30 20:28:53 -08003723 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003724 if (mHotseat != null) {
3725 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 }
3727 }
3728
Adam Cohendcd297f2013-06-18 13:13:40 -07003729 @Override
3730 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003731 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003732
Adam Cohen5084cba2013-09-03 12:01:16 -07003733 // If there are no screens, we need to have an empty screen
3734 if (orderedScreenIds.size() == 0) {
3735 mWorkspace.addExtraEmptyScreen();
3736 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003737
3738 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003739 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003740 if (hasCustomContentToLeft()) {
3741 mWorkspace.createCustomContentContainer();
3742 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003743 }
Winson Chung64359a52013-07-08 17:17:08 -07003744 }
3745
3746 @Override
3747 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003748 // Log to disk
3749 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3750 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3751 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003752 int count = orderedScreenIds.size();
3753 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003754 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003755 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003756 }
3757
Adam Cohen39a06042013-07-19 14:30:12 -07003758 private boolean shouldShowWeightWatcher() {
3759 String spKey = LauncherAppState.getSharedPreferencesKey();
3760 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003761 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003762
3763 return show;
3764 }
3765
3766 private void toggleShowWeightWatcher() {
3767 String spKey = LauncherAppState.getSharedPreferencesKey();
3768 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3769 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3770
3771 show = !show;
3772
3773 SharedPreferences.Editor editor = sp.edit();
3774 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3775 editor.commit();
3776
3777 if (mWeightWatcher != null) {
3778 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3779 }
3780 }
3781
Winson Chungd64d1762013-08-20 14:37:16 -07003782 public void bindAppsAdded(final ArrayList<Long> newScreens,
3783 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003784 final ArrayList<ItemInfo> addAnimated,
3785 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003786 Runnable r = new Runnable() {
3787 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003788 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003789 }
3790 };
3791 if (waitUntilResume(r)) {
3792 return;
3793 }
3794
Winson Chungd64d1762013-08-20 14:37:16 -07003795 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003796 if (newScreens != null) {
3797 bindAddScreens(newScreens);
3798 }
Winson Chungd64d1762013-08-20 14:37:16 -07003799
3800 // We add the items without animation on non-visible pages, and with
3801 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003802 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 bindItems(addNotAnimated, 0,
3804 addNotAnimated.size(), false);
3805 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003806 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 bindItems(addAnimated, 0,
3808 addAnimated.size(), true);
3809 }
Winson Chungc58497e2013-09-03 17:48:37 -07003810
Winson Chung87412982013-10-03 18:34:14 -07003811 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003812 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003813
Winson Chungb745afb2015-03-02 11:51:23 -08003814 if (addedApps != null && mAppsView != null) {
3815 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003816 }
Winson Chungd64d1762013-08-20 14:37:16 -07003817 }
3818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003819 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 * Bind the items start-end from the list.
3821 *
3822 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003823 */
Winson Chung64359a52013-07-08 17:17:08 -07003824 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3825 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003826 Runnable r = new Runnable() {
3827 public void run() {
3828 bindItems(shortcuts, start, end, forceAnimateIcons);
3829 }
3830 };
3831 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003832 return;
3833 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003834
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003836 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3837 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003838 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003839 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003840 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003843
3844 // Short circuit if we are loading dock items for a configuration which has no dock
3845 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3846 mHotseat == null) {
3847 continue;
3848 }
3849
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 switch (item.itemType) {
3851 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3852 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003853 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003854 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003855
Winson Chung64359a52013-07-08 17:17:08 -07003856 /*
3857 * TODO: FIX collision case
3858 */
Winson Chungce376632013-07-11 16:12:41 -07003859 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3860 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3861 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003862 View v = cl.getChildAt(item.cellX, item.cellY);
3863 Object tag = v.getTag();
3864 String desc = "Collision while binding workspace item: " + item
3865 + ". Collides with " + tag;
3866 if (LauncherAppState.isDogfoodBuild()) {
3867 throw (new RuntimeException(desc));
3868 } else {
3869 Log.d(TAG, desc);
3870 }
Winson Chungce376632013-07-11 16:12:41 -07003871 }
Winson Chung64359a52013-07-08 17:17:08 -07003872 }
3873
Adam Cohendcd297f2013-06-18 13:13:40 -07003874 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3875 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003876 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003877 // Animate all the applications up now
3878 shortcut.setAlpha(0f);
3879 shortcut.setScaleX(0f);
3880 shortcut.setScaleY(0f);
3881 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003882 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003885 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003886 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003887 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003888 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003889 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3890 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003891 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003892 default:
3893 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003895 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003896
Winson Chung997a9232013-07-24 15:33:46 -07003897 if (animateIcons) {
3898 // Animate to the correct page
3899 if (newShortcutsScreenId > -1) {
3900 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003901 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003902 final Runnable startBounceAnimRunnable = new Runnable() {
3903 public void run() {
3904 anim.playTogether(bounceAnims);
3905 anim.start();
3906 }
3907 };
Winson Chung997a9232013-07-24 15:33:46 -07003908 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003909 // We post the animation slightly delayed to prevent slowdowns
3910 // when we are loading right after we return to launcher.
3911 mWorkspace.postDelayed(new Runnable() {
3912 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003913 if (mWorkspace != null) {
3914 mWorkspace.snapToPage(newScreenIndex);
3915 mWorkspace.postDelayed(startBounceAnimRunnable,
3916 NEW_APPS_ANIMATION_DELAY);
3917 }
Winson Chung94d67682013-09-25 16:29:40 -07003918 }
3919 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003920 } else {
3921 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003922 }
3923 }
Winson Chung64359a52013-07-08 17:17:08 -07003924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
3927
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 /**
3929 * Implementation of the method from LauncherModel.Callbacks.
3930 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003931 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003932 Runnable r = new Runnable() {
3933 public void run() {
3934 bindFolders(folders);
3935 }
3936 };
3937 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003938 return;
3939 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003940 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942
3943 /**
3944 * Add the views for a widget to the workspace.
3945 *
3946 * Implementation of the method from LauncherModel.Callbacks.
3947 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003948 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003949 Runnable r = new Runnable() {
3950 public void run() {
3951 bindAppWidget(item);
3952 }
3953 };
3954 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003955 return;
3956 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003957
Daniel Sandler843e8602010-06-07 14:59:01 -04003958 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3959 if (DEBUG_WIDGETS) {
3960 Log.d(TAG, "bindAppWidget: " + item);
3961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 final Workspace workspace = mWorkspace;
3963
Adam Cohen59400422014-03-05 18:07:04 -08003964 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003965 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003966
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003967 if (!mIsSafeModeEnabled
3968 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3969 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003970 if (appWidgetInfo == null) {
3971 if (DEBUG_WIDGETS) {
3972 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3973 + " belongs to component " + item.providerName
3974 + ", as the povider is null");
3975 }
3976 LauncherModel.deleteItemFromDatabase(this, item);
3977 return;
3978 }
3979 // Note: This assumes that the id remap broadcast is received before this step.
3980 // If that is not the case, the id remap will be ignored and user may see the
3981 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003982 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003983 pendingInfo.spanX = item.spanX;
3984 pendingInfo.spanY = item.spanY;
3985 pendingInfo.minSpanX = item.minSpanX;
3986 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003987 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003988 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003989
Sunny Goyalff572272014-07-23 13:58:07 -07003990 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003991 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3992 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003993
3994 // TODO consider showing a permission dialog when the widget is clicked.
3995 if (!success) {
3996 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3997 if (DEBUG_WIDGETS) {
3998 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3999 + " belongs to component " + item.providerName
4000 + ", as the launcher is unable to bing a new widget id");
4001 }
4002 LauncherModel.deleteItemFromDatabase(this, item);
4003 return;
4004 }
4005
4006 item.appWidgetId = newWidgetId;
4007
4008 // If the widget has a configure activity, it is still needs to set it up, otherwise
4009 // the widget is ready to go.
4010 item.restoreStatus = (appWidgetInfo.configure == null)
4011 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4012 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4013
4014 LauncherModel.updateItemInDatabase(this, item);
4015 }
4016
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004017 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004018 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004019 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004020 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4021 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004022 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004023
Sunny Goyal651077b2014-06-30 14:15:31 -07004024 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4025 } else {
4026 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004027 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4028 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004029 view.updateIcon(mIconCache);
4030 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004031 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004032 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004033 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004036 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004037
Adam Cohendcd297f2013-06-18 13:13:40 -07004038 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004039 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004040 if (!item.isCustomWidget()) {
4041 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4042 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004043
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 workspace.requestLayout();
4045
Daniel Sandler843e8602010-06-07 14:59:01 -04004046 if (DEBUG_WIDGETS) {
4047 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4048 + (SystemClock.uptimeMillis()-start) + "ms");
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 }
4051
Sunny Goyalff572272014-07-23 13:58:07 -07004052 /**
4053 * Restores a pending widget.
4054 *
4055 * @param appWidgetId The app widget id
4056 * @param cellInfo The position on screen where to create the widget.
4057 */
4058 private void completeRestoreAppWidget(final int appWidgetId) {
4059 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4060 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4061 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4062 return;
4063 }
4064
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004065 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004066 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4067
4068 mWorkspace.reinflateWidgetsIfNecessary();
4069 LauncherModel.updateItemInDatabase(this, info);
4070 }
4071
Adam Cohen1462de32012-07-24 22:34:36 -07004072 public void onPageBoundSynchronously(int page) {
4073 mSynchronouslyBoundPages.add(page);
4074 }
4075
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 /**
4077 * Callback saying that there aren't any more items to bind.
4078 *
4079 * Implementation of the method from LauncherModel.Callbacks.
4080 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004081 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004082 Runnable r = new Runnable() {
4083 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004084 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004085 }
4086 };
4087 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004088 return;
4089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 if (mSavedState != null) {
4091 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004092 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 mSavedState = null;
4095 }
4096
Adam Cohen1462de32012-07-24 22:34:36 -07004097 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004099 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004100 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004101
4102 // If we received the result of any pending adds while the loader was running (e.g. the
4103 // widget configuration forced an orientation change), process them now.
4104 if (sPendingAddItem != null) {
4105 final long screenId = completeAdd(sPendingAddItem);
4106
4107 // TODO: this moves the user to the page where the pending item was added. Ideally,
4108 // the screen would be guaranteed to exist after bind, and the page would be set through
4109 // the workspace restore process.
4110 mWorkspace.post(new Runnable() {
4111 @Override
4112 public void run() {
4113 mWorkspace.snapToScreenId(screenId);
4114 }
4115 });
4116 sPendingAddItem = null;
4117 }
4118
Sunny Goyal756adbc2015-04-16 15:20:51 -07004119 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004120
4121 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004122 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004123 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004124 }
4125
Adam Cohen3ed316a2014-07-23 18:21:20 -07004126 private void sendLoadingCompleteBroadcastIfNecessary() {
4127 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4128 String permission =
4129 getResources().getString(R.string.receive_first_load_broadcast_permission);
4130 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4131 sendBroadcast(intent, permission);
4132 SharedPreferences.Editor editor = mSharedPrefs.edit();
4133 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4134 editor.apply();
4135 }
4136 }
4137
Winson Chunga0b7e862013-09-05 16:03:15 -07004138 public boolean isAllAppsButtonRank(int rank) {
4139 if (mHotseat != null) {
4140 return mHotseat.isAllAppsButtonRank(rank);
4141 }
4142 return false;
4143 }
4144
Winson Chunga2413752012-04-03 14:22:34 -07004145 private boolean canRunNewAppsAnimation() {
4146 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4147 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4148 }
4149
Winson Chungc9168342013-06-26 14:54:55 -07004150 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4151 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4152 PropertyValuesHolder.ofFloat("alpha", 1f),
4153 PropertyValuesHolder.ofFloat("scaleX", 1f),
4154 PropertyValuesHolder.ofFloat("scaleY", 1f));
4155 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4156 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4157 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4158 return bounceAnim;
4159 }
4160
Adam Cohen27772732013-11-11 14:00:56 +00004161 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004162 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004163 }
4164
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004165 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004166 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004167 }
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}