blob: 7220e02583c998c2ceacb98ac9ae333cac6f90ce [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;
Adam Cohen091440a2015-03-18 14:16:05 -0700103import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700104import com.android.launcher3.widget.PendingAddWidgetInfo;
105import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700106
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.DataInputStream;
108import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700110import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800115import java.lang.reflect.InvocationTargetException;
116import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700119import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700122import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700123import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000124import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126/**
127 * Default launcher application.
128 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100129public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700130 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800131 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
132 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700133 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700150 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Mathew Inwood876a8462013-06-14 14:12:41 +0100152 /**
153 * IntentStarter uses request codes starting with this. This must be greater than all activity
154 * request codes used internally.
155 */
156 protected static final int REQUEST_LAST = 100;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
159
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Winson Chung2672ff92012-05-04 16:22:30 -0700166 // The Intent extra that defines whether to ignore the launch animation
167 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400168 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
171 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700172 // Type: int
173 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
176 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
178 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: boolean
183 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
184 // Type: long
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700186 // Type: int
187 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: int[]
195 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Adam Cohen3ed316a2014-07-23 18:21:20 -0700200 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
201 static final String ACTION_FIRST_LOAD_COMPLETE =
202 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
203
Adam Cohen39a06042013-07-19 14:30:12 -0700204 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700205 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700206
Sunny Goyal594d76d2014-11-06 10:12:54 -0800207 private static final String QSB_WIDGET_ID = "qsb_widget_id";
208 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
209
Winson Chunga6945242014-01-08 14:04:34 -0800210 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
211
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800213 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217 private boolean mIsSafeModeEnabled;
218
Adam Cohenc2d6e892014-10-16 09:49:24 -0700219 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
220 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700221 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000228 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
229 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
230
Winson Chunga2413752012-04-03 14:22:34 -0700231 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700232 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
233 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700235
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800236 private final BroadcastReceiver mCloseSystemDialogsReceiver
237 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private LayoutInflater mInflater;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700243 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800244 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800246 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700247 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalffe83f12014-08-14 17:39:34 -0700249 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800253 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800254 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255
Michael Jurka0280c3b2010-09-17 15:00:07 -0700256 private int[] mTmpAddItemCellCoordinates = new int[2];
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private FolderInfo mFolderInfo;
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
267 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the widget tray screen.
271 private WidgetsContainerView mWidgetsView;
272
Winson Chungf0ea4d32011-06-06 14:27:16 -0700273 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800274 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700277 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
278 // scroll issues (because the workspace may not have been measured yet) and extra work.
279 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
280 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
282 private SpannableStringBuilder mDefaultKeySsb = null;
283
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800286 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private boolean mRestoring;
288 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700289 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Michael Jurka1e2f4652013-07-08 18:03:46 -0700291 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700292 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 private Bundle mSavedInstanceState;
295
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700298 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700300 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Adam Cohen61f560d2013-09-30 15:58:20 -0700307 private View.OnTouchListener mHapticFeedbackTouchListener;
308
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 // Related to the auto-advancing of widgets
310 private final int ADVANCE_MSG = 1;
311 private final int mAdvanceInterval = 20000;
312 private final int mAdvanceStagger = 250;
313 private long mAutoAdvanceSentTime;
314 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 new HashMap<View, AppWidgetProviderInfo>();
317
Winson Chung400438b2011-01-16 17:53:48 -0800318 // Determines how long to wait after a rotation before restoring the screen orientation to
319 // match the sensor state.
320 private final int mRestoreScreenOrientationDelay = 500;
321
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700323
Adam Cohen1462de32012-07-24 22:34:36 -0700324 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700326
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700327 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700328 static Date sDateStamp = new Date();
329 static DateFormat sDateFormat =
330 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
331 static long sRunStart = System.currentTimeMillis();
332 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333
Winson Chung46353de2012-02-16 14:05:10 -0800334 // We only want to get the SharedPreferences once since it does an FS stat each time we get
335 // it from the context.
336 private SharedPreferences mSharedPrefs;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700340 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Michael Jurkaddd62e92011-02-16 17:49:14 -0800345 private BubbleTextView mWaitingForResume;
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
348 new HashMap<String, CustomAppWidget>();
349
350 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
351 static {
352 if (ENABLE_CUSTOM_WIDGET_TEST) {
353 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
354 }
355 }
356
Chet Haasea8f996d2015-02-17 12:56:04 -0800357 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
358 private static Method sClipRevealMethod = null;
359 static {
360 Class<?> activityOptionsClass = ActivityOptions.class;
361 try {
362 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
363 View.class, int.class, int.class, int.class, int.class);
364 } catch (Exception e) {
365 // Earlier version
366 }
367 }
368
Adam Cohen091440a2015-03-18 14:16:05 -0700369 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700370 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700371 if (mWorkspace != null) {
372 mWorkspace.buildPageHardwareLayers();
373 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700374 }
375 };
376
Adam Cohendb364c32014-05-20 14:23:40 -0700377 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800378
Adam Cohen091440a2015-03-18 14:16:05 -0700379 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int requestCode;
381 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700382 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700383 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 int cellX;
385 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700386 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800387 }
388
Daniel Sandlerff02d492013-08-05 02:12:05 -0400389 private Stats mStats;
390
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700391 FocusIndicatorView mFocusHandler;
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 @Override
394 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700395 if (DEBUG_STRICT_MODE) {
396 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
397 .detectDiskReads()
398 .detectDiskWrites()
399 .detectNetwork() // or .detectAll() for all detectable problems
400 .penaltyLog()
401 .build());
402 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
403 .detectLeakedSqlLiteObjects()
404 .detectLeakedClosableObjects()
405 .penaltyLog()
406 .penaltyDeath()
407 .build());
408 }
409
Adam Cohen9211d422014-10-07 18:14:53 -0700410 if (mLauncherCallbacks != null) {
411 mLauncherCallbacks.preOnCreate();
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400415
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400417 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700418 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700421 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700422
423 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400424 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700425 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700426 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800427 mModel = app.setLauncher(this);
428 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700429 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400430 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800432 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Daniel Sandlerff02d492013-08-05 02:12:05 -0400434 mStats = new Stats(this);
435
Sunny Goyalffe83f12014-08-14 17:39:34 -0700436 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200447 android.os.Debug.startMethodTracing(
448 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
451 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100455 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800457 registerContentObservers();
458
Joe Onorato7c312c12009-08-13 21:36:53 -0700459 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 mSavedState = savedInstanceState;
462 restoreState(mSavedState);
463
464 if (PROFILE_STARTUP) {
465 android.os.Debug.stopMethodTracing();
466 }
467
468 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700469 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 // If the user leaves launcher, then we should just load items asynchronously when
471 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500472 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 } else {
474 // We only load the page synchronously if the user rotates (or triggers a
475 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800476 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
480 // For handling default keys
481 mDefaultKeySsb = new SpannableStringBuilder();
482 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800483
484 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
485 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800486
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 // On large interfaces, we want the screen to auto-rotate based on the current orientation
488 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 if (mLauncherCallbacks.hasLauncherOverlay()) {
493 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700494 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
495 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
496 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 mWorkspace.setLauncherOverlay(mLauncherOverlay);
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700500
501 if (shouldShowIntroScreen()) {
502 showIntroScreen();
503 } else {
504 showFirstRunActivity();
505 showFirstRunClings();
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
508
509 private LauncherCallbacks mLauncherCallbacks;
510
511 public void onPostCreate(Bundle savedInstanceState) {
512 super.onPostCreate(savedInstanceState);
513 if (mLauncherCallbacks != null) {
514 mLauncherCallbacks.onPostCreate(savedInstanceState);
515 }
516 }
517
518 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
519 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700520 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
521 @Override
522 public void onAllAppsBoundsChanged(Rect bounds) {
523 mAppsView.setFixedBounds(Launcher.this, bounds);
524 }
525
526 @Override
527 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700528 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
529 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700530 }
531 });
Adam Cohen9211d422014-10-07 18:14:53 -0700532 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700533 }
534
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700535 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public void onLauncherProviderChange() {
537 if (mLauncherCallbacks != null) {
538 mLauncherCallbacks.onLauncherProviderChange();
539 }
540 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700541
Adam Cohen9211d422014-10-07 18:14:53 -0700542 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700543 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700544 if (mLauncherCallbacks != null) {
545 return mLauncherCallbacks.hasCustomContentToLeft();
546 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700547 return false;
548 }
549
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500551 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 * {@link #addToCustomContentPage}. This will only be invoked if
553 * {@link #hasCustomContentToLeft()} is {@code true}.
554 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500555 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700556 if (mLauncherCallbacks != null) {
557 mLauncherCallbacks.populateCustomContentContainer();
558 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600559 }
560
561 /**
562 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
563 * ensure the custom content page is added or removed if necessary.
564 */
565 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600566 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 // Not bound yet, wait for bindScreens to be called.
568 return;
569 }
570
571 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
572 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500573 mWorkspace.createCustomContentContainer();
574 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600575 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
576 mWorkspace.removeCustomContentPage();
577 }
578 }
579
Adam Cohen091440a2015-03-18 14:16:05 -0700580 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 if (sLocaleConfiguration == null) {
582 new AsyncTask<Void, Void, LocaleConfiguration>() {
583 @Override
584 protected LocaleConfiguration doInBackground(Void... unused) {
585 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
586 readConfiguration(Launcher.this, localeConfiguration);
587 return localeConfiguration;
588 }
589
590 @Override
591 protected void onPostExecute(LocaleConfiguration result) {
592 sLocaleConfiguration = result;
593 checkForLocaleChange(); // recursive, but now with a locale configuration
594 }
595 }.execute();
596 return;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final Configuration configuration = getResources().getConfiguration();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final String locale = configuration.locale.toString();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mcc = configuration.mcc;
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mnc = configuration.mnc;
609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 sLocaleConfiguration.locale = locale;
614 sLocaleConfiguration.mcc = mcc;
615 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700616
Joe Onorato0589f0f2010-02-08 13:44:00 -0800617 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618
619 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100620 new AsyncTask<Void, Void, Void>() {
621 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100623 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 }
Michael Jurka43467462013-11-14 12:03:58 +0100625 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700626 }
627 }
628
Adam Cohen091440a2015-03-18 14:16:05 -0700629 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700630 public String locale;
631 public int mcc = -1;
632 public int mnc = -1;
633 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700634
Adam Cohen091440a2015-03-18 14:16:05 -0700635 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700636 DataInputStream in = null;
637 try {
Helena Josol28db2802014-10-09 17:04:09 +0100638 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700639 configuration.locale = in.readUTF();
640 configuration.mcc = in.readInt();
641 configuration.mnc = in.readInt();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 // Ignore
646 } finally {
647 if (in != null) {
648 try {
649 in.close();
650 } catch (IOException e) {
651 // Ignore
652 }
653 }
654 }
655 }
656
Adam Cohen091440a2015-03-18 14:16:05 -0700657 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700658 DataOutputStream out = null;
659 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100660 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100661 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700662 out.writeUTF(configuration.locale);
663 out.writeInt(configuration.mcc);
664 out.writeInt(configuration.mnc);
665 out.flush();
666 } catch (FileNotFoundException e) {
667 // Ignore
668 } catch (IOException e) {
669 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100670 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700671 } finally {
672 if (out != null) {
673 try {
674 out.close();
675 } catch (IOException e) {
676 // Ignore
677 }
678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680 }
681
Anton Hanssona2f665f2013-09-26 12:18:32 +0100682 public Stats getStats() {
683 return mStats;
684 }
685
Bjorn Bringertc459e522013-06-07 19:36:01 +0100686 public LayoutInflater getInflater() {
687 return mInflater;
688 }
689
Hyunyoung Song3f471442015-04-08 19:01:34 -0700690 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700691 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
692 // that is subsequently removed from the workspace in startBinding().
693 return !mModel.isLoadingWorkspace();
694 }
695
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800696 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000697 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100698 if (Build.VERSION.SDK_INT >= 17) {
699 return View.generateViewId();
700 } else {
701 // View.generateViewId() is not available. The following fallback logic is a copy
702 // of its implementation.
703 for (;;) {
704 final int result = sNextGeneratedId.get();
705 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
706 int newValue = result + 1;
707 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
708 if (sNextGeneratedId.compareAndSet(result, newValue)) {
709 return result;
710 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000711 }
712 }
713 }
714
715 public int getViewIdForItem(ItemInfo info) {
716 // This cast is safe given the > 2B range for int.
717 int itemId = (int) info.id;
718 if (mItemIdToViewId.containsKey(itemId)) {
719 return mItemIdToViewId.get(itemId);
720 }
721 int viewId = generateViewId();
722 mItemIdToViewId.put(itemId, viewId);
723 return viewId;
724 }
725
726 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
728 * a configuration step, this allows the proper animations to run after other transitions.
729 */
Adam Cohendb364c32014-05-20 14:23:40 -0700730 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700731 long screenId = args.screenId;
732 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
733 // When the screen id represents an actual screen (as opposed to a rank) we make sure
734 // that the drop page actually exists.
735 screenId = ensurePendingDropLayoutExists(args.screenId);
736 }
Adam Cohendb364c32014-05-20 14:23:40 -0700737
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700740 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700742 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700744 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700745 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700746 case REQUEST_RECONFIGURE_APPWIDGET:
747 completeRestoreAppWidget(args.appWidgetId);
748 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800749 }
Michael Jurka27614d22012-04-02 06:40:22 -0700750 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
751 // if you turned the screen off and then back while in All Apps, Launcher would not
752 // return to the workspace. Clearing mAddInfo.container here fixes this issue
753 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700754 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 }
756
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800757 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700759 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700760 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700763
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 Runnable exitSpringLoaded = new Runnable() {
765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
768 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
769 }
770 };
771
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
775 if (resultCode == RESULT_CANCELED) {
776 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700777 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800780 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 }
783 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
785 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700786 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200787 }
788 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200790
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
792 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700793
Adam Cohendb364c32014-05-20 14:23:40 -0700794 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 // We have special handling for widgets
796 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800797 final int appWidgetId;
798 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
799 : -1;
800 if (widgetId < 0) {
801 appWidgetId = pendingAddWidgetId;
802 } else {
803 appWidgetId = widgetId;
804 }
805
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800807 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700808 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
809 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 result = RESULT_CANCELED;
811 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700812 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 @Override
814 public void run() {
815 exitSpringLoadedDragModeDelayed(false, 0, null);
816 }
817 };
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (workspaceLocked) {
819 // No need to remove the empty screen if we're mid-binding, as the
820 // the bind will not add the empty screen.
821 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
822 } else {
823 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825 }
Winson Chung5aaab772012-04-27 15:24:02 -0700826 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700828 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
829 // When the screen id represents an actual screen (as opposed to a rank)
830 // we make sure that the drop page actually exists.
831 mPendingAddInfo.screenId =
832 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
833 }
Adam Cohendb364c32014-05-20 14:23:40 -0700834 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
835
836 dropLayout.setDropPending(true);
837 final Runnable onComplete = new Runnable() {
838 @Override
839 public void run() {
840 completeTwoStageWidgetDrop(resultCode, appWidgetId);
841 dropLayout.setDropPending(false);
842 }
843 };
844 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
845 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
846 } else {
847 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
848 mPendingAddInfo);
849 sPendingAddItem = args;
850 }
Winson Chung5aaab772012-04-27 15:24:02 -0700851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 return;
853 }
854
Sunny Goyalff572272014-07-23 13:58:07 -0700855 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
856 if (resultCode == RESULT_OK) {
857 // Update the widget view.
858 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
859 pendingAddWidgetId, mPendingAddInfo);
860 if (workspaceLocked) {
861 sPendingAddItem = args;
862 } else {
863 completeAdd(args);
864 }
865 }
866 // Leave the widget in the pending state if the user canceled the configure.
867 return;
868 }
869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 // The pattern used here is that a user PICKs a specific application,
871 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
874 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700875 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700876 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
877 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800878 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700879 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800880 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700882 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
883 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
Adam Cohen00074722013-10-11 17:46:09 -0700885 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700886 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700887 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800890
891 }
892
893 @Override
894 protected void onActivityResult(
895 final int requestCode, final int resultCode, final Intent data) {
896 handleActivityResult(requestCode, resultCode, data);
897 if (mLauncherCallbacks != null) {
898 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
899 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 }
901
Adam Cohendb364c32014-05-20 14:23:40 -0700902 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
903 appWidgetId, ItemInfo info) {
904 PendingAddArguments args = new PendingAddArguments();
905 args.requestCode = requestCode;
906 args.intent = data;
907 args.container = info.container;
908 args.screenId = info.screenId;
909 args.cellX = info.cellX;
910 args.cellY = info.cellY;
911 args.appWidgetId = appWidgetId;
912 return args;
913 }
914
915 /**
916 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
917 *
918 * @param screenId the screen id to check
919 * @return the new screen, or screenId if it exists
920 */
921 private long ensurePendingDropLayoutExists(long screenId) {
922 CellLayout dropLayout =
923 (CellLayout) mWorkspace.getScreenWithId(screenId);
924 if (dropLayout == null) {
925 // it's possible that the add screen was removed because it was
926 // empty and a re-bind occurred
927 mWorkspace.addExtraEmptyScreen();
928 return mWorkspace.commitExtraEmptyScreen();
929 } else {
930 return screenId;
931 }
932 }
933
Adam Cohen091440a2015-03-18 14:16:05 -0700934 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700935 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 Runnable onCompleteRunnable = null;
938 int animationType = 0;
939
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 if (resultCode == RESULT_OK) {
942 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
943 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 mPendingAddWidgetInfo);
945 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 onCompleteRunnable = new Runnable() {
947 @Override
948 public void run() {
949 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700951 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
952 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 }
954 };
955 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800956 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800957 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 if (mDragLayer.getAnimatedView() != null) {
960 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
961 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
962 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700963 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700964 // The animated view may be null in the case of a rotation during widget configuration
965 onCompleteRunnable.run();
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 protected void onStop() {
971 super.onStop();
972 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onStop();
976 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700977 }
978
979 @Override
980 protected void onStart() {
981 super.onStart();
982 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700983
984 if (mLauncherCallbacks != null) {
985 mLauncherCallbacks.onStart();
986 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700987 }
988
989 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200991 long startTime = 0;
992 if (DEBUG_RESUME_TIME) {
993 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400994 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200995 }
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.preOnResume();
999 }
1000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001002
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001004 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.APPS) {
1007 showAppsView(false /* animated */, false /* resetListToTop */);
1008 } else if (mOnResumeState == State.WIDGETS) {
1009 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 }
1011 mOnResumeState = State.NONE;
1012
Craig Mautner360310b2012-10-26 15:13:08 -07001013 // Background was set to gradient in onPause(), restore to black if in all apps.
1014 setWorkspaceBackground(mState == State.WORKSPACE);
1015
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001016 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001017 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001018 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001019 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1025 // execute them here
1026 long startTimeCallbacks = 0;
1027 if (DEBUG_RESUME_TIME) {
1028 startTimeCallbacks = System.currentTimeMillis();
1029 }
1030
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1032 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001033 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 if (DEBUG_RESUME_TIME) {
1036 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1037 (System.currentTimeMillis() - startTimeCallbacks));
1038 }
Michael Jurka447bf842013-05-15 14:52:15 +02001039 }
Michael Jurka54554252013-08-01 12:52:23 +02001040 if (mOnResumeCallbacks.size() > 0) {
1041 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1042 mOnResumeCallbacks.get(i).run();
1043 }
1044 mOnResumeCallbacks.clear();
1045 }
Winson Chunge4e50662012-01-23 14:45:13 -08001046
1047 // Reset the pressed state of icons that were locked in the press state while activities
1048 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001049 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001050 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 mWaitingForResume.setStayPressed(false);
1052 }
Winson Chung82963d52013-10-09 11:20:57 -07001053
Adam Cohen06dff352012-06-01 17:17:08 -07001054 // It is possible that widgets can receive updates while launcher is not in the foreground.
1055 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1056 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1057 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001058 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001059 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
1065 if (mWorkspace.getCustomContentCallbacks() != null) {
1066 // If we are resuming and the custom content is the current page, we call onShow().
1067 // It is also poassible that onShow will instead be called slightly after first layout
1068 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1069 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001070 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001071 }
1072 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001073 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001074 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001075
Sunny Goyal756adbc2015-04-16 15:20:51 -07001076 if (!isWorkspaceLoading()) {
1077 // Process any items that were added while Launcher was away.
1078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1079 }
Adam Cohen9211d422014-10-07 18:14:53 -07001080
1081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onResume();
1083 }
Adam Cohen06dff352012-06-01 17:17:08 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001088 // Ensure that items added to Launcher are queued until Launcher returns
1089 InstallShortcutReceiver.enableInstallQueue();
1090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001093 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001094 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001095
1096 // We call onHide() aggressively. The custom content callbacks should be able to
1097 // debounce excess onHide calls.
1098 if (mWorkspace.getCustomContentCallbacks() != null) {
1099 mWorkspace.getCustomContentCallbacks().onHide();
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen9211d422014-10-07 18:14:53 -07001102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
1107 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001108 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1109 // by a onResume or by scrolling otherwise.
1110 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111
1112 // Custom content is completely hidden
1113 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001114
1115 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1116 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001117
1118 // Indicates whether the user is allowed to scroll away from the custom content.
1119 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlay {
1123
1124 /**
1125 * Touch interaction leading to overscroll has begun
1126 */
1127 public void onScrollInteractionBegin();
1128
1129 /**
1130 * Touch interaction related to overscroll has ended
1131 */
1132 public void onScrollInteractionEnd();
1133
1134 /**
1135 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1136 * screen (or in the case of RTL, the rightmost screen).
1137 */
1138 public void onScrollChange(int progress, boolean rtl);
1139
1140 /**
1141 * Screen has stopped scrolling
1142 */
1143 public void onScrollSettled();
1144
1145 /**
1146 * This method can be called by the Launcher in order to force the LauncherOverlay
1147 * to exit fully immersive mode.
1148 */
1149 public void forceExitFullImmersion();
1150 }
1151
Winson Chung0f785722015-04-08 10:27:49 -07001152 public interface LauncherAppsCallbacks {
1153 /**
1154 * Updates launcher to the available space that AllApps can take so as not to overlap with
1155 * any other views.
1156 */
1157 public void onAllAppsBoundsChanged(Rect bounds);
1158
1159 /**
1160 * Called to dismiss all apps if it is showing.
1161 */
1162 public void dismissAllApps();
1163 }
1164
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 public interface LauncherOverlayCallbacks {
1166 /**
1167 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1168 * however it doesn't modify any state within the launcher.
1169 */
1170 public boolean canEnterFullImmersion();
1171
1172 /**
1173 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1174 * eg. by occupying the full screen and handling all touch events.
1175 *
1176 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1177 * case, Launcher will modify any necessary state and assumes the overlay is
1178 * handling all interaction. If false, the LauncherOverlay should cancel any
1179 *
1180 */
1181 public boolean enterFullImmersion();
1182
1183 /**
1184 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1185 * full control over UI and state.
1186 */
1187 public void exitFullImmersion();
1188 }
1189
1190 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1191
1192 @Override
1193 public boolean canEnterFullImmersion() {
1194 return mState == State.WORKSPACE;
1195 }
1196
1197 @Override
1198 public boolean enterFullImmersion() {
1199 if (mState == State.WORKSPACE) {
1200 // When fully immersed, disregard any touches which fall through.
1201 mDragLayer.setBlockTouch(true);
1202 return true;
1203 }
1204 return false;
1205 }
1206
1207 @Override
1208 public void exitFullImmersion() {
1209 mDragLayer.setBlockTouch(false);
1210 }
1211 }
1212
Jorim Jaggid017f882014-01-14 17:08:48 -08001213 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001214 if (mLauncherCallbacks != null) {
1215 return mLauncherCallbacks.hasSettings();
1216 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001217 return false;
1218 }
1219
Adam Cohen9211d422014-10-07 18:14:53 -07001220 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001221 CustomContentCallbacks callbacks, String description) {
1222 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001223 }
1224
Adam Cohenedb40762013-07-18 16:45:45 -07001225 // The custom content needs to offset its content to account for the QSB
1226 public int getTopOffsetForCustomContent() {
1227 return mWorkspace.getPaddingTop();
1228 }
1229
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001230 @Override
1231 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001233 if (mModel.isCurrentCallbacks(this)) {
1234 mModel.stopLoader();
1235 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001236 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1237
Romain Guy13c2e7b2010-03-10 19:45:00 -08001238 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001239 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001241 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001242 @Override
1243 public void onWindowFocusChanged(boolean hasFocus) {
1244 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001245 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001246
1247 if (mLauncherCallbacks != null) {
1248 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1249 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001250 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 private boolean acceptFilter() {
1253 final InputMethodManager inputManager = (InputMethodManager)
1254 getSystemService(Context.INPUT_METHOD_SERVICE);
1255 return !inputManager.isFullscreenMode();
1256 }
1257
1258 @Override
1259 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001260 final int uniChar = event.getUnicodeChar();
1261 final boolean handled = super.onKeyDown(keyCode, event);
1262 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1263 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1265 keyCode, event);
1266 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001268 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001269 // showSearchDialog()
1270 // If there are multiple keystrokes before the search dialog takes focus,
1271 // onSearchRequested() will be called for every keystroke,
1272 // but it is idempotent, so it's fine.
1273 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275 }
1276
Joe Onorato8a9625e2010-01-28 15:55:35 -08001277 // Eat the long press event so the keyboard doesn't come up.
1278 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1279 return true;
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 return handled;
1283 }
1284
Karl Rosaen138a0412009-04-23 19:00:21 -07001285 private String getTypedText() {
1286 return mDefaultKeySsb.toString();
1287 }
1288
1289 private void clearTypedText() {
1290 mDefaultKeySsb.clear();
1291 mDefaultKeySsb.clearSpans();
1292 Selection.setSelection(mDefaultKeySsb, 0);
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001296 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1297 * State
1298 */
1299 private static State intToState(int stateOrdinal) {
1300 State state = State.WORKSPACE;
1301 final State[] stateValues = State.values();
1302 for (int i = 0; i < stateValues.length; i++) {
1303 if (stateValues[i].ordinal() == stateOrdinal) {
1304 state = stateValues[i];
1305 break;
1306 }
1307 }
1308 return state;
1309 }
1310
1311 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 * Restores the previous state, if it exists.
1313 *
1314 * @param savedState The previous state.
1315 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001316 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 private void restoreState(Bundle savedState) {
1318 if (savedState == null) {
1319 return;
1320 }
1321
Michael Jurka883f55b2010-10-21 15:47:14 -07001322 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001323 if (state == State.APPS || state == State.WIDGETS) {
1324 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 }
1326
Adam Cohen21cd0022013-10-09 18:57:02 -07001327 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1328 PagedView.INVALID_RESTORE_PAGE);
1329 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001330 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001335
Winson Chung3d503fb2011-07-13 17:25:49 -07001336 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1337 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1340 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001341 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1342 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001343 AppWidgetProviderInfo info = savedState.getParcelable(
1344 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1345 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001346 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001347 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 mRestoring = true;
1349 }
1350
1351 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1352 if (renameFolder) {
1353 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001354 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 mRestoring = true;
1356 }
Winson Chunga12a2502010-12-20 14:41:35 -08001357
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001358 mItemIdToViewId = (HashMap<Integer, Integer>)
1359 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 /**
1363 * Finds all the views we need and configure them properly.
1364 */
1365 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001366 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001367
Craig Mautner360310b2012-10-26 15:13:08 -07001368 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001369 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001370 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1371 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001372 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001373 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001374
John Spurlock77e1f472013-09-11 10:09:51 -04001375 mLauncherView.setSystemUiVisibility(
1376 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001377 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Winson Chung4c98d922011-05-31 16:50:48 -07001379 // Setup the drag layer
1380 mDragLayer.setup(this, dragController);
1381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 // Setup the hotseat
1383 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1384 if (mHotseat != null) {
1385 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001386 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 }
1388
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001390 View widgetButton = findViewById(R.id.widget_button);
1391 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 @Override
1393 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001394 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001395 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001396 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 }
1398 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001399 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1400
1401 View wallpaperButton = findViewById(R.id.wallpaper_button);
1402 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 @Override
1404 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001405 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001406 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001407 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408 }
1409 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001410 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1411
1412 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 if (hasSettings()) {
1414 settingsButton.setOnClickListener(new OnClickListener() {
1415 @Override
1416 public void onClick(View arg0) {
1417 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001418 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001419 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001420 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001421 });
1422 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423 } else {
1424 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001425 }
1426
Adam Cohendbdff6b2013-09-18 19:09:15 -07001427 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001428
Winson Chung4c98d922011-05-31 16:50:48 -07001429 // Setup the workspace
1430 mWorkspace.setHapticFeedbackEnabled(false);
1431 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001432 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001433 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001434
Winson Chungf0ea4d32011-06-06 14:27:16 -07001435 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001436 mSearchDropTargetBar = (SearchDropTargetBar)
1437 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001438
Winson Chungb745afb2015-03-02 11:51:23 -08001439 // Setup Apps
1440 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001441 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1442 mAppsView.hideSearchBar();
1443 }
Winson Chungb745afb2015-03-02 11:51:23 -08001444
Winson Chungf0ea4d32011-06-06 14:27:16 -07001445 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001446 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001447
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001449 dragController.setDragScoller(mWorkspace);
1450 dragController.setScrollView(mDragLayer);
1451 dragController.setMoveTarget(mWorkspace);
1452 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001453 if (mSearchDropTargetBar != null) {
1454 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001455 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001456 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001457
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001458 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001460 mWeightWatcher = new WeightWatcher(this);
1461 mWeightWatcher.setAlpha(0.5f);
1462 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 new FrameLayout.LayoutParams(
1464 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001465 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001466 Gravity.BOTTOM)
1467 );
Adam Cohen39a06042013-07-19 14:30:12 -07001468
1469 boolean show = shouldShowWeightWatcher();
1470 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001475 * Sets the all apps button. This method is called from {@link Hotseat}.
1476 */
1477 public void setAllAppsButton(View allAppsButton) {
1478 mAllAppsButton = allAppsButton;
1479 }
1480
1481 public View getAllAppsButton() {
1482 return mAllAppsButton;
1483 }
1484
1485 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 * Creates a view representing a shortcut.
1487 *
1488 * @param info The data structure describing the shortcut.
1489 *
1490 * @return A View inflated from R.layout.application.
1491 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001492 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001494 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 /**
1498 * Creates a view representing a shortcut inflated from the specified resource.
1499 *
1500 * @param layoutResId The id of the XML layout used to create the shortcut.
1501 * @param parent The group the shortcut belongs to.
1502 * @param info The data structure describing the shortcut.
1503 *
1504 * @return A View inflated from layoutResId.
1505 */
Adam Cohen59400422014-03-05 18:07:04 -08001506 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001507 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001508 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001510 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 return favorite;
1512 }
1513
1514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * Add a shortcut to the workspace.
1516 *
1517 * @param data The intent describing the shortcut.
1518 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001520 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 int cellY) {
1522 int[] cellXY = mTmpAddItemCellCoordinates;
1523 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527
Sunny Goyal2350bc92014-10-14 16:42:54 -07001528 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001529 if (info == null) {
1530 return;
1531 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 final View view = createShortcut(info);
1533
Adam Cohenfbba09b2011-07-18 21:43:05 -07001534 // First we check if we already know the exact location where we want to add this item.
1535 if (cellX >= 0 && cellY >= 0) {
1536 cellXY[0] = cellX;
1537 cellXY[1] = cellY;
1538 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001539
1540 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001541 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 true, null,null)) {
1543 return;
1544 }
1545 DragObject dragObject = new DragObject();
1546 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001547 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1548 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001549 return;
1550 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001552 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001554 foundCellSpan = (result != null);
1555 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001557 }
1558
1559 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001560 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 return;
1562 }
1563
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001564 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1573 int minHeight) {
1574 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001575 // We want to account for the extra amount of padding that we are adding to the widget
1576 // to ensure that it gets the full amount of space that it has requested
1577 int requiredWidth = minWidth + padding.left + padding.right;
1578 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001579 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001580 }
1581
Adam Cohen2f093b62012-04-30 18:59:53 -07001582 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1583 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1587 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001588 }
1589
Adam Cohen2f093b62012-04-30 18:59:53 -07001590 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1591 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001592 }
1593
Adam Cohen2f093b62012-04-30 18:59:53 -07001594 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1595 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001596 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001597 }
1598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001600 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001602 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 */
Adam Cohen091440a2015-03-18 14:16:05 -07001604 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001605 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1606
1607 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001608 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001609 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1610 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 }
Romain Guycbb89e42009-06-08 15:52:54 -07001612
Adam Cohen59400422014-03-05 18:07:04 -08001613 if (appWidgetInfo.isCustomWidget) {
1614 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001615 }
1616
Adam Cohen59400422014-03-05 18:07:04 -08001617 LauncherAppWidgetInfo launcherInfo;
1618 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1619 launcherInfo.spanX = info.spanX;
1620 launcherInfo.spanY = info.spanY;
1621 launcherInfo.minSpanX = info.minSpanX;
1622 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001623 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001626 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
1628 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001629 if (hostView == null) {
1630 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001631 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1632 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 } else {
1634 // The AppWidgetHostView has already been inflated and instantiated
1635 launcherInfo.hostView = hostView;
1636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001638 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001639 launcherInfo.notifyWidgetSizeChanged(this);
1640
Adam Cohen59400422014-03-05 18:07:04 -08001641 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1642 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001643
1644 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001646 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1650 @Override
1651 public void onReceive(Context context, Intent intent) {
1652 final String action = intent.getAction();
1653 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1654 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001655 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001657
Winson Chungc100e8e2011-08-09 16:02:43 -07001658 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001659 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001660 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001661 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001662 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1665 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001666 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001667 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1668 mModel.resetLoadedState(false, true);
1669 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1670 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1671 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1672 mModel.resetLoadedState(false, true);
1673 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1674 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1675 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001676 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1677 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1678 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680 }
1681 };
1682
1683 @Override
1684 public void onAttachedToWindow() {
1685 super.onAttachedToWindow();
1686
1687 // Listen for broadcasts related to user-presence
1688 final IntentFilter filter = new IntentFilter();
1689 filter.addAction(Intent.ACTION_SCREEN_OFF);
1690 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001691 // For handling managed profiles
1692 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1693 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 if (ENABLE_DEBUG_INTENTS) {
1695 filter.addAction(DebugIntents.DELETE_DATABASE);
1696 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1697 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001700 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001701 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 mVisible = true;
1703 }
1704
Adam Cohen0b395352014-06-09 22:54:36 +00001705 /**
1706 * Sets up transparent navigation and status bars in LMP.
1707 * This method is a no-op for other platform versions.
1708 */
Sunny Goyald0091012015-01-20 15:55:34 -08001709 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001710 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001711 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001712 Window window = getWindow();
1713 window.getAttributes().systemUiVisibility |=
1714 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1715 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1716 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1717 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1718 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1719 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1720 window.setStatusBarColor(Color.TRANSPARENT);
1721 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001722 }
1723 }
1724
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 @Override
1726 public void onDetachedFromWindow() {
1727 super.onDetachedFromWindow();
1728 mVisible = false;
1729
Adam Cohend113e0c2010-11-11 10:48:05 -08001730 if (mAttached) {
1731 unregisterReceiver(mReceiver);
1732 mAttached = false;
1733 }
Winson Chungb745afb2015-03-02 11:51:23 -08001734 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
1737 public void onWindowVisibilityChanged(int visibility) {
1738 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 // The following code used to be in onResume, but it turns out onResume is called when
1741 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1742 // is a more appropriate event to handle
1743 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 if (!mWorkspaceLoading) {
1745 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001746 // We want to let Launcher draw itself at least once before we force it to build
1747 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001748 // apps is nice and speedy.
1749 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001750 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001751 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 if (mStarted) return;
1753 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001754 // We delay the layer building a bit in order to give
1755 // other message processing a time to run. In particular
1756 // this avoids a delay in hiding the IME if it was
1757 // currently shown, because doing that may involve
1758 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001759 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 final ViewTreeObserver.OnDrawListener listener = this;
1761 mWorkspace.post(new Runnable() {
1762 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001763 if (mWorkspace != null &&
1764 mWorkspace.getViewTreeObserver() != null) {
1765 mWorkspace.getViewTreeObserver().
1766 removeOnDrawListener(listener);
1767 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 }
1769 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001770 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001771 }
1772 });
1773 }
1774 clearTypedText();
1775 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777
Adam Cohen091440a2015-03-18 14:16:05 -07001778 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 mHandler.removeMessages(ADVANCE_MSG);
1780 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1781 mHandler.sendMessageDelayed(msg, delay);
1782 mAutoAdvanceSentTime = System.currentTimeMillis();
1783 }
1784
Adam Cohen091440a2015-03-18 14:16:05 -07001785 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1787 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1788 mAutoAdvanceRunning = autoAdvanceRunning;
1789 if (autoAdvanceRunning) {
1790 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1791 sendAdvanceMessage(delay);
1792 } else {
1793 if (!mWidgetsToAdvance.isEmpty()) {
1794 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1795 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1796 }
1797 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001798 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 }
1800 }
1801 }
1802
1803 private final Handler mHandler = new Handler() {
1804 @Override
1805 public void handleMessage(Message msg) {
1806 if (msg.what == ADVANCE_MSG) {
1807 int i = 0;
1808 for (View key: mWidgetsToAdvance.keySet()) {
1809 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1810 final int delay = mAdvanceStagger * i;
1811 if (v instanceof Advanceable) {
1812 postDelayed(new Runnable() {
1813 public void run() {
1814 ((Advanceable) v).advance();
1815 }
1816 }, delay);
1817 }
1818 i++;
1819 }
1820 sendAdvanceMessage(mAdvanceInterval);
1821 }
1822 }
1823 };
1824
1825 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001826 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1828 if (v instanceof Advanceable) {
1829 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001830 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833 }
1834
1835 void removeWidgetToAutoAdvance(View hostView) {
1836 if (mWidgetsToAdvance.containsKey(hostView)) {
1837 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001838 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 launcherInfo.hostView = null;
1845 }
1846
Hyunyoung Song3f471442015-04-08 19:01:34 -07001847 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001848 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1849 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 }
1851
Winson Chunga6945242014-01-08 14:04:34 -08001852 public DragLayer getDragLayer() {
1853 return mDragLayer;
1854 }
1855
Winson Chungb745afb2015-03-02 11:51:23 -08001856 public AppsContainerView getAppsView() {
1857 return mAppsView;
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 public WidgetsContainerView getWidgetsView() {
1861 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001862 }
1863
Winson Chunga6945242014-01-08 14:04:34 -08001864 public Workspace getWorkspace() {
1865 return mWorkspace;
1866 }
1867
1868 public Hotseat getHotseat() {
1869 return mHotseat;
1870 }
1871
Jorim Jaggid017f882014-01-14 17:08:48 -08001872 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001873 return mOverviewPanel;
1874 }
1875
1876 public SearchDropTargetBar getSearchBar() {
1877 return mSearchDropTargetBar;
1878 }
1879
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001880 public LauncherAppWidgetHost getAppWidgetHost() {
1881 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Romain Guycbb89e42009-06-08 15:52:54 -07001883
Winson Chunga9abd0e2010-10-27 17:18:37 -07001884 public LauncherModel getModel() {
1885 return mModel;
1886 }
1887
Winson Chunga6945242014-01-08 14:04:34 -08001888 protected SharedPreferences getSharedPrefs() {
1889 return mSharedPrefs;
1890 }
1891
Bjorn Bringertc459e522013-06-07 19:36:01 +01001892 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001893 getWindow().closeAllPanels();
1894
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001895 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001896 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 @Override
1900 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001901 long startTime = 0;
1902 if (DEBUG_RESUME_TIME) {
1903 startTime = System.currentTimeMillis();
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 super.onNewIntent(intent);
1906
1907 // Close the menu
1908 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001909 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001910 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001911
Adam Cohened307df2013-10-02 09:37:31 -07001912 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1913 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1914 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001915
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 if (mWorkspace == null) {
1917 // Can be cases where mWorkspace is null, this prevents a NPE
1918 return;
1919 }
1920 Folder openFolder = mWorkspace.getOpenFolder();
1921 // In all these cases, only animate if we're already on home
1922 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001923
1924 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1925 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001927 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001929 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001930
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 closeFolder();
1932 exitSpringLoadedDragMode();
1933
1934 // If we are already on home, then just animate back to the workspace,
1935 // otherwise, just wait until onResume to set the state back to Workspace
1936 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001937 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 } else {
1939 mOnResumeState = State.WORKSPACE;
1940 }
1941
1942 final View v = getWindow().peekDecorView();
1943 if (v != null && v.getWindowToken() != null) {
1944 InputMethodManager imm = (InputMethodManager)getSystemService(
1945 INPUT_METHOD_SERVICE);
1946 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1947 }
1948
Winson Chungb745afb2015-03-02 11:51:23 -08001949 // Reset the apps view
1950 if (!alreadyOnHome && mAppsView != null) {
1951 mAppsView.scrollToTop();
1952 }
1953
Hyunyoung Song3f471442015-04-08 19:01:34 -07001954 // Reset the widgets view
1955 if (!alreadyOnHome && mWidgetsView != null) {
1956 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001957 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onHomeIntent();
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
Adam Cohened307df2013-10-02 09:37:31 -07001963
Michael Jurka447bf842013-05-15 14:52:15 +02001964 if (DEBUG_RESUME_TIME) {
1965 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Adam Cohen9211d422014-10-07 18:14:53 -07001968 if (mLauncherCallbacks != null) {
1969 mLauncherCallbacks.onNewIntent(intent);
1970 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001974 public void onRestoreInstanceState(Bundle state) {
1975 super.onRestoreInstanceState(state);
1976 for (int page: mSynchronouslyBoundPages) {
1977 mWorkspace.restoreInstanceStateForChild(page);
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
1981 @Override
1982 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001983 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001984 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1985 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001987 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
Michael Jurka883f55b2010-10-21 15:47:14 -07001989 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001990 // We close any open folder since it will not be re-opened, and we need to make sure
1991 // this state is reflected.
1992 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993
Adam Cohendcd297f2013-06-18 13:13:40 -07001994 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001995 mWaitingForResult) {
1996 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1999 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002000 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2001 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2002 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
2006 if (mFolderInfo != null && mWaitingForResult) {
2007 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2008 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2009 }
Michael Jurka946ad472010-07-09 18:05:18 -07002010
Hyunyoung Song3f471442015-04-08 19:01:34 -07002011 // Save the current widgets tray?
2012 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002013 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002014
2015 if (mLauncherCallbacks != null) {
2016 mLauncherCallbacks.onSaveInstanceState(outState);
2017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
2020 @Override
2021 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002023
Winson Chunge7a03942011-08-05 15:05:12 -07002024 // Remove all pending runnables
2025 mHandler.removeMessages(ADVANCE_MSG);
2026 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002027 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002028
Winson Chungcd2b0142011-06-08 16:02:26 -07002029 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002030 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002031
2032 // It's possible to receive onDestroy after a new Launcher activity has
2033 // been created. In this case, don't interfere with the new Launcher.
2034 if (mModel.isCurrentCallbacks(this)) {
2035 mModel.stopLoader();
2036 app.setLauncher(null);
2037 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002040 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002042 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002044 mAppWidgetHost = null;
2045
2046 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047
2048 TextKeyListener.getInstance().release();
2049
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002050 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002051 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002052
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002053 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002055 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002056 mWorkspace = null;
2057 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002058
Michael Jurka2ecf9952012-06-18 12:52:28 -07002059 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002060
2061 if (mLauncherCallbacks != null) {
2062 mLauncherCallbacks.onDestroy();
2063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065
Adam Cohena9cf38f2011-05-02 15:36:58 -07002066 public DragController getDragController() {
2067 return mDragController;
2068 }
2069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 @Override
2071 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002072 onStartForResult(requestCode);
2073 super.startActivityForResult(intent, requestCode);
2074 }
2075
2076 @Override
2077 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2078 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2079 onStartForResult(requestCode);
2080 try {
2081 super.startIntentSenderForResult(intent, requestCode,
2082 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2083 } catch (IntentSender.SendIntentException e) {
2084 throw new ActivityNotFoundException();
2085 }
2086 }
2087
2088 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002089 if (requestCode >= 0) {
2090 setWaitingForResult(true);
2091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
2093
Winson Chung70d72102011-08-12 11:24:35 -07002094 /**
2095 * Indicates that we want global search for this activity by setting the globalSearch
2096 * argument for {@link #startSearch} to true.
2097 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002099 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002101
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002102 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002103
Karl Rosaen138a0412009-04-23 19:00:21 -07002104 if (initialQuery == null) {
2105 // Use any text typed in the launcher as the initial query
2106 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 if (appSearchData == null) {
2109 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002110 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
Winson Chungadf0c182012-08-23 14:59:07 -07002112 Rect sourceBounds = new Rect();
2113 if (mSearchDropTargetBar != null) {
2114 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2115 }
Romain Guycbb89e42009-06-08 15:52:54 -07002116
Ian Parkinson047036e2014-09-01 15:40:53 +01002117 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002118 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002119 if (clearTextImmediately) {
2120 clearTypedText();
2121 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002122 }
2123
Ian Parkinson047036e2014-09-01 15:40:53 +01002124 /**
2125 * Start a text search.
2126 *
2127 * @return {@code true} if the search will start immediately, so any further keypresses
2128 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2129 * to buffer keypresses.
2130 */
2131 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002133 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2134 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2135 sourceBounds);
2136 }
2137
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002141 }
2142
2143 /**
2144 * Starts the global search activity. This code is a copied from SearchManager
2145 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002146 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002149 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2150 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2151 if (globalSearchActivity == null) {
2152 Log.w(TAG, "No global search activity found.");
2153 return;
2154 }
2155 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2156 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2157 intent.setComponent(globalSearchActivity);
2158 // Make sure that we have a Bundle to put source in
2159 if (appSearchData == null) {
2160 appSearchData = new Bundle();
2161 } else {
2162 appSearchData = new Bundle(appSearchData);
2163 }
Adam Cohen9211d422014-10-07 18:14:53 -07002164 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 if (!appSearchData.containsKey("source")) {
2166 appSearchData.putString("source", getPackageName());
2167 }
2168 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2169 if (!TextUtils.isEmpty(initialQuery)) {
2170 intent.putExtra(SearchManager.QUERY, initialQuery);
2171 }
2172 if (selectInitialQuery) {
2173 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2174 }
2175 intent.setSourceBounds(sourceBounds);
2176 try {
2177 startActivity(intent);
2178 } catch (ActivityNotFoundException ex) {
2179 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 }
2182
Yura4f93ec62014-02-04 14:15:21 +00002183 public boolean isOnCustomContent() {
2184 return mWorkspace.isOnOrMovingToCustomContent();
2185 }
2186
Winson Chung70d72102011-08-12 11:24:35 -07002187 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002188 public boolean onPrepareOptionsMenu(Menu menu) {
2189 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002190 if (!isOnCustomContent()) {
2191 // Close any open folders
2192 closeFolder();
2193 // Stop resizing any widgets
2194 mWorkspace.exitWidgetResizeMode();
2195 if (!mWorkspace.isInOverviewMode()) {
2196 // Show the overview mode
2197 showOverviewMode(true);
2198 } else {
2199 showWorkspace(true);
2200 }
Winson Chunge0298742014-01-17 12:03:00 -08002201 }
Adam Cohen9211d422014-10-07 18:14:53 -07002202 if (mLauncherCallbacks != null) {
2203 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2204 }
2205
Michael Jurkab94f3f82013-09-11 18:08:54 +02002206 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002209 @Override
2210 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002211 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002212 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002213 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002214 }
2215
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 public boolean isWorkspaceLocked() {
2217 return mWorkspaceLoading || mWaitingForResult;
2218 }
2219
Adam Cohen517a7f52014-03-01 12:12:59 -08002220 public boolean isWorkspaceLoading() {
2221 return mWorkspaceLoading;
2222 }
2223
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002224 private void setWorkspaceLoading(boolean value) {
2225 boolean isLocked = isWorkspaceLocked();
2226 mWorkspaceLoading = value;
2227 if (isLocked != isWorkspaceLocked()) {
2228 onWorkspaceLockedChanged();
2229 }
2230 }
2231
2232 private void setWaitingForResult(boolean value) {
2233 boolean isLocked = isWorkspaceLocked();
2234 mWaitingForResult = value;
2235 if (isLocked != isWorkspaceLocked()) {
2236 onWorkspaceLockedChanged();
2237 }
2238 }
2239
Adam Cohen9211d422014-10-07 18:14:53 -07002240 protected void onWorkspaceLockedChanged() {
2241 if (mLauncherCallbacks != null) {
2242 mLauncherCallbacks.onWorkspaceLockedChanged();
2243 }
2244 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002245
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2250 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002251 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002254
Sunny Goyale6b63a32015-01-16 16:14:29 -08002255 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002256 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002257 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2258 }
2259
Sunny Goyale6b63a32015-01-16 16:14:29 -08002260 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002261 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2262 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002263 if (appWidgetInfo.configure != null) {
2264 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002265 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002266
Bjorn Bringert7984c942009-12-09 15:38:25 +00002267 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002268 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2269 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002272 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002273 Runnable onComplete = new Runnable() {
2274 @Override
2275 public void run() {
2276 // Exit spring loaded mode if necessary after adding the widget
2277 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2278 null);
2279 }
2280 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002282 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002283 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 }
2285 }
Romain Guycbb89e42009-06-08 15:52:54 -07002286
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002287 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002288 // Close any folders that may be open.
2289 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002290 mWorkspace.moveToCustomContentScreen(animate);
2291 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002292
2293 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2294 int[] cell, int spanX, int spanY) {
2295 switch (info.itemType) {
2296 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2297 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2298 int span[] = new int[2];
2299 span[0] = spanX;
2300 span[1] = spanY;
2301 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2302 container, screenId, cell, span);
2303 break;
2304 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2305 processShortcutFromDrop(info.componentName, container, screenId, cell);
2306 break;
2307 default:
2308 throw new IllegalStateException("Unknown item type: " + info.itemType);
2309 }
2310 }
2311
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 /**
2313 * Process a shortcut drop.
2314 *
2315 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002319 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2320 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002324 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325
2326 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 mPendingAddInfo.cellX = cell[0];
2328 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002330
2331 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2332 createShortcutIntent.setComponent(componentName);
2333 processShortcut(createShortcutIntent);
2334 }
2335
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 /**
2337 * Process a widget drop.
2338 *
2339 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002343 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2344 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002348 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002349 mPendingAddInfo.minSpanX = info.minSpanX;
2350 mPendingAddInfo.minSpanY = info.minSpanY;
2351
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.cellX = cell[0];
2354 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002356 if (span != null) {
2357 mPendingAddInfo.spanX = span[0];
2358 mPendingAddInfo.spanY = span[1];
2359 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360
Adam Cohened66b2b2012-01-23 17:28:51 -08002361 AppWidgetHostView hostView = info.boundWidget;
2362 int appWidgetId;
2363 if (hostView != null) {
2364 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002365 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002366 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002367 // In this case, we either need to start an activity to get permission to bind
2368 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002369 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002370 Bundle options = info.bindOptions;
2371
Sunny Goyalffe83f12014-08-14 17:39:34 -07002372 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2373 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002374 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002375 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002376 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002378 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2379 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2380 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002381 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2382 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002383 // TODO: we need to make sure that this accounts for the options bundle.
2384 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002385 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2386 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002387 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002388 }
2389
Joe Onoratodeb98af2010-02-19 14:59:39 -08002390 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002391 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 }
2393
Winson Chung24ab2f12010-09-16 14:10:47 -07002394 void processWallpaper(Intent intent) {
2395 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2396 }
2397
Adam Cohendcd297f2013-06-18 13:13:40 -07002398 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002399 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002400 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 folderInfo.title = getText(R.string.folder_name);
2402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002404 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2405 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002406 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407
2408 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 FolderIcon newFolder =
2410 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002413 // Force measure the new folder icon
2414 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2415 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002416 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
Romain Guycbb89e42009-06-08 15:52:54 -07002418
Joe Onorato9c1289c2009-08-17 11:03:03 -04002419 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002420 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 }
2422
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002423 /**
2424 * Registers various content observers. The current implementation registers
2425 * only a favorites observer to keep track of the favorites applications.
2426 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002427 private void registerContentObservers() {
2428 ContentResolver resolver = getContentResolver();
2429 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2430 true, mWidgetObserver);
2431 }
2432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 @Override
2434 public boolean dispatchKeyEvent(KeyEvent event) {
2435 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2436 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002438 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002439 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002440 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002441 dumpState();
2442 return true;
2443 }
2444 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002445 }
2446 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2447 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 return true;
2450 }
2451 }
2452
2453 return super.dispatchKeyEvent(event);
2454 }
2455
Joe Onorato88ec0992009-11-19 13:16:06 -08002456 @Override
2457 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002458 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2459 return;
2460 }
2461
Winson Chung3d9490a2015-03-24 17:38:42 -07002462 LauncherAccessibilityDelegate delegate =
2463 LauncherAppState.getInstance().getAccessibilityDelegate();
2464 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002465 return;
2466 }
2467
Winson Chungb745afb2015-03-02 11:51:23 -08002468 if (isAppsViewVisible()) {
2469 showWorkspace(true);
2470 } else if (isWidgetsViewVisible()) {
2471 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002472 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002473 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002475 Folder openFolder = mWorkspace.getOpenFolder();
2476 if (openFolder.isEditingName()) {
2477 openFolder.dismissEditingName();
2478 } else {
2479 closeFolder();
2480 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002481 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002482 mWorkspace.exitWidgetResizeMode();
2483
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 // Back button is a no-op here, but give at least some feedback for the button press
2485 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002486 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002490 * Re-listen when widgets are reset.
2491 */
Adam Cohen091440a2015-03-18 14:16:05 -07002492 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002493 if (mAppWidgetHost != null) {
2494 mAppWidgetHost.startListening();
2495 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002496 }
2497
2498 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 * Launches the intent referred by the clicked shortcut.
2500 *
2501 * @param v The view representing the clicked shortcut.
2502 */
2503 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002504 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2505 // view has detached (it's possible for this to happen if the view is removed mid touch).
2506 if (v.getWindowToken() == null) {
2507 return;
2508 }
2509
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002510 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002511 return;
2512 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002513
Adam Cohen1697b792013-09-17 19:08:21 -07002514 if (v instanceof Workspace) {
2515 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002516 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 }
2518 return;
2519 }
2520
2521 if (v instanceof CellLayout) {
2522 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002523 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002524 }
2525 }
2526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002528 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002529 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002531 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002532 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002533 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002534 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002536 } else if (tag instanceof AppInfo) {
2537 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002538 } else if (tag instanceof LauncherAppWidgetInfo) {
2539 if (v instanceof PendingAppWidgetHostView) {
2540 onClickPendingWidget((PendingAppWidgetHostView) v);
2541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 }
2543 }
2544
Sunny Goyal508da152014-08-14 10:53:27 -07002545 public void onClickPagedViewIcon(View v) {
2546 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002547 if (mLauncherCallbacks != null) {
2548 mLauncherCallbacks.onClickPagedViewIcon(v);
2549 }
Sunny Goyal508da152014-08-14 10:53:27 -07002550 }
2551
Michael Jurka0e260592010-06-30 17:07:39 -07002552 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002553 return false;
2554 }
2555
Michael Jurkaaf442092010-06-10 17:01:57 -07002556 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002557 * Event handler for the app widget view which has not fully restored.
2558 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002559 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002560 if (mIsSafeModeEnabled) {
2561 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2562 return;
2563 }
2564
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002566 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002567 int widgetId = info.appWidgetId;
2568 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2569 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002570 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2571 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002572 mPendingAddInfo.copyFrom(info);
2573 mPendingAddWidgetId = widgetId;
2574
Sunny Goyalffe83f12014-08-14 17:39:34 -07002575 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2576 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002577 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 } else if (info.installProgress < 0) {
2579 // The install has not been queued
2580 final String packageName = info.providerName.getPackageName();
2581 showBrokenAppInstallDialog(packageName,
2582 new DialogInterface.OnClickListener() {
2583 public void onClick(DialogInterface dialog, int id) {
2584 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2585 }
2586 });
2587 } else {
2588 // Download has started.
2589 final String packageName = info.providerName.getPackageName();
2590 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002591 }
2592 }
2593
2594 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002595 * Event handler for the "grid" button that appears on the home screen, which
2596 * enters all apps mode.
2597 *
2598 * @param v The view that was clicked.
2599 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 protected void onClickAllAppsButton(View v) {
2601 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002602 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 showWorkspace(true);
2604 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002605 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 }
2607 }
2608
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002609 private void showBrokenAppInstallDialog(final String packageName,
2610 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002611 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 .setTitle(R.string.abandoned_promises_title)
2613 .setMessage(R.string.abandoned_promise_explanation)
2614 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2615 .setNeutralButton(R.string.abandoned_clean_this,
2616 new DialogInterface.OnClickListener() {
2617 public void onClick(DialogInterface dialog, int id) {
2618 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2619 mWorkspace.removeAbandonedPromise(packageName, user);
2620 }
2621 })
2622 .create().show();
2623 return;
2624 }
2625
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002626 /**
2627 * Event handler for an app shortcut click.
2628 *
2629 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2630 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002631 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2633 Object tag = v.getTag();
2634 if (!(tag instanceof ShortcutInfo)) {
2635 throw new IllegalArgumentException("Input must be a Shortcut");
2636 }
2637
2638 // Open shortcut
2639 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002640
2641 if (shortcut.isDisabled != 0) {
2642 int error = R.string.activity_not_available;
2643 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2644 error = R.string.safemode_shortcut_error;
2645 }
2646 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2647 return;
2648 }
2649
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 final Intent intent = shortcut.intent;
2651
2652 // Check for special shortcuts
2653 if (intent.getComponent() != null) {
2654 final String shortcutClass = intent.getComponent().getClassName();
2655
2656 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2657 MemoryDumpActivity.startDump(this);
2658 return;
2659 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2660 toggleShowWeightWatcher();
2661 return;
2662 }
2663 }
2664
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002666 if ((v instanceof BubbleTextView)
2667 && shortcut.isPromise()
2668 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002669 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002670 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 new DialogInterface.OnClickListener() {
2672 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002673 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 return;
2677 }
2678
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002679 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002680 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002681
2682 if (mLauncherCallbacks != null) {
2683 mLauncherCallbacks.onClickAppShortcut(v);
2684 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 }
2686
Adam Cohen091440a2015-03-18 14:16:05 -07002687 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002689 final ShortcutInfo shortcut;
2690 final Intent intent;
2691 if (tag instanceof ShortcutInfo) {
2692 shortcut = (ShortcutInfo) tag;
2693 intent = shortcut.intent;
2694 int[] pos = new int[2];
2695 v.getLocationOnScreen(pos);
2696 intent.setSourceBounds(new Rect(pos[0], pos[1],
2697 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002698
Sunny Goyal508da152014-08-14 10:53:27 -07002699 } else if (tag instanceof AppInfo) {
2700 shortcut = null;
2701 intent = ((AppInfo) tag).intent;
2702 } else {
2703 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2704 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705
2706 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 mStats.recordLaunch(intent, shortcut);
2708
2709 if (success && v instanceof BubbleTextView) {
2710 mWaitingForResume = (BubbleTextView) v;
2711 mWaitingForResume.setStayPressed(true);
2712 }
2713 }
2714
2715 /**
2716 * Event handler for a folder icon click.
2717 *
2718 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2719 */
2720 protected void onClickFolderIcon(View v) {
2721 if (LOGD) Log.d(TAG, "onClickFolder");
2722 if (!(v instanceof FolderIcon)){
2723 throw new IllegalArgumentException("Input must be a FolderIcon");
2724 }
2725
2726 FolderIcon folderIcon = (FolderIcon) v;
2727 final FolderInfo info = folderIcon.getFolderInfo();
2728 Folder openFolder = mWorkspace.getFolderForTag(info);
2729
2730 // If the folder info reports that the associated folder is open, then verify that
2731 // it is actually opened. There have been a few instances where this gets out of sync.
2732 if (info.opened && openFolder == null) {
2733 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2734 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2735 info.opened = false;
2736 }
2737
2738 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2739 // Close any open folder
2740 closeFolder();
2741 // Open the requested folder
2742 openFolder(folderIcon);
2743 } else {
2744 // Find the open folder...
2745 int folderScreen;
2746 if (openFolder != null) {
2747 folderScreen = mWorkspace.getPageForView(openFolder);
2748 // .. and close it
2749 closeFolder(openFolder);
2750 if (folderScreen != mWorkspace.getCurrentPage()) {
2751 // Close any folder open on the current screen
2752 closeFolder();
2753 // Pull the folder onto this screen
2754 openFolder(folderIcon);
2755 }
2756 }
2757 }
Adam Cohen9211d422014-10-07 18:14:53 -07002758
2759 if (mLauncherCallbacks != null) {
2760 mLauncherCallbacks.onClickFolderIcon(v);
2761 }
Michael Jurka5130e402011-10-13 04:55:35 -07002762 }
2763
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002764 /**
2765 * Event handler for the (Add) Widgets button that appears after a long press
2766 * on the home screen.
2767 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002768 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002770 if (mIsSafeModeEnabled) {
2771 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2772 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002773 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002774 if (mLauncherCallbacks != null) {
2775 mLauncherCallbacks.onClickAddWidgetButton(view);
2776 }
Adam Cohen9211d422014-10-07 18:14:53 -07002777 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002778 }
2779
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002780 /**
2781 * Event handler for the wallpaper picker button that appears after a long press
2782 * on the home screen.
2783 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002784 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002785 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002786 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2787 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002788
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onClickWallpaperPicker(v);
2791 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 }
2793
2794 /**
2795 * Event handler for a click on the settings button that appears after a long press
2796 * on the home screen.
2797 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002798 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002799 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onClickSettingsButton(v);
2802 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 }
2804
Michael Jurka5130e402011-10-13 04:55:35 -07002805 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002806 // Provide the same haptic feedback that the system offers for virtual keys.
2807 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002808 }
2809
Adam Cohen61f560d2013-09-30 15:58:20 -07002810 public void performHapticFeedbackOnTouchDown(View v) {
2811 // Provide the same haptic feedback that the system offers for virtual keys.
2812 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2813 }
2814
2815 public View.OnTouchListener getHapticFeedbackTouchListener() {
2816 if (mHapticFeedbackTouchListener == null) {
2817 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2818 @Override
2819 public boolean onTouch(View v, MotionEvent event) {
2820 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2821 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2822 }
2823 return false;
2824 }
2825 };
2826 }
2827 return mHapticFeedbackTouchListener;
2828 }
2829
Adam Cohen9211d422014-10-07 18:14:53 -07002830 public void onDragStarted(View view) {
2831 if (isOnCustomContent()) {
2832 // Custom content screen doesn't participate in drag and drop. If on custom
2833 // content screen, move to default.
2834 moveWorkspaceToDefaultScreen();
2835 }
2836
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onDragStarted(view);
2839 }
2840 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002841
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002842 /**
2843 * Called when the user stops interacting with the launcher.
2844 * This implies that the user is now on the homescreen and is not doing housekeeping.
2845 */
Adam Cohen9211d422014-10-07 18:14:53 -07002846 protected void onInteractionEnd() {
2847 if (mLauncherCallbacks != null) {
2848 mLauncherCallbacks.onInteractionEnd();
2849 }
2850 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002851
2852 /**
2853 * Called when the user starts interacting with the launcher.
2854 * The possible interactions are:
2855 * - open all apps
2856 * - reorder an app shortcut, or a widget
2857 * - open the overview mode.
2858 * This is a good time to stop doing things that only make sense
2859 * when the user is on the homescreen and not doing housekeeping.
2860 */
Adam Cohen9211d422014-10-07 18:14:53 -07002861 protected void onInteractionBegin() {
2862 if (mLauncherCallbacks != null) {
2863 mLauncherCallbacks.onInteractionBegin();
2864 }
2865 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002866
Kenny Guyf07af7b2014-07-31 11:39:16 +01002867 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002868 try {
2869 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002870 launcherApps.showAppDetailsForProfile(componentName, user);
2871 } catch (SecurityException e) {
2872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2873 Log.e(TAG, "Launcher does not have permission to launch settings");
2874 } catch (ActivityNotFoundException e) {
2875 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2876 Log.e(TAG, "Unable to launch settings");
2877 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002878 }
2879
Michael Jurka1e2f4652013-07-08 18:03:46 -07002880 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002881 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2882 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002883 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 // System applications cannot be installed. For now, show a toast explaining that.
2885 // We may give them the option of disabling apps this way.
2886 int messageId = R.string.uninstall_system_app_text;
2887 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002888 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002889 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 String packageName = componentName.getPackageName();
2891 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002892 Intent intent = new Intent(
2893 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002894 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2895 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002896 if (user != null) {
2897 user.addToIntent(intent, Intent.EXTRA_USER);
2898 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002902 }
2903
Michael Jurka86a720e2012-05-09 11:23:23 -07002904 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002905 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002907 // Only launch using the new animation if the shortcut has not opted out (this is a
2908 // private contract between launcher and may be ignored in the future).
2909 boolean useLaunchAnimation = (v != null) &&
2910 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002911 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2912 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002913
Kenny Guy1317e2d2014-05-08 18:52:50 +01002914 UserHandleCompat user = null;
2915 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2916 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2917 user = userManager.getUserForSerialNumber(serialNumber);
2918 }
2919
2920 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002921 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002922 ActivityOptions opts = null;
2923 if (sClipRevealMethod != null) {
2924 // TODO: call method directly when Launcher3 can depend on M APIs
2925 int left = 0, top = 0;
2926 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2927 if (v instanceof TextView) {
2928 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002929 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2930 if (icon != null) {
2931 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002932 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002933 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002934 width = bounds.width();
2935 height = bounds.height();
2936 }
2937 }
2938 try {
2939 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2940 left, top, width, height);
2941 } catch (IllegalAccessException e) {
2942 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2943 sClipRevealMethod = null;
2944 } catch (InvocationTargetException e) {
2945 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2946 sClipRevealMethod = null;
2947 }
2948 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002949 if (opts == null && !Utilities.isLmpOrAbove()) {
2950 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002951 v.getMeasuredWidth(), v.getMeasuredHeight());
2952 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002953 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002954 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002955
2956 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2957 // Could be launching some bookkeeping activity
2958 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002959 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002960 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002961 launcherApps.startActivityForProfile(intent.getComponent(), user,
2962 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002963 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002964 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 } catch (SecurityException e) {
2966 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002967 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002969 "or use the exported attribute for this activity. "
2970 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002972 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002974
Michael Jurka86a720e2012-05-09 11:23:23 -07002975 boolean startActivitySafely(View v, Intent intent, Object tag) {
2976 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002977 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2978 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2979 return false;
2980 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002981 try {
2982 success = startActivity(v, intent, tag);
2983 } catch (ActivityNotFoundException e) {
2984 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2985 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2986 }
2987 return success;
2988 }
2989
Adam Cohen268c4752012-06-06 17:47:33 -07002990 /**
2991 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2992 * in the DragLayer in the exact absolute location of the original FolderIcon.
2993 */
2994 private void copyFolderIconToImage(FolderIcon fi) {
2995 final int width = fi.getMeasuredWidth();
2996 final int height = fi.getMeasuredHeight();
2997
2998 // Lazy load ImageView, Bitmap and Canvas
2999 if (mFolderIconImageView == null) {
3000 mFolderIconImageView = new ImageView(this);
3001 }
3002 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3003 mFolderIconBitmap.getHeight() != height) {
3004 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3005 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3006 }
3007
3008 DragLayer.LayoutParams lp;
3009 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3010 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3011 } else {
3012 lp = new DragLayer.LayoutParams(width, height);
3013 }
3014
Adam Cohen307fe232012-08-16 17:55:58 -07003015 // The layout from which the folder is being opened may be scaled, adjust the starting
3016 // view size by this scale factor.
3017 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003018 lp.customPosition = true;
3019 lp.x = mRectForFolderAnimation.left;
3020 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003021 lp.width = (int) (scale * width);
3022 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003023
3024 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3025 fi.draw(mFolderIconCanvas);
3026 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003027 if (fi.getFolder() != null) {
3028 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3029 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003030 }
Adam Cohen268c4752012-06-06 17:47:33 -07003031 // Just in case this image view is still in the drag layer from a previous animation,
3032 // we remove it and re-add it.
3033 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3034 mDragLayer.removeView(mFolderIconImageView);
3035 }
3036 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 if (fi.getFolder() != null) {
3038 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003039 }
Adam Cohen268c4752012-06-06 17:47:33 -07003040 }
3041
Adam Cohen2801caf2011-05-13 20:57:39 -07003042 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003043 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3045 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3046 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3047
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 FolderInfo info = (FolderInfo) fi.getTag();
3049 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3050 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003051 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3052 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003053 }
3054
Adam Cohen268c4752012-06-06 17:47:33 -07003055 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3056 copyFolderIconToImage(fi);
3057 fi.setVisibility(View.INVISIBLE);
3058
Michael Jurka2ecf9952012-06-18 12:52:28 -07003059 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003060 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003061 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003062 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3063 }
3064 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003065 oa.start();
3066 }
3067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003069 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003070 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3071 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3072 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003075
Adam Cohen268c4752012-06-06 17:47:33 -07003076 // We remove and re-draw the FolderIcon in-case it has changed
3077 mDragLayer.removeView(mFolderIconImageView);
3078 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003079 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003080 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003081 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003082 oa.addListener(new AnimatorListenerAdapter() {
3083 @Override
3084 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003085 if (cl != null) {
3086 cl.clearFolderLeaveBehind();
3087 // Remove the ImageView copy of the FolderIcon and make the original visible.
3088 mDragLayer.removeView(mFolderIconImageView);
3089 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003090 }
3091 }
3092 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003093 oa.start();
3094 }
3095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003097 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 * is animated relative to the specified View. If the View is null, no animation
3099 * is played.
3100 *
3101 * @param folderInfo The FolderInfo describing the folder to open.
3102 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003103 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003104 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003105 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3106 if (openFolder != null && openFolder != folder) {
3107 // Close any open folder before opening a folder.
3108 closeFolder();
3109 }
3110
Adam Cohena9cf38f2011-05-02 15:36:58 -07003111 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114
Sunny Goyalf4066152015-04-15 09:42:19 -07003115 // While the folder is open, the position of the icon cannot change.
3116 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3117
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 // Just verify that the folder hasn't already been added to the DragLayer.
3119 // There was a one-off crash where the folder had a parent already.
3120 if (folder.getParent() == null) {
3121 mDragLayer.addView(folder);
3122 mDragController.addDropTarget((DropTarget) folder);
3123 } else {
3124 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3125 folder.getParent() + ").");
3126 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003127 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003128 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003129
3130 // Notify the accessibility manager that this folder "window" has appeared and occluded
3131 // the workspace items
3132 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3133 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003134 }
3135
3136 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003137 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003138 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003139 if (folder.isEditingName()) {
3140 folder.dismissEditingName();
3141 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 closeFolder(folder);
3143 }
3144 }
3145
3146 void closeFolder(Folder folder) {
3147 folder.getInfo().opened = false;
3148
3149 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3150 if (parent != null) {
3151 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3152 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003153 if (fi != null) {
3154 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3155 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003156 }
3157 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003158
3159 // Notify the accessibility manager that this folder "window" has disappeard and no
3160 // longer occludeds the workspace items
3161 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003165 if (!isDraggingEnabled()) return false;
3166 if (isWorkspaceLocked()) return false;
3167 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168
Adam Cohen1697b792013-09-17 19:08:21 -07003169 if (v instanceof Workspace) {
3170 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003171
3172 if (!mWorkspace.isTouchActive()) {
3173 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003174 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3175 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3176 return true;
3177 } else {
3178 return false;
3179 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003180 } else {
3181 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003182 }
Adam Cohen1697b792013-09-17 19:08:21 -07003183 }
3184
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003185 CellLayout.CellInfo longClickCellInfo = null;
3186 View itemUnderLongClick = null;
3187 if (v.getTag() instanceof ItemInfo) {
3188 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003189 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003190 itemUnderLongClick = longClickCellInfo.cell;
3191 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
3193
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 // The hotseat touch handling does not go through Workspace, and we always allow long press
3195 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003196 final boolean inHotseat = isHotseatLayout(v);
3197 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003199 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003200 // User long pressed on empty space
3201 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3202 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003203 if (mWorkspace.isInOverviewMode()) {
3204 mWorkspace.startReordering(v);
3205 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003206 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003209 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3210 mHotseat.getOrderInHotseat(
3211 longClickCellInfo.cellX,
3212 longClickCellInfo.cellY));
3213 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003215 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 }
3217 }
3218 }
3219 return true;
3220 }
3221
Winson Chung3d503fb2011-07-13 17:25:49 -07003222 boolean isHotseatLayout(View layout) {
3223 return mHotseat != null && layout != null &&
3224 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3225 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003226
Winson Chung3d503fb2011-07-13 17:25:49 -07003227 /**
3228 * Returns the CellLayout of the specified container at the specified screen.
3229 */
Sunny Goyal92820592015-03-02 11:31:35 -08003230 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3232 if (mHotseat != null) {
3233 return mHotseat.getLayout();
3234 } else {
3235 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003236 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003238 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003239 }
3240 }
3241
Winson Chungb745afb2015-03-02 11:51:23 -08003242 /**
3243 * For overridden classes.
3244 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003245 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003246 return isAppsViewVisible();
3247 }
3248
3249 public boolean isAppsViewVisible() {
3250 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3251 }
3252
3253 public boolean isWidgetsViewVisible() {
3254 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003255 }
3256
Craig Mautner360310b2012-10-26 15:13:08 -07003257 private void setWorkspaceBackground(boolean workspace) {
3258 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003259 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003260 }
3261
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003262 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003263 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3264 int curflags = getWindow().getAttributes().flags
3265 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3266 if (wpflags != curflags) {
3267 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3268 }
Craig Mautner360310b2012-10-26 15:13:08 -07003269 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003270 }
3271
Michael Jurkae326f182011-11-21 14:05:46 -08003272 @Override
3273 public void onTrimMemory(int level) {
3274 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003275 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3276 // The widget preview db can result in holding onto over
3277 // 3MB of memory for caching which isn't necessary.
3278 SQLiteDatabase.releaseMemory();
3279
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003280 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003281 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003282 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003283 if (mLauncherCallbacks != null) {
3284 mLauncherCallbacks.onTrimMemory(level);
3285 }
Michael Jurkae326f182011-11-21 14:05:46 -08003286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 @Override
3289 public void onStateTransitionHideSearchBar() {
3290 // Hide the search bar
3291 if (mSearchDropTargetBar != null) {
3292 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3293 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003294 }
3295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003297 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3298 true);
Adam Cohened307df2013-10-02 09:37:31 -07003299 }
3300
Winson Chungdc61c4d2015-04-20 18:26:57 -07003301 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3302 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3303 onCompleteRunnable, true);
3304 }
3305
3306 protected void showWorkspace(int snapToPage, boolean animated) {
3307 showWorkspace(snapToPage, animated, null, true);
3308 }
3309
3310 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3311 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003312 boolean changed = mState != State.WORKSPACE ||
3313 mWorkspace.getState() != Workspace.State.NORMAL;
3314 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003315 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003316 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003317 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003318 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003319
Winson Chungc7d2b602012-05-16 17:02:20 -07003320 // Show the search bar (only animate if we were showing the drop target bar in spring
3321 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003322 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003323 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003324 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003325
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 // Set focus to the AppsCustomize button
3327 if (mAllAppsButton != null) {
3328 mAllAppsButton.requestFocus();
3329 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003330 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003331
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003332 // Change the state *after* we've called all the transition code
3333 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003334
Winson Chung5fb63472011-02-02 17:03:37 -08003335 // Resume the auto-advance of widgets
3336 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003337 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003338
Winson Chung0f785722015-04-08 10:27:49 -07003339 if (changed) {
3340 // Send an accessibility event to announce the context change
3341 getWindow().getDecorView()
3342 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003343
Winson Chung0f785722015-04-08 10:27:49 -07003344 onWorkspaceShown(animated);
3345 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003346 }
3347
Adam Cohened307df2013-10-02 09:37:31 -07003348 void showOverviewMode(boolean animated) {
3349 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003350 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003351 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3352 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003353 mState = State.WORKSPACE;
3354 onWorkspaceShown(animated);
3355 }
3356
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003357 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003358 }
3359
Winson Chungb745afb2015-03-02 11:51:23 -08003360 /**
3361 * Shows the apps view.
3362 */
3363 void showAppsView(boolean animated, boolean resetListToTop) {
3364 if (resetListToTop) {
3365 mAppsView.scrollToTop();
3366 }
3367 showAppsOrWidgets(animated, State.APPS);
3368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the widgets view.
3372 */
3373 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003374 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003375 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003377 }
Winson Chungb745afb2015-03-02 11:51:23 -08003378 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003379
3380 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003381 @Override
3382 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003383 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003384 }
3385 });
Winson Chungb745afb2015-03-02 11:51:23 -08003386 }
3387
3388 /**
3389 * Sets up the transition to show the apps/widgets view.
3390 */
3391 private void showAppsOrWidgets(boolean animated, State toState) {
3392 if (mState != State.WORKSPACE) return;
3393 if (toState != State.APPS && toState != State.WIDGETS) return;
3394
3395 if (toState == State.APPS) {
3396 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003397 if (mLauncherCallbacks != null) {
3398 mLauncherCallbacks.onAllAppsShown();
3399 }
Winson Chungb745afb2015-03-02 11:51:23 -08003400 } else {
3401 mStateTransitionAnimation.startAnimationToWidgets(animated);
3402 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003403
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003405 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406
3407 // Pause the auto-advance of widgets until we are out of AllApps
3408 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003409 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410 closeFolder();
3411
3412 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003413 getWindow().getDecorView()
3414 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415 }
3416
Hyunyoung Song3f471442015-04-08 19:01:34 -07003417 public void enterSpringLoadedDragMode() {
3418 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3419 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003420 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3421 mState == State.WIDGETS_SPRING_LOADED) {
3422 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003423 }
Winson Chungb745afb2015-03-02 11:51:23 -08003424
3425 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003426 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3427 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003428 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003429 }
Adam Cohen7777d962011-08-18 18:58:38 -07003430
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003432 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003433 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003434
Winson Chunge7a03942011-08-05 15:05:12 -07003435 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003436 @Override
3437 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003438 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3440 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003441 // Before we show workspace, hide all apps again because
3442 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3443 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003444 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003445 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003446 } else {
3447 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003448 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003449 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003450 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003451 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452
Michael Jurkad3ef3062010-11-23 16:23:58 -08003453 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003454 if (mState == State.APPS_SPRING_LOADED) {
3455 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3456 mState = State.APPS;
3457 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3458 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3459 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003460 }
3461 // Otherwise, we are not in spring loaded mode, so don't do anything.
3462 }
3463
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464 void lockAllApps() {
3465 // TODO
3466 }
3467
3468 void unlockAllApps() {
3469 // TODO
3470 }
3471
Sunny Goyal594d76d2014-11-06 10:12:54 -08003472 protected void disableVoiceButtonProxy(boolean disable) {
3473 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003474 }
3475
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003476 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003477 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3478 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003479 }
3480
Adam Cohen24ce0b32014-01-14 16:18:14 -08003481 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003482 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3483 if (searchProvider == null) {
3484 return null;
3485 }
3486
3487 Bundle opts = new Bundle();
3488 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003489 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003490
3491 SharedPreferences sp = getSharedPreferences(
3492 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3493 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003494 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003495 if (!searchProvider.provider.flattenToString().equals(
3496 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003497 || (widgetInfo == null)
3498 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003499 // A valid widget is not already bound.
3500 if (widgetId > -1) {
3501 mAppWidgetHost.deleteAppWidgetId(widgetId);
3502 widgetId = -1;
3503 }
3504
3505 // Try to bind a new widget
3506 widgetId = mAppWidgetHost.allocateAppWidgetId();
3507
3508 if (!AppWidgetManagerCompat.getInstance(this)
3509 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3510 mAppWidgetHost.deleteAppWidgetId(widgetId);
3511 widgetId = -1;
3512 }
3513
3514 sp.edit()
3515 .putInt(QSB_WIDGET_ID, widgetId)
3516 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3517 .commit();
3518 }
3519
3520 if (widgetId != -1) {
3521 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3522 mQsb.updateAppWidgetOptions(opts);
3523 mQsb.setPadding(0, 0, 0, 0);
3524 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003525 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003526 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003527 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003528 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003529 }
3530
Sunny Goyal22235bc2015-04-03 09:23:43 -07003531 private void reinflateQSBIfNecessary() {
3532 if (mQsb instanceof LauncherAppWidgetHostView &&
3533 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3534 mSearchDropTargetBar.removeView(mQsb);
3535 mQsb = null;
3536 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3537 }
3538 }
3539
Michael Jurkad7c28052012-04-27 15:43:36 -07003540 @Override
3541 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003542 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003543 final List<CharSequence> text = event.getText();
3544 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003545 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003546 if (mState == State.APPS) {
3547 text.add("Apps");
3548 } else if (mState == State.WIDGETS) {
3549 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003550 } else {
3551 text.add(getString(R.string.all_apps_home_button_label));
3552 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003553 return result;
3554 }
3555
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003556 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003557 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003558 */
Adam Cohen091440a2015-03-18 14:16:05 -07003559 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003560 @Override
3561 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003562 closeSystemDialogs();
3563 }
3564 }
3565
3566 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003567 * Receives notifications whenever the appwidgets are reset.
3568 */
3569 private class AppWidgetResetObserver extends ContentObserver {
3570 public AppWidgetResetObserver() {
3571 super(new Handler());
3572 }
3573
3574 @Override
3575 public void onChange(boolean selfChange) {
3576 onAppWidgetReset();
3577 }
3578 }
3579
3580 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003581 * If the activity is currently paused, signal that we need to run the passed Runnable
3582 * in onResume.
3583 *
3584 * This needs to be called from incoming places where resources might have been loaded
3585 * while we are paused. That is becaues the Configuration might be wrong
3586 * when we're not running, and if it comes back to what it was when we
3587 * were paused, we are not restarted.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 *
3591 * @return true if we are currently paused. The caller might be able to
3592 * skip some work in that case since we will come back again.
3593 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003594 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 if (mPaused) {
3596 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003597 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003598 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003599 }
3600 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003601 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 return true;
3603 } else {
3604 return false;
3605 }
3606 }
3607
Michael Jurkac402cd92013-05-20 15:49:32 +02003608 private boolean waitUntilResume(Runnable run) {
3609 return waitUntilResume(run, false);
3610 }
3611
Michael Jurka1e2f4652013-07-08 18:03:46 -07003612 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003613 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 }
3615
Michael Jurka7607c2f2013-04-03 14:33:19 -07003616 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003617 * If the activity is currently paused, signal that we need to re-run the loader
3618 * in onResume.
3619 *
3620 * This needs to be called from incoming places where resources might have been loaded
3621 * while we are paused. That is becaues the Configuration might be wrong
3622 * when we're not running, and if it comes back to what it was when we
3623 * were paused, we are not restarted.
3624 *
3625 * Implementation of the method from LauncherModel.Callbacks.
3626 *
3627 * @return true if we are currently paused. The caller might be able to
3628 * skip some work in that case since we will come back again.
3629 */
3630 public boolean setLoadOnResume() {
3631 if (mPaused) {
3632 Log.i(TAG, "setLoadOnResume");
3633 mOnResumeNeedsLoad = true;
3634 return true;
3635 } else {
3636 return false;
3637 }
3638 }
3639
3640 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003641 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003642 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003644 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003645 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003646 } else {
3647 return SCREEN_COUNT / 2;
3648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003650
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 /**
3652 * Refreshes the shortcuts shown on the workspace.
3653 *
3654 * Implementation of the method from LauncherModel.Callbacks.
3655 */
3656 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003657 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003658
Michael Jurka7607c2f2013-04-03 14:33:19 -07003659 // If we're starting binding all over again, clear any bind calls we'd postponed in
3660 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3661 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003662 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003663
Winson Chungd64d1762013-08-20 14:37:16 -07003664 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003665 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003666 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003667
Michael Jurka05bf644e2011-11-30 20:28:53 -08003668 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003669 if (mHotseat != null) {
3670 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003671 }
3672 }
3673
Adam Cohendcd297f2013-06-18 13:13:40 -07003674 @Override
3675 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003676 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003677
Adam Cohen5084cba2013-09-03 12:01:16 -07003678 // If there are no screens, we need to have an empty screen
3679 if (orderedScreenIds.size() == 0) {
3680 mWorkspace.addExtraEmptyScreen();
3681 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003682
3683 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003684 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003685 if (hasCustomContentToLeft()) {
3686 mWorkspace.createCustomContentContainer();
3687 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003688 }
Winson Chung64359a52013-07-08 17:17:08 -07003689 }
3690
3691 @Override
3692 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003693 // Log to disk
3694 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3695 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3696 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003697 int count = orderedScreenIds.size();
3698 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003699 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003700 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003701 }
3702
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003703 @Override
3704 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3705 final long screenId, final int[] cell, final int spanX, final int spanY) {
3706 showWorkspace(true, new Runnable() {
3707
3708 @Override
3709 public void run() {
3710 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3711 addPendingItem(info, container, screenId, cell, spanX, spanY);
3712 }
3713 });
3714 }
3715
Adam Cohen39a06042013-07-19 14:30:12 -07003716 private boolean shouldShowWeightWatcher() {
3717 String spKey = LauncherAppState.getSharedPreferencesKey();
3718 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003719 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003720
3721 return show;
3722 }
3723
3724 private void toggleShowWeightWatcher() {
3725 String spKey = LauncherAppState.getSharedPreferencesKey();
3726 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3727 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3728
3729 show = !show;
3730
3731 SharedPreferences.Editor editor = sp.edit();
3732 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3733 editor.commit();
3734
3735 if (mWeightWatcher != null) {
3736 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3737 }
3738 }
3739
Winson Chungd64d1762013-08-20 14:37:16 -07003740 public void bindAppsAdded(final ArrayList<Long> newScreens,
3741 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003742 final ArrayList<ItemInfo> addAnimated,
3743 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003744 Runnable r = new Runnable() {
3745 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003746 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003747 }
3748 };
3749 if (waitUntilResume(r)) {
3750 return;
3751 }
3752
Winson Chungd64d1762013-08-20 14:37:16 -07003753 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003754 if (newScreens != null) {
3755 bindAddScreens(newScreens);
3756 }
Winson Chungd64d1762013-08-20 14:37:16 -07003757
3758 // We add the items without animation on non-visible pages, and with
3759 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003760 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003761 bindItems(addNotAnimated, 0,
3762 addNotAnimated.size(), false);
3763 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003764 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003765 bindItems(addAnimated, 0,
3766 addAnimated.size(), true);
3767 }
Winson Chungc58497e2013-09-03 17:48:37 -07003768
Winson Chung87412982013-10-03 18:34:14 -07003769 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003770 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003771
Winson Chungb745afb2015-03-02 11:51:23 -08003772 if (addedApps != null && mAppsView != null) {
3773 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003774 }
Winson Chungd64d1762013-08-20 14:37:16 -07003775 }
3776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003777 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 * Bind the items start-end from the list.
3779 *
3780 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003781 */
Winson Chung64359a52013-07-08 17:17:08 -07003782 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3783 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003784 Runnable r = new Runnable() {
3785 public void run() {
3786 bindItems(shortcuts, start, end, forceAnimateIcons);
3787 }
3788 };
3789 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 return;
3791 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003792
Winson Chungf0c6ae02012-03-21 16:10:31 -07003793 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003794 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3795 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003796 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003797 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003798 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003801
3802 // Short circuit if we are loading dock items for a configuration which has no dock
3803 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3804 mHotseat == null) {
3805 continue;
3806 }
3807
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 switch (item.itemType) {
3809 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3810 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003811 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003812 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003813
Winson Chung64359a52013-07-08 17:17:08 -07003814 /*
3815 * TODO: FIX collision case
3816 */
Winson Chungce376632013-07-11 16:12:41 -07003817 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3818 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3819 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003820 View v = cl.getChildAt(item.cellX, item.cellY);
3821 Object tag = v.getTag();
3822 String desc = "Collision while binding workspace item: " + item
3823 + ". Collides with " + tag;
3824 if (LauncherAppState.isDogfoodBuild()) {
3825 throw (new RuntimeException(desc));
3826 } else {
3827 Log.d(TAG, desc);
3828 }
Winson Chungce376632013-07-11 16:12:41 -07003829 }
Winson Chung64359a52013-07-08 17:17:08 -07003830 }
3831
Adam Cohendcd297f2013-06-18 13:13:40 -07003832 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3833 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003834 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003835 // Animate all the applications up now
3836 shortcut.setAlpha(0f);
3837 shortcut.setScaleX(0f);
3838 shortcut.setScaleY(0f);
3839 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003840 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003843 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003844 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003845 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003846 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003847 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3848 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003850 default:
3851 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003853 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003854
Winson Chung997a9232013-07-24 15:33:46 -07003855 if (animateIcons) {
3856 // Animate to the correct page
3857 if (newShortcutsScreenId > -1) {
3858 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003859 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003860 final Runnable startBounceAnimRunnable = new Runnable() {
3861 public void run() {
3862 anim.playTogether(bounceAnims);
3863 anim.start();
3864 }
3865 };
Winson Chung997a9232013-07-24 15:33:46 -07003866 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003867 // We post the animation slightly delayed to prevent slowdowns
3868 // when we are loading right after we return to launcher.
3869 mWorkspace.postDelayed(new Runnable() {
3870 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003871 if (mWorkspace != null) {
3872 mWorkspace.snapToPage(newScreenIndex);
3873 mWorkspace.postDelayed(startBounceAnimRunnable,
3874 NEW_APPS_ANIMATION_DELAY);
3875 }
Winson Chung94d67682013-09-25 16:29:40 -07003876 }
3877 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003878 } else {
3879 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003880 }
3881 }
Winson Chung64359a52013-07-08 17:17:08 -07003882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003883 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003884 }
3885
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 /**
3887 * Implementation of the method from LauncherModel.Callbacks.
3888 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003889 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003890 Runnable r = new Runnable() {
3891 public void run() {
3892 bindFolders(folders);
3893 }
3894 };
3895 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003896 return;
3897 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003898 sFolders.clear();
3899 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901
3902 /**
3903 * Add the views for a widget to the workspace.
3904 *
3905 * Implementation of the method from LauncherModel.Callbacks.
3906 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003907 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003908 Runnable r = new Runnable() {
3909 public void run() {
3910 bindAppWidget(item);
3911 }
3912 };
3913 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003914 return;
3915 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003916
Daniel Sandler843e8602010-06-07 14:59:01 -04003917 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3918 if (DEBUG_WIDGETS) {
3919 Log.d(TAG, "bindAppWidget: " + item);
3920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 final Workspace workspace = mWorkspace;
3922
Adam Cohen59400422014-03-05 18:07:04 -08003923 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003924 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003925
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003926 if (!mIsSafeModeEnabled
3927 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3928 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003929 if (appWidgetInfo == null) {
3930 if (DEBUG_WIDGETS) {
3931 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3932 + " belongs to component " + item.providerName
3933 + ", as the povider is null");
3934 }
3935 LauncherModel.deleteItemFromDatabase(this, item);
3936 return;
3937 }
3938 // Note: This assumes that the id remap broadcast is received before this step.
3939 // If that is not the case, the id remap will be ignored and user may see the
3940 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003941 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003942 pendingInfo.spanX = item.spanX;
3943 pendingInfo.spanY = item.spanY;
3944 pendingInfo.minSpanX = item.minSpanX;
3945 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003946 Bundle options = null;
3947 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003948
Sunny Goyalff572272014-07-23 13:58:07 -07003949 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003950 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3951 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003952
3953 // TODO consider showing a permission dialog when the widget is clicked.
3954 if (!success) {
3955 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3958 + " belongs to component " + item.providerName
3959 + ", as the launcher is unable to bing a new widget id");
3960 }
3961 LauncherModel.deleteItemFromDatabase(this, item);
3962 return;
3963 }
3964
3965 item.appWidgetId = newWidgetId;
3966
3967 // If the widget has a configure activity, it is still needs to set it up, otherwise
3968 // the widget is ready to go.
3969 item.restoreStatus = (appWidgetInfo.configure == null)
3970 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3971 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3972
3973 LauncherModel.updateItemInDatabase(this, item);
3974 }
3975
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003976 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003977 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003978 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003979 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3980 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003981 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003982
Sunny Goyal651077b2014-06-30 14:15:31 -07003983 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3984 } else {
3985 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003986 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3987 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003988 view.updateIcon(mIconCache);
3989 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003991 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003995 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Adam Cohendcd297f2013-06-18 13:13:40 -07003997 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003999 if (!item.isCustomWidget()) {
4000 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4001 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004002
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 workspace.requestLayout();
4004
Daniel Sandler843e8602010-06-07 14:59:01 -04004005 if (DEBUG_WIDGETS) {
4006 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4007 + (SystemClock.uptimeMillis()-start) + "ms");
4008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 }
4010
Sunny Goyalff572272014-07-23 13:58:07 -07004011 /**
4012 * Restores a pending widget.
4013 *
4014 * @param appWidgetId The app widget id
4015 * @param cellInfo The position on screen where to create the widget.
4016 */
4017 private void completeRestoreAppWidget(final int appWidgetId) {
4018 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4019 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4020 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4021 return;
4022 }
4023
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004024 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004025 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4026
4027 mWorkspace.reinflateWidgetsIfNecessary();
4028 LauncherModel.updateItemInDatabase(this, info);
4029 }
4030
Adam Cohen1462de32012-07-24 22:34:36 -07004031 public void onPageBoundSynchronously(int page) {
4032 mSynchronouslyBoundPages.add(page);
4033 }
4034
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 /**
4036 * Callback saying that there aren't any more items to bind.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004040 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004041 Runnable r = new Runnable() {
4042 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004043 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004044 }
4045 };
4046 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004047 return;
4048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 if (mSavedState != null) {
4050 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004051 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 mSavedState = null;
4054 }
4055
Adam Cohen1462de32012-07-24 22:34:36 -07004056 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004058 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004059 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004060
4061 // If we received the result of any pending adds while the loader was running (e.g. the
4062 // widget configuration forced an orientation change), process them now.
4063 if (sPendingAddItem != null) {
4064 final long screenId = completeAdd(sPendingAddItem);
4065
4066 // TODO: this moves the user to the page where the pending item was added. Ideally,
4067 // the screen would be guaranteed to exist after bind, and the page would be set through
4068 // the workspace restore process.
4069 mWorkspace.post(new Runnable() {
4070 @Override
4071 public void run() {
4072 mWorkspace.snapToScreenId(screenId);
4073 }
4074 });
4075 sPendingAddItem = null;
4076 }
4077
Sunny Goyal756adbc2015-04-16 15:20:51 -07004078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004079
4080 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004081 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004082 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004083 }
4084
Adam Cohen3ed316a2014-07-23 18:21:20 -07004085 private void sendLoadingCompleteBroadcastIfNecessary() {
4086 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4087 String permission =
4088 getResources().getString(R.string.receive_first_load_broadcast_permission);
4089 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4090 sendBroadcast(intent, permission);
4091 SharedPreferences.Editor editor = mSharedPrefs.edit();
4092 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4093 editor.apply();
4094 }
4095 }
4096
Winson Chunga0b7e862013-09-05 16:03:15 -07004097 public boolean isAllAppsButtonRank(int rank) {
4098 if (mHotseat != null) {
4099 return mHotseat.isAllAppsButtonRank(rank);
4100 }
4101 return false;
4102 }
4103
Winson Chunga2413752012-04-03 14:22:34 -07004104 private boolean canRunNewAppsAnimation() {
4105 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4106 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4107 }
4108
Winson Chungc9168342013-06-26 14:54:55 -07004109 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4110 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4111 PropertyValuesHolder.ofFloat("alpha", 1f),
4112 PropertyValuesHolder.ofFloat("scaleX", 1f),
4113 PropertyValuesHolder.ofFloat("scaleY", 1f));
4114 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4115 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4116 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4117 return bounceAnim;
4118 }
4119
Adam Cohen27772732013-11-11 14:00:56 +00004120 public boolean useVerticalBarLayout() {
4121 return LauncherAppState.getInstance().getDynamicGrid().
4122 getDeviceProfile().isVerticalBarLayout();
4123 }
4124
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004125 protected Rect getSearchBarBounds() {
4126 return LauncherAppState.getInstance().getDynamicGrid().
4127 getDeviceProfile().getSearchBarBounds();
4128 }
4129
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004130 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004131 if (mSearchDropTargetBar == null) {
4132 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004133 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004134 if (mQsb != null) {
4135 mSearchDropTargetBar.removeView(mQsb);
4136 mQsb = null;
4137 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004138 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004139 }
4140
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004142 * Add the icons for all apps.
4143 *
4144 * Implementation of the method from LauncherModel.Callbacks.
4145 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004146 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004147 if (mAppsView != null) {
4148 mAppsView.setApps(apps);
4149 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004150 if (mWidgetsView != null) {
4151 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4152 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004153 }
Adam Cohen9211d422014-10-07 18:14:53 -07004154 if (mLauncherCallbacks != null) {
4155 mLauncherCallbacks.bindAllApplications(apps);
4156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 }
4158
4159 /**
4160 * A package was updated.
4161 *
4162 * Implementation of the method from LauncherModel.Callbacks.
4163 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004164 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004165 Runnable r = new Runnable() {
4166 public void run() {
4167 bindAppsUpdated(apps);
4168 }
4169 };
4170 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004171 return;
4172 }
4173
Winson Chungb745afb2015-03-02 11:51:23 -08004174 if (mAppsView != null) {
4175 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004176 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004177 }
4178
Sunny Goyal4390ace2014-10-13 11:33:11 -07004179 @Override
4180 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindWidgetsRestored(widgets);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
4188 }
4189 mWorkspace.widgetsRestored(widgets);
4190 }
4191
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004193 * Some shortcuts were updated in the background.
4194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004197 @Override
4198 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4199 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004200 Runnable r = new Runnable() {
4201 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004202 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004203 }
4204 };
4205 if (waitUntilResume(r)) {
4206 return;
4207 }
4208
Sunny Goyal4390ace2014-10-13 11:33:11 -07004209 if (!updated.isEmpty()) {
4210 mWorkspace.updateShortcuts(updated);
4211 }
4212
4213 if (!removed.isEmpty()) {
4214 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4215 for (ShortcutInfo si : removed) {
4216 removedComponents.add(si.getTargetComponent());
4217 }
4218 mWorkspace.removeItemsByComponentName(removedComponents, user);
4219 // Notify the drag controller
4220 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004221 }
4222 }
4223
4224 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004225 * Update the state of a package, typically related to install state.
4226 *
4227 * Implementation of the method from LauncherModel.Callbacks.
4228 */
Sunny Goyale755d462014-07-22 13:48:29 -07004229 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004230 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4231 Runnable r = new Runnable() {
4232 public void run() {
4233 bindRestoreItemsChange(updates);
4234 }
4235 };
4236 if (waitUntilResume(r)) {
4237 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004238 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004239
Sunny Goyal756adbc2015-04-16 15:20:51 -07004240 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004241 }
4242
4243 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004244 * A package was uninstalled. We take both the super set of packageNames
4245 * in addition to specific applications to remove, the reason being that
4246 * this can be called when a package is updated as well. In that scenario,
4247 * we only remove specific components from the workspace, where as
4248 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004250 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004251 * Implementation of the method from LauncherModel.Callbacks.
4252 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004253 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004254 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004256 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004257 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004258 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004259 }
Winson Chungd64d1762013-08-20 14:37:16 -07004260 };
4261 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004262 return;
4263 }
4264
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4267 for (AppInfo info : appInfos) {
4268 removedComponents.add(info.componentName);
4269 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004270 if (!packageNames.isEmpty()) {
4271 mWorkspace.removeItemsByPackageName(packageNames, user);
4272 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004273 if (!removedComponents.isEmpty()) {
4274 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004275 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 // Notify the drag controller
4277 mDragController.onAppsRemoved(packageNames, removedComponents);
4278
Sunny Goyal1a745e82014-10-02 15:58:31 -07004279 } else {
4280 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004281 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004282
Winson Chungdf95eb12013-10-16 14:57:07 -07004283 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004284 if (mAppsView != null) {
4285 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 }
Joe Onoratobe386092009-11-17 17:32:16 -08004288
4289 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004290 * A number of packages were updated.
4291 */
Adam Cohen091440a2015-03-18 14:16:05 -07004292 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004293 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004294 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004295 bindPackagesUpdated(mWidgetsAndShortcuts);
4296 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004297 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004298 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004299
Michael Jurkac402cd92013-05-20 15:49:32 +02004300 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4301 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4302 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 return;
4304 }
4305
Hyunyoung Song3f471442015-04-08 19:01:34 -07004306 if (mWidgetsView != null) {
4307 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4308 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004310 }
4311
Winson Chung400438b2011-01-16 17:53:48 -08004312 private int mapConfigurationOriActivityInfoOri(int configOri) {
4313 final Display d = getWindowManager().getDefaultDisplay();
4314 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4315 switch (d.getRotation()) {
4316 case Surface.ROTATION_0:
4317 case Surface.ROTATION_180:
4318 // We are currently in the same basic orientation as the natural orientation
4319 naturalOri = configOri;
4320 break;
4321 case Surface.ROTATION_90:
4322 case Surface.ROTATION_270:
4323 // We are currently in the other basic orientation to the natural orientation
4324 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4325 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4326 break;
4327 }
4328
4329 int[] oriMap = {
4330 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4331 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4332 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4333 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4334 };
4335 // Since the map starts at portrait, we need to offset if this device's natural orientation
4336 // is landscape.
4337 int indexOffset = 0;
4338 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4339 indexOffset = 1;
4340 }
4341 return oriMap[(d.getRotation() + indexOffset) % 4];
4342 }
Adam Cohen446e9402011-09-15 18:21:21 -07004343
Winson Chung4b919f82012-05-01 10:44:08 -07004344 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004345 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004346 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4347 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4348 .getConfiguration().orientation));
4349 } else {
4350 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4351 }
Winson Chung4b919f82012-05-01 10:44:08 -07004352 }
4353 }
4354 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004355 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004356 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004357 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004358 } else {
4359 mHandler.postDelayed(new Runnable() {
4360 public void run() {
4361 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4362 }
4363 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004364 }
Winson Chung4b919f82012-05-01 10:44:08 -07004365 }
Winson Chung400438b2011-01-16 17:53:48 -08004366 }
4367
Winson Chunge43a1e72014-01-15 10:33:02 -08004368 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.isLauncherPreinstalled();
4371 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004372 PackageManager pm = getPackageManager();
4373 try {
4374 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4375 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4376 return true;
4377 } else {
4378 return false;
4379 }
4380 } catch (NameNotFoundException e) {
4381 e.printStackTrace();
4382 return false;
4383 }
4384 }
4385
Adam Cohenea90f832014-05-21 15:03:34 -07004386 /**
4387 * This method indicates whether or not we should suggest default wallpaper dimensions
4388 * when our wallpaper cropper was not yet used to set a wallpaper.
4389 */
4390 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004391 if (mLauncherCallbacks != null) {
4392 return mLauncherCallbacks.overrideWallpaperDimensions();
4393 }
Adam Cohenea90f832014-05-21 15:03:34 -07004394 return true;
4395 }
4396
Adam Cohen432609a2014-03-13 17:03:22 -07004397 /**
4398 * To be overridden by subclasses to indicate that there is an activity to launch
4399 * before showing the standard launcher experience.
4400 */
4401 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004402 if (mLauncherCallbacks != null) {
4403 return mLauncherCallbacks.hasFirstRunActivity();
4404 }
Adam Cohen432609a2014-03-13 17:03:22 -07004405 return false;
4406 }
4407
4408 /**
4409 * To be overridden by subclasses to launch any first run activity
4410 */
4411 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004412 if (mLauncherCallbacks != null) {
4413 return mLauncherCallbacks.getFirstRunActivity();
4414 }
Adam Cohen432609a2014-03-13 17:03:22 -07004415 return null;
4416 }
4417
Winson Chung2826a402015-04-17 16:18:53 -07004418 /**
4419 * Returns whether the launcher callbacks overrides search in all apps.
4420 * @return
4421 */
4422 @Thunk boolean isAllAppsSearchOverridden() {
4423 if (mLauncherCallbacks != null) {
4424 return mLauncherCallbacks.overrideAllAppsSearch();
4425 }
4426 return false;
4427 }
4428
Winson Chunga6945242014-01-08 14:04:34 -08004429 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004430 return !ActivityManager.isRunningInTestHarness() &&
4431 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004432 }
4433
Adam Cohen4a9423d2014-03-28 14:22:31 -07004434 protected boolean hasRunFirstRunActivity() {
4435 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4436 }
4437
Adam Cohen432609a2014-03-13 17:03:22 -07004438 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004439 if (shouldRunFirstRunActivity() &&
4440 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004441 Intent firstRunIntent = getFirstRunActivity();
4442 if (firstRunIntent != null) {
4443 startActivity(firstRunIntent);
4444 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004445 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004446 }
4447 }
Adam Cohen432609a2014-03-13 17:03:22 -07004448 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004449 }
4450
4451 private void markFirstRunActivityShown() {
4452 SharedPreferences.Editor editor = mSharedPrefs.edit();
4453 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4454 editor.apply();
4455 }
4456
Adam Cohen432609a2014-03-13 17:03:22 -07004457 /**
4458 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4459 * screen that must be displayed and dismissed.
4460 */
4461 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004462 if (mLauncherCallbacks != null) {
4463 return mLauncherCallbacks.hasDismissableIntroScreen();
4464 }
Adam Cohen432609a2014-03-13 17:03:22 -07004465 return false;
4466 }
4467
4468 /**
4469 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4470 */
4471 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004472 if (mLauncherCallbacks != null) {
4473 return mLauncherCallbacks.getIntroScreen();
4474 }
Adam Cohen432609a2014-03-13 17:03:22 -07004475 return null;
4476 }
4477
4478 /**
4479 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4480 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4481 */
4482 private boolean shouldShowIntroScreen() {
4483 return hasDismissableIntroScreen() &&
4484 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4485 }
4486
4487 protected void showIntroScreen() {
4488 View introScreen = getIntroScreen();
4489 changeWallpaperVisiblity(false);
4490 if (introScreen != null) {
4491 mDragLayer.showOverlayView(introScreen);
4492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004493 if (mLauncherOverlayContainer != null) {
4494 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4495 }
Adam Cohen432609a2014-03-13 17:03:22 -07004496 }
4497
4498 public void dismissIntroScreen() {
4499 markIntroScreenDismissed();
4500 if (showFirstRunActivity()) {
4501 // We delay hiding the intro view until the first run activity is showing. This
4502 // avoids a blip.
4503 mWorkspace.postDelayed(new Runnable() {
4504 @Override
4505 public void run() {
4506 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004507 if (mLauncherOverlayContainer != null) {
4508 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4509 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004510 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512 }, ACTIVITY_START_DELAY);
4513 } else {
4514 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004515 if (mLauncherOverlayContainer != null) {
4516 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4517 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004518 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004519 }
4520 changeWallpaperVisiblity(true);
4521 }
4522
4523 private void markIntroScreenDismissed() {
4524 SharedPreferences.Editor editor = mSharedPrefs.edit();
4525 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4526 editor.apply();
4527 }
4528
Adam Cohen091440a2015-03-18 14:16:05 -07004529 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004530 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4531 // on the device, then we always show the first run cling experience (or if there is no
4532 // launcher2). Otherwise, we prompt the user upon started for migration
4533 LauncherClings launcherClings = new LauncherClings(this);
4534 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4535 if (mModel.canMigrateFromOldLauncherDb(this)) {
4536 launcherClings.showMigrationCling();
4537 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004538 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004539 }
4540 }
4541 }
4542
Winson Chunga6945242014-01-08 14:04:34 -08004543 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004544 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4545 if (mHotseat != null) mHotseat.setAlpha(1f);
4546 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4547 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004548 }
4549
4550 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004551 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4552 if (mHotseat != null) mHotseat.setAlpha(0f);
4553 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4554 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004555 }
4556
Mathew Inwood72fbec12013-11-19 15:45:07 +00004557 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004558 // Called from search suggestion, not supported in other profiles.
4559 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4560 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4561 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4562 myUser);
4563 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004564 return null;
4565 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004566 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 }
4568
4569 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4570 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004571 // Called from search suggestion, not supported in other profiles.
4572 return createShortcutDragInfo(shortcutIntent, caption, icon,
4573 UserHandleCompat.myUserHandle());
4574 }
4575
4576 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4577 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004578 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004579 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004580 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004581 }
4582
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004583 protected void moveWorkspaceToDefaultScreen() {
4584 mWorkspace.moveToDefaultScreen(false);
4585 }
4586
Mathew Inwood72fbec12013-11-19 15:45:07 +00004587 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4588 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004589 mWorkspace.onExternalDragStartedWithItem(dragView);
4590 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004591 }
4592
Anjali Koppalf5d29132014-02-28 13:33:27 -08004593 @Override
4594 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004595 if (mLauncherCallbacks != null) {
4596 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4597 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004598 }
4599
Winson Chung80baf5a2010-08-09 16:03:15 -07004600 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004601 * Prints out out state for debugging.
4602 */
4603 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004604 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004605 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004606 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4607 Log.d(TAG, "mRestoring=" + mRestoring);
4608 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4609 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004610 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004611 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004612 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004613
Daniel Sandler325dc232013-06-05 22:57:57 -04004614 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004615 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004616
4617 @Override
4618 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4619 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004620 synchronized (sDumpLogs) {
4621 writer.println(" ");
4622 writer.println("Debug logs: ");
4623 for (int i = 0; i < sDumpLogs.size(); i++) {
4624 writer.println(" " + sDumpLogs.get(i));
4625 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004626 }
Adam Cohen9211d422014-10-07 18:14:53 -07004627 if (mLauncherCallbacks != null) {
4628 mLauncherCallbacks.dump(prefix, fd, writer, args);
4629 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004630 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004631
4632 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004633 if (DEBUG_DUMP_LOG) {
4634 synchronized (sDumpLogs) {
4635 Log.d(TAG, "");
4636 Log.d(TAG, "*********************");
4637 Log.d(TAG, "Launcher debug logs: ");
4638 for (int i = 0; i < sDumpLogs.size(); i++) {
4639 Log.d(TAG, " " + sDumpLogs.get(i));
4640 }
4641 Log.d(TAG, "*********************");
4642 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004643 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004644 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004645 }
4646
4647 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004648 addDumpLog(tag, log, null, debugLog);
4649 }
4650
4651 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004652 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004653 if (e != null) {
4654 Log.d(tag, log, e);
4655 } else {
4656 Log.d(tag, log);
4657 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004658 }
Winson Chungede41292013-09-19 16:27:36 -07004659 if (DEBUG_DUMP_LOG) {
4660 sDateStamp.setTime(System.currentTimeMillis());
4661 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004662 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4663 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004664 }
Winson Chungede41292013-09-19 16:27:36 -07004665 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 }
4667
Adam Cohen59400422014-03-05 18:07:04 -08004668 public static CustomAppWidget getCustomAppWidget(String name) {
4669 return sCustomAppWidgets.get(name);
4670 }
4671
4672 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4673 return sCustomAppWidgets;
4674 }
4675
Winson Chungede41292013-09-19 16:27:36 -07004676 public void dumpLogsToLocalData() {
4677 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004678 new AsyncTask<Void, Void, Void>() {
4679 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004680 boolean success = false;
4681 sDateStamp.setTime(sRunStart);
4682 String FILENAME = sDateStamp.getMonth() + "-"
4683 + sDateStamp.getDay() + "_"
4684 + sDateStamp.getHours() + "-"
4685 + sDateStamp.getMinutes() + "_"
4686 + sDateStamp.getSeconds() + ".txt";
4687
4688 FileOutputStream fos = null;
4689 File outFile = null;
4690 try {
4691 outFile = new File(getFilesDir(), FILENAME);
4692 outFile.createNewFile();
4693 fos = new FileOutputStream(outFile);
4694 } catch (Exception e) {
4695 e.printStackTrace();
4696 }
4697 if (fos != null) {
4698 PrintWriter writer = new PrintWriter(fos);
4699
4700 writer.println(" ");
4701 writer.println("Debug logs: ");
4702 synchronized (sDumpLogs) {
4703 for (int i = 0; i < sDumpLogs.size(); i++) {
4704 writer.println(" " + sDumpLogs.get(i));
4705 }
4706 }
4707 writer.close();
4708 }
4709 try {
4710 if (fos != null) {
4711 fos.close();
4712 success = true;
4713 }
4714 } catch (IOException e) {
4715 e.printStackTrace();
4716 }
Michael Jurka43467462013-11-14 12:03:58 +01004717 return null;
Winson Chungede41292013-09-19 16:27:36 -07004718 }
Michael Jurka43467462013-11-14 12:03:58 +01004719 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004720 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004722}
Michael Jurka2763be32011-02-24 11:19:57 -08004723
Michael Jurkaabded662011-03-04 12:06:57 -08004724interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004725 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004726 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004727 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004728 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004729 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004730}
Dan Sandlerd5024042014-01-09 15:01:33 -05004731
4732interface DebugIntents {
4733 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4734 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004735}