blob: 8258984c3373c6d147b4752766023d389936dac6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700103import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700105import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700106import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700107import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700108
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.DataInputStream;
110import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800117import java.lang.reflect.InvocationTargetException;
118import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700121import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700124import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700125import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000126import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128/**
129 * Default launcher application.
130 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100131public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700132 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800133 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
134 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700136 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700139 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700140 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700142 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
Dan Sandlerd5024042014-01-09 15:01:33 -0500144 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700149 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Michael Jurka8b805b12012-04-18 14:23:14 -0700151 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700152 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700153
Mathew Inwood876a8462013-06-14 14:12:41 +0100154 /**
155 * IntentStarter uses request codes starting with this. This must be greater than all activity
156 * request codes used internally.
157 */
158 protected static final int REQUEST_LAST = 100;
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
161
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800162 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 // To turn on these properties, type
165 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800215 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700219 private boolean mIsSafeModeEnabled;
220
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
222 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700223 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000230 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
231 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
232
Winson Chunga2413752012-04-03 14:22:34 -0700233 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700234 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
235 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700237
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800238 private final BroadcastReceiver mCloseSystemDialogsReceiver
239 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800240 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private LayoutInflater mInflater;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700245 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800246 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700249 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Sunny Goyalffe83f12014-08-14 17:39:34 -0700251 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800255 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800256 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257
Michael Jurka0280c3b2010-09-17 15:00:07 -0700258 private int[] mTmpAddItemCellCoordinates = new int[2];
259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private FolderInfo mFolderInfo;
261
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266
Winson Chungc51db6a2011-10-05 11:44:49 -0700267 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
269 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
272 // Main container view for the widget tray screen.
273 private WidgetsContainerView mWidgetsView;
274
Winson Chungf0ea4d32011-06-06 14:27:16 -0700275 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800276 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
280 // scroll issues (because the workspace may not have been measured yet) and extra work.
281 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
282 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
284 private SpannableStringBuilder mDefaultKeySsb = null;
285
Adam Cohen091440a2015-03-18 14:16:05 -0700286 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800288 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private boolean mRestoring;
290 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700291 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700294 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private Bundle mSavedInstanceState;
297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700302 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304
Adam Cohen091440a2015-03-18 14:16:05 -0700305 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306
Sunny Goyale2df0622015-04-24 11:27:00 -0700307 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700308
Adam Cohen61f560d2013-09-30 15:58:20 -0700309 private View.OnTouchListener mHapticFeedbackTouchListener;
310
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 // Related to the auto-advancing of widgets
312 private final int ADVANCE_MSG = 1;
313 private final int mAdvanceInterval = 20000;
314 private final int mAdvanceStagger = 250;
315 private long mAutoAdvanceSentTime;
316 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700317 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700318 new HashMap<View, AppWidgetProviderInfo>();
319
Winson Chung400438b2011-01-16 17:53:48 -0800320 // Determines how long to wait after a rotation before restoring the screen orientation to
321 // match the sensor state.
322 private final int mRestoreScreenOrientationDelay = 500;
323
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700325
Adam Cohen1462de32012-07-24 22:34:36 -0700326 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700327 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700328
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700330 static Date sDateStamp = new Date();
331 static DateFormat sDateFormat =
332 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
333 static long sRunStart = System.currentTimeMillis();
334 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700335
Winson Chung46353de2012-02-16 14:05:10 -0800336 // We only want to get the SharedPreferences once since it does an FS stat each time we get
337 // it from the context.
338 private SharedPreferences mSharedPrefs;
339
Winson Chungf0c6ae02012-03-21 16:10:31 -0700340 // Holds the page that we need to animate to, and the icon views that we need to animate up
341 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700342 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700343 private Bitmap mFolderIconBitmap;
344 private Canvas mFolderIconCanvas;
345 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700346
Michael Jurkaddd62e92011-02-16 17:49:14 -0800347 private BubbleTextView mWaitingForResume;
348
Adam Cohen59400422014-03-05 18:07:04 -0800349 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
350 new HashMap<String, CustomAppWidget>();
351
352 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
353 static {
354 if (ENABLE_CUSTOM_WIDGET_TEST) {
355 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
356 }
357 }
358
Chet Haasea8f996d2015-02-17 12:56:04 -0800359 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
360 private static Method sClipRevealMethod = null;
361 static {
362 Class<?> activityOptionsClass = ActivityOptions.class;
363 try {
364 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
365 View.class, int.class, int.class, int.class, int.class);
366 } catch (Exception e) {
367 // Earlier version
368 }
369 }
370
Adam Cohen091440a2015-03-18 14:16:05 -0700371 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700372 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700373 if (mWorkspace != null) {
374 mWorkspace.buildPageHardwareLayers();
375 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700376 }
377 };
378
Adam Cohendb364c32014-05-20 14:23:40 -0700379 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380
Adam Cohen091440a2015-03-18 14:16:05 -0700381 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382 int requestCode;
383 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700384 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700385 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386 int cellX;
387 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700388 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 }
390
Daniel Sandlerff02d492013-08-05 02:12:05 -0400391 private Stats mStats;
392
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700393 FocusIndicatorView mFocusHandler;
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 @Override
396 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700397 if (DEBUG_STRICT_MODE) {
398 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
399 .detectDiskReads()
400 .detectDiskWrites()
401 .detectNetwork() // or .detectAll() for all detectable problems
402 .penaltyLog()
403 .build());
404 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
405 .detectLeakedSqlLiteObjects()
406 .detectLeakedClosableObjects()
407 .penaltyLog()
408 .penaltyDeath()
409 .build());
410 }
411
Adam Cohen9211d422014-10-07 18:14:53 -0700412 if (mLauncherCallbacks != null) {
413 mLauncherCallbacks.preOnCreate();
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700420 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Winson Chung5f8afe62013-08-12 16:19:28 -0700422 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700423 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700424
425 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400426 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700427 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700428 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mModel = app.setLauncher(this);
430 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700431 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400432 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800434 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Daniel Sandlerff02d492013-08-05 02:12:05 -0400436 mStats = new Stats(this);
437
Sunny Goyalffe83f12014-08-14 17:39:34 -0700438 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
441 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700442
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700443 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
444 // this also ensures that any synchronous binding below doesn't re-trigger another
445 // LauncherModel load.
446 mPaused = false;
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200449 android.os.Debug.startMethodTracing(
450 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452
453 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100457 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800459 registerContentObservers();
460
Joe Onorato7c312c12009-08-13 21:36:53 -0700461 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 mSavedState = savedInstanceState;
464 restoreState(mSavedState);
465
466 if (PROFILE_STARTUP) {
467 android.os.Debug.stopMethodTracing();
468 }
469
470 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700471 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 // If the user leaves launcher, then we should just load items asynchronously when
473 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500474 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 } else {
476 // We only load the page synchronously if the user rotates (or triggers a
477 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800478 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481
482 // For handling default keys
483 mDefaultKeySsb = new SpannableStringBuilder();
484 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800485
486 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
487 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488
Adam Cohenf9426d52012-06-04 17:26:21 -0700489 // On large interfaces, we want the screen to auto-rotate based on the current orientation
490 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700494 if (mLauncherCallbacks.hasLauncherOverlay()) {
495 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700496 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
497 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
498 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700499 mWorkspace.setLauncherOverlay(mLauncherOverlay);
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700502
503 if (shouldShowIntroScreen()) {
504 showIntroScreen();
505 } else {
506 showFirstRunActivity();
507 showFirstRunClings();
508 }
Adam Cohen9211d422014-10-07 18:14:53 -0700509 }
510
511 private LauncherCallbacks mLauncherCallbacks;
512
513 public void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onPostCreate(savedInstanceState);
517 }
518 }
519
520 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
521 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700522 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
523 @Override
524 public void onAllAppsBoundsChanged(Rect bounds) {
525 mAppsView.setFixedBounds(Launcher.this, bounds);
526 }
527
528 @Override
529 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700530 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
531 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700532 }
533 });
Adam Cohen9211d422014-10-07 18:14:53 -0700534 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700535 }
536
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700537 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700538 public void onLauncherProviderChange() {
539 if (mLauncherCallbacks != null) {
540 mLauncherCallbacks.onLauncherProviderChange();
541 }
542 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700543
Adam Cohen9211d422014-10-07 18:14:53 -0700544 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700545 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700546 if (mLauncherCallbacks != null) {
547 return mLauncherCallbacks.hasCustomContentToLeft();
548 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700549 return false;
550 }
551
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500553 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600554 * {@link #addToCustomContentPage}. This will only be invoked if
555 * {@link #hasCustomContentToLeft()} is {@code true}.
556 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500557 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700558 if (mLauncherCallbacks != null) {
559 mLauncherCallbacks.populateCustomContentContainer();
560 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600561 }
562
563 /**
564 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
565 * ensure the custom content page is added or removed if necessary.
566 */
567 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600568 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600569 // Not bound yet, wait for bindScreens to be called.
570 return;
571 }
572
573 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
574 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500575 mWorkspace.createCustomContentContainer();
576 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600577 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
578 mWorkspace.removeCustomContentPage();
579 }
580 }
581
Adam Cohen091440a2015-03-18 14:16:05 -0700582 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 if (sLocaleConfiguration == null) {
584 new AsyncTask<Void, Void, LocaleConfiguration>() {
585 @Override
586 protected LocaleConfiguration doInBackground(Void... unused) {
587 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
588 readConfiguration(Launcher.this, localeConfiguration);
589 return localeConfiguration;
590 }
591
592 @Override
593 protected void onPostExecute(LocaleConfiguration result) {
594 sLocaleConfiguration = result;
595 checkForLocaleChange(); // recursive, but now with a locale configuration
596 }
597 }.execute();
598 return;
599 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 final Configuration configuration = getResources().getConfiguration();
602
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final String locale = configuration.locale.toString();
605
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final int mcc = configuration.mcc;
608
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final int mnc = configuration.mnc;
611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613
Romain Guy84f296c2009-11-04 15:00:44 -0800614 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 sLocaleConfiguration.locale = locale;
616 sLocaleConfiguration.mcc = mcc;
617 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700618
Joe Onorato0589f0f2010-02-08 13:44:00 -0800619 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620
621 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100622 new AsyncTask<Void, Void, Void>() {
623 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100625 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700626 }
Michael Jurka43467462013-11-14 12:03:58 +0100627 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700628 }
629 }
630
Adam Cohen091440a2015-03-18 14:16:05 -0700631 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700632 public String locale;
633 public int mcc = -1;
634 public int mnc = -1;
635 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700636
Adam Cohen091440a2015-03-18 14:16:05 -0700637 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700638 DataInputStream in = null;
639 try {
Helena Josol28db2802014-10-09 17:04:09 +0100640 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700641 configuration.locale = in.readUTF();
642 configuration.mcc = in.readInt();
643 configuration.mnc = in.readInt();
644 } catch (FileNotFoundException e) {
645 // Ignore
646 } catch (IOException e) {
647 // Ignore
648 } finally {
649 if (in != null) {
650 try {
651 in.close();
652 } catch (IOException e) {
653 // Ignore
654 }
655 }
656 }
657 }
658
Adam Cohen091440a2015-03-18 14:16:05 -0700659 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700660 DataOutputStream out = null;
661 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100662 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100663 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700664 out.writeUTF(configuration.locale);
665 out.writeInt(configuration.mcc);
666 out.writeInt(configuration.mnc);
667 out.flush();
668 } catch (FileNotFoundException e) {
669 // Ignore
670 } catch (IOException e) {
671 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100672 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700673 } finally {
674 if (out != null) {
675 try {
676 out.close();
677 } catch (IOException e) {
678 // Ignore
679 }
680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682 }
683
Anton Hanssona2f665f2013-09-26 12:18:32 +0100684 public Stats getStats() {
685 return mStats;
686 }
687
Bjorn Bringertc459e522013-06-07 19:36:01 +0100688 public LayoutInflater getInflater() {
689 return mInflater;
690 }
691
Hyunyoung Song3f471442015-04-08 19:01:34 -0700692 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700693 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
694 // that is subsequently removed from the workspace in startBinding().
695 return !mModel.isLoadingWorkspace();
696 }
697
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800698 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000699 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100700 if (Build.VERSION.SDK_INT >= 17) {
701 return View.generateViewId();
702 } else {
703 // View.generateViewId() is not available. The following fallback logic is a copy
704 // of its implementation.
705 for (;;) {
706 final int result = sNextGeneratedId.get();
707 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
708 int newValue = result + 1;
709 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
710 if (sNextGeneratedId.compareAndSet(result, newValue)) {
711 return result;
712 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000713 }
714 }
715 }
716
717 public int getViewIdForItem(ItemInfo info) {
718 // This cast is safe given the > 2B range for int.
719 int itemId = (int) info.id;
720 if (mItemIdToViewId.containsKey(itemId)) {
721 return mItemIdToViewId.get(itemId);
722 }
723 int viewId = generateViewId();
724 mItemIdToViewId.put(itemId, viewId);
725 return viewId;
726 }
727
728 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700729 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
730 * a configuration step, this allows the proper animations to run after other transitions.
731 */
Adam Cohendb364c32014-05-20 14:23:40 -0700732 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700733 long screenId = args.screenId;
734 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
735 // When the screen id represents an actual screen (as opposed to a rank) we make sure
736 // that the drop page actually exists.
737 screenId = ensurePendingDropLayoutExists(args.screenId);
738 }
Adam Cohendb364c32014-05-20 14:23:40 -0700739
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700742 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700744 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700746 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700748 case REQUEST_RECONFIGURE_APPWIDGET:
749 completeRestoreAppWidget(args.appWidgetId);
750 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 }
Michael Jurka27614d22012-04-02 06:40:22 -0700752 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
753 // if you turned the screen off and then back while in All Apps, Launcher would not
754 // return to the workspace. Clearing mAddInfo.container here fixes this issue
755 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700756 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
758
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800759 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700761 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700762 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700763 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700765
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 Runnable exitSpringLoaded = new Runnable() {
767 @Override
768 public void run() {
769 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
770 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
771 }
772 };
773
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
777 if (resultCode == RESULT_CANCELED) {
778 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700779 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800782 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 }
785 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200786 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
787 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700788 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789 }
790 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200792
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
794 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700795
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800797 // We have special handling for widgets
798 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800799 final int appWidgetId;
800 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
801 : -1;
802 if (widgetId < 0) {
803 appWidgetId = pendingAddWidgetId;
804 } else {
805 appWidgetId = widgetId;
806 }
807
Adam Cohenad4e15c2013-10-17 16:21:35 -0700808 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800809 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
811 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700812 result = RESULT_CANCELED;
813 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 @Override
816 public void run() {
817 exitSpringLoadedDragModeDelayed(false, 0, null);
818 }
819 };
Adam Cohendb364c32014-05-20 14:23:40 -0700820 if (workspaceLocked) {
821 // No need to remove the empty screen if we're mid-binding, as the
822 // the bind will not add the empty screen.
823 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
824 } else {
825 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
826 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
827 }
Winson Chung5aaab772012-04-27 15:24:02 -0700828 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700829 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700830 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
831 // When the screen id represents an actual screen (as opposed to a rank)
832 // we make sure that the drop page actually exists.
833 mPendingAddInfo.screenId =
834 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
835 }
Adam Cohendb364c32014-05-20 14:23:40 -0700836 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
837
838 dropLayout.setDropPending(true);
839 final Runnable onComplete = new Runnable() {
840 @Override
841 public void run() {
842 completeTwoStageWidgetDrop(resultCode, appWidgetId);
843 dropLayout.setDropPending(false);
844 }
845 };
846 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 } else {
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
850 mPendingAddInfo);
851 sPendingAddItem = args;
852 }
Winson Chung5aaab772012-04-27 15:24:02 -0700853 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 return;
855 }
856
Sunny Goyalff572272014-07-23 13:58:07 -0700857 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
858 if (resultCode == RESULT_OK) {
859 // Update the widget view.
860 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
861 pendingAddWidgetId, mPendingAddInfo);
862 if (workspaceLocked) {
863 sPendingAddItem = args;
864 } else {
865 completeAdd(args);
866 }
867 }
868 // Leave the widget in the pending state if the user canceled the configure.
869 return;
870 }
871
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 // The pattern used here is that a user PICKs a specific application,
873 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
876 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700877 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700878 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
879 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800880 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700881 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800882 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700883 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700884 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
885 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
Adam Cohen00074722013-10-11 17:46:09 -0700887 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700888 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800892
893 }
894
895 @Override
896 protected void onActivityResult(
897 final int requestCode, final int resultCode, final Intent data) {
898 handleActivityResult(requestCode, resultCode, data);
899 if (mLauncherCallbacks != null) {
900 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
901 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800902 }
903
Adam Cohendb364c32014-05-20 14:23:40 -0700904 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
905 appWidgetId, ItemInfo info) {
906 PendingAddArguments args = new PendingAddArguments();
907 args.requestCode = requestCode;
908 args.intent = data;
909 args.container = info.container;
910 args.screenId = info.screenId;
911 args.cellX = info.cellX;
912 args.cellY = info.cellY;
913 args.appWidgetId = appWidgetId;
914 return args;
915 }
916
917 /**
918 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
919 *
920 * @param screenId the screen id to check
921 * @return the new screen, or screenId if it exists
922 */
923 private long ensurePendingDropLayoutExists(long screenId) {
924 CellLayout dropLayout =
925 (CellLayout) mWorkspace.getScreenWithId(screenId);
926 if (dropLayout == null) {
927 // it's possible that the add screen was removed because it was
928 // empty and a re-bind occurred
929 mWorkspace.addExtraEmptyScreen();
930 return mWorkspace.commitExtraEmptyScreen();
931 } else {
932 return screenId;
933 }
934 }
935
Adam Cohen091440a2015-03-18 14:16:05 -0700936 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700937 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 Runnable onCompleteRunnable = null;
940 int animationType = 0;
941
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 if (resultCode == RESULT_OK) {
944 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
945 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700946 mPendingAddWidgetInfo);
947 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 onCompleteRunnable = new Runnable() {
949 @Override
950 public void run() {
951 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700953 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
954 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 }
956 };
957 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800958 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 if (mDragLayer.getAnimatedView() != null) {
962 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
963 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
964 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700965 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700966 // The animated view may be null in the case of a rotation during widget configuration
967 onCompleteRunnable.run();
968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970
971 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 protected void onStop() {
973 super.onStop();
974 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStop();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
982 protected void onStart() {
983 super.onStart();
984 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700985
986 if (mLauncherCallbacks != null) {
987 mLauncherCallbacks.onStart();
988 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700989 }
990
991 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200993 long startTime = 0;
994 if (DEBUG_RESUME_TIME) {
995 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400996 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200997 }
Adam Cohen9211d422014-10-07 18:14:53 -0700998
999 if (mLauncherCallbacks != null) {
1000 mLauncherCallbacks.preOnResume();
1001 }
1002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001004
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001006 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001007 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001008 } else if (mOnResumeState == State.APPS) {
1009 showAppsView(false /* animated */, false /* resetListToTop */);
1010 } else if (mOnResumeState == State.WIDGETS) {
1011 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001012 }
1013 mOnResumeState = State.NONE;
1014
Craig Mautner360310b2012-10-26 15:13:08 -07001015 // Background was set to gradient in onPause(), restore to black if in all apps.
1016 setWorkspaceBackground(mState == State.WORKSPACE);
1017
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001018 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001019 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001020 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001021 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1027 // execute them here
1028 long startTimeCallbacks = 0;
1029 if (DEBUG_RESUME_TIME) {
1030 startTimeCallbacks = System.currentTimeMillis();
1031 }
1032
Michael Jurka1e2f4652013-07-08 18:03:46 -07001033 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1034 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1039 (System.currentTimeMillis() - startTimeCallbacks));
1040 }
Michael Jurka447bf842013-05-15 14:52:15 +02001041 }
Michael Jurka54554252013-08-01 12:52:23 +02001042 if (mOnResumeCallbacks.size() > 0) {
1043 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1044 mOnResumeCallbacks.get(i).run();
1045 }
1046 mOnResumeCallbacks.clear();
1047 }
Winson Chunge4e50662012-01-23 14:45:13 -08001048
1049 // Reset the pressed state of icons that were locked in the press state while activities
1050 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001052 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001053 mWaitingForResume.setStayPressed(false);
1054 }
Winson Chung82963d52013-10-09 11:20:57 -07001055
Adam Cohen06dff352012-06-01 17:17:08 -07001056 // It is possible that widgets can receive updates while launcher is not in the foreground.
1057 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1058 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1059 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001060 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001061 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001062
Michael Jurka447bf842013-05-15 14:52:15 +02001063 if (DEBUG_RESUME_TIME) {
1064 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1065 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001066
1067 if (mWorkspace.getCustomContentCallbacks() != null) {
1068 // If we are resuming and the custom content is the current page, we call onShow().
1069 // It is also poassible that onShow will instead be called slightly after first layout
1070 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1071 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001072 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001073 }
1074 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001075 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001076 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001077
Sunny Goyal756adbc2015-04-16 15:20:51 -07001078 if (!isWorkspaceLoading()) {
1079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1081 }
Adam Cohen9211d422014-10-07 18:14:53 -07001082
1083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onResume();
1085 }
Adam Cohen06dff352012-06-01 17:17:08 -07001086 }
1087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001090 // Ensure that items added to Launcher are queued until Launcher returns
1091 InstallShortcutReceiver.enableInstallQueue();
1092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001094 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001095 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001096 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001097
1098 // We call onHide() aggressively. The custom content callbacks should be able to
1099 // debounce excess onHide calls.
1100 if (mWorkspace.getCustomContentCallbacks() != null) {
1101 mWorkspace.getCustomContentCallbacks().onHide();
1102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Adam Cohen9211d422014-10-07 18:14:53 -07001104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
1109 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001110 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1111 // by a onResume or by scrolling otherwise.
1112 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001113
1114 // Custom content is completely hidden
1115 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001116
1117 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1118 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001119
1120 // Indicates whether the user is allowed to scroll away from the custom content.
1121 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 }
1123
Adam Cohenc2d6e892014-10-16 09:49:24 -07001124 public interface LauncherOverlay {
1125
1126 /**
1127 * Touch interaction leading to overscroll has begun
1128 */
1129 public void onScrollInteractionBegin();
1130
1131 /**
1132 * Touch interaction related to overscroll has ended
1133 */
1134 public void onScrollInteractionEnd();
1135
1136 /**
1137 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1138 * screen (or in the case of RTL, the rightmost screen).
1139 */
1140 public void onScrollChange(int progress, boolean rtl);
1141
1142 /**
1143 * Screen has stopped scrolling
1144 */
1145 public void onScrollSettled();
1146
1147 /**
1148 * This method can be called by the Launcher in order to force the LauncherOverlay
1149 * to exit fully immersive mode.
1150 */
1151 public void forceExitFullImmersion();
1152 }
1153
Winson Chung0f785722015-04-08 10:27:49 -07001154 public interface LauncherAppsCallbacks {
1155 /**
1156 * Updates launcher to the available space that AllApps can take so as not to overlap with
1157 * any other views.
1158 */
1159 public void onAllAppsBoundsChanged(Rect bounds);
1160
1161 /**
1162 * Called to dismiss all apps if it is showing.
1163 */
1164 public void dismissAllApps();
1165 }
1166
Adam Cohenc2d6e892014-10-16 09:49:24 -07001167 public interface LauncherOverlayCallbacks {
1168 /**
1169 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1170 * however it doesn't modify any state within the launcher.
1171 */
1172 public boolean canEnterFullImmersion();
1173
1174 /**
1175 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1176 * eg. by occupying the full screen and handling all touch events.
1177 *
1178 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1179 * case, Launcher will modify any necessary state and assumes the overlay is
1180 * handling all interaction. If false, the LauncherOverlay should cancel any
1181 *
1182 */
1183 public boolean enterFullImmersion();
1184
1185 /**
1186 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1187 * full control over UI and state.
1188 */
1189 public void exitFullImmersion();
1190 }
1191
1192 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1193
1194 @Override
1195 public boolean canEnterFullImmersion() {
1196 return mState == State.WORKSPACE;
1197 }
1198
1199 @Override
1200 public boolean enterFullImmersion() {
1201 if (mState == State.WORKSPACE) {
1202 // When fully immersed, disregard any touches which fall through.
1203 mDragLayer.setBlockTouch(true);
1204 return true;
1205 }
1206 return false;
1207 }
1208
1209 @Override
1210 public void exitFullImmersion() {
1211 mDragLayer.setBlockTouch(false);
1212 }
1213 }
1214
Jorim Jaggid017f882014-01-14 17:08:48 -08001215 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001216 if (mLauncherCallbacks != null) {
1217 return mLauncherCallbacks.hasSettings();
1218 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001219 return false;
1220 }
1221
Adam Cohen9211d422014-10-07 18:14:53 -07001222 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001223 CustomContentCallbacks callbacks, String description) {
1224 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001225 }
1226
Adam Cohenedb40762013-07-18 16:45:45 -07001227 // The custom content needs to offset its content to account for the QSB
1228 public int getTopOffsetForCustomContent() {
1229 return mWorkspace.getPaddingTop();
1230 }
1231
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001232 @Override
1233 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001235 if (mModel.isCurrentCallbacks(this)) {
1236 mModel.stopLoader();
1237 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001238 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1239
Romain Guy13c2e7b2010-03-10 19:45:00 -08001240 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001241 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001243 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001244 @Override
1245 public void onWindowFocusChanged(boolean hasFocus) {
1246 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001247 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001248
1249 if (mLauncherCallbacks != null) {
1250 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1251 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001252 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 private boolean acceptFilter() {
1255 final InputMethodManager inputManager = (InputMethodManager)
1256 getSystemService(Context.INPUT_METHOD_SERVICE);
1257 return !inputManager.isFullscreenMode();
1258 }
1259
1260 @Override
1261 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001262 final int uniChar = event.getUnicodeChar();
1263 final boolean handled = super.onKeyDown(keyCode, event);
1264 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1265 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1267 keyCode, event);
1268 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001269 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001270 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 // showSearchDialog()
1272 // If there are multiple keystrokes before the search dialog takes focus,
1273 // onSearchRequested() will be called for every keystroke,
1274 // but it is idempotent, so it's fine.
1275 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
1277 }
1278
Joe Onorato8a9625e2010-01-28 15:55:35 -08001279 // Eat the long press event so the keyboard doesn't come up.
1280 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1281 return true;
1282 }
1283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 return handled;
1285 }
1286
Karl Rosaen138a0412009-04-23 19:00:21 -07001287 private String getTypedText() {
1288 return mDefaultKeySsb.toString();
1289 }
1290
1291 private void clearTypedText() {
1292 mDefaultKeySsb.clear();
1293 mDefaultKeySsb.clearSpans();
1294 Selection.setSelection(mDefaultKeySsb, 0);
1295 }
1296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001298 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1299 * State
1300 */
1301 private static State intToState(int stateOrdinal) {
1302 State state = State.WORKSPACE;
1303 final State[] stateValues = State.values();
1304 for (int i = 0; i < stateValues.length; i++) {
1305 if (stateValues[i].ordinal() == stateOrdinal) {
1306 state = stateValues[i];
1307 break;
1308 }
1309 }
1310 return state;
1311 }
1312
1313 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 * Restores the previous state, if it exists.
1315 *
1316 * @param savedState The previous state.
1317 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001318 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 private void restoreState(Bundle savedState) {
1320 if (savedState == null) {
1321 return;
1322 }
1323
Michael Jurka883f55b2010-10-21 15:47:14 -07001324 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001325 if (state == State.APPS || state == State.WIDGETS) {
1326 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001327 }
1328
Adam Cohen21cd0022013-10-09 18:57:02 -07001329 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1330 PagedView.INVALID_RESTORE_PAGE);
1331 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001332 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
Winson Chung3d503fb2011-07-13 17:25:49 -07001335 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001336 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1339 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001340 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001341 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1342 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001343 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1344 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001345 AppWidgetProviderInfo info = savedState.getParcelable(
1346 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1347 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001348 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001349 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 mRestoring = true;
1351 }
1352
1353 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1354 if (renameFolder) {
1355 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001356 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 mRestoring = true;
1358 }
Winson Chunga12a2502010-12-20 14:41:35 -08001359
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001360 mItemIdToViewId = (HashMap<Integer, Integer>)
1361 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363
1364 /**
1365 * Finds all the views we need and configure them properly.
1366 */
1367 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001368 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001369
Craig Mautner360310b2012-10-26 15:13:08 -07001370 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001371 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001372 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1373 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001374 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001375 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001376
John Spurlock77e1f472013-09-11 10:09:51 -04001377 mLauncherView.setSystemUiVisibility(
1378 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001379 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380
Winson Chung4c98d922011-05-31 16:50:48 -07001381 // Setup the drag layer
1382 mDragLayer.setup(this, dragController);
1383
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 // Setup the hotseat
1385 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1386 if (mHotseat != null) {
1387 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001388 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001389 }
1390
Jorim Jaggid017f882014-01-14 17:08:48 -08001391 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001392 View widgetButton = findViewById(R.id.widget_button);
1393 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001394 @Override
1395 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001396 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001397 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001398 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001399 }
1400 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001401 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1402
1403 View wallpaperButton = findViewById(R.id.wallpaper_button);
1404 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001405 @Override
1406 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001407 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001408 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001410 }
1411 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001412 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1413
1414 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001415 if (hasSettings()) {
1416 settingsButton.setOnClickListener(new OnClickListener() {
1417 @Override
1418 public void onClick(View arg0) {
1419 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001420 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001421 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001422 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001423 });
1424 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425 } else {
1426 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001427 }
1428
Adam Cohendbdff6b2013-09-18 19:09:15 -07001429 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430
Winson Chung4c98d922011-05-31 16:50:48 -07001431 // Setup the workspace
1432 mWorkspace.setHapticFeedbackEnabled(false);
1433 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001434 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001435 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001436
Winson Chungf0ea4d32011-06-06 14:27:16 -07001437 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001438 mSearchDropTargetBar = (SearchDropTargetBar)
1439 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001440
Winson Chungb745afb2015-03-02 11:51:23 -08001441 // Setup Apps
1442 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001443 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1444 mAppsView.hideSearchBar();
1445 }
Winson Chungb745afb2015-03-02 11:51:23 -08001446
Winson Chungf0ea4d32011-06-06 14:27:16 -07001447 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001448 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001449
Winson Chung3d503fb2011-07-13 17:25:49 -07001450 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001451 dragController.setDragScoller(mWorkspace);
1452 dragController.setScrollView(mDragLayer);
1453 dragController.setMoveTarget(mWorkspace);
1454 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001455 if (mSearchDropTargetBar != null) {
1456 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001457 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001458 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001459
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001460 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001461 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001462 mWeightWatcher = new WeightWatcher(this);
1463 mWeightWatcher.setAlpha(0.5f);
1464 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001465 new FrameLayout.LayoutParams(
1466 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001467 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001468 Gravity.BOTTOM)
1469 );
Adam Cohen39a06042013-07-19 14:30:12 -07001470
1471 boolean show = shouldShowWeightWatcher();
1472 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001477 * Sets the all apps button. This method is called from {@link Hotseat}.
1478 */
1479 public void setAllAppsButton(View allAppsButton) {
1480 mAllAppsButton = allAppsButton;
1481 }
1482
1483 public View getAllAppsButton() {
1484 return mAllAppsButton;
1485 }
1486
1487 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 * Creates a view representing a shortcut.
1489 *
1490 * @param info The data structure describing the shortcut.
1491 *
1492 * @return A View inflated from R.layout.application.
1493 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001494 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001496 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
1499 /**
1500 * Creates a view representing a shortcut inflated from the specified resource.
1501 *
1502 * @param layoutResId The id of the XML layout used to create the shortcut.
1503 * @param parent The group the shortcut belongs to.
1504 * @param info The data structure describing the shortcut.
1505 *
1506 * @return A View inflated from layoutResId.
1507 */
Adam Cohen59400422014-03-05 18:07:04 -08001508 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001509 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001510 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001512 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return favorite;
1514 }
1515
1516 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 * Add a shortcut to the workspace.
1518 *
1519 * @param data The intent describing the shortcut.
1520 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001523 int cellY) {
1524 int[] cellXY = mTmpAddItemCellCoordinates;
1525 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001526 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529
Sunny Goyal2350bc92014-10-14 16:42:54 -07001530 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001531 if (info == null) {
1532 return;
1533 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 final View view = createShortcut(info);
1535
Adam Cohenfbba09b2011-07-18 21:43:05 -07001536 // First we check if we already know the exact location where we want to add this item.
1537 if (cellX >= 0 && cellY >= 0) {
1538 cellXY[0] = cellX;
1539 cellXY[1] = cellY;
1540 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001541
1542 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001543 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001544 true, null,null)) {
1545 return;
1546 }
1547 DragObject dragObject = new DragObject();
1548 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001549 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1550 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001551 return;
1552 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001554 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001555 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001556 foundCellSpan = (result != null);
1557 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001558 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001559 }
1560
1561 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001562 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 return;
1564 }
1565
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001566 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
1568 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001570 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
1573
Adam Cohen2f093b62012-04-30 18:59:53 -07001574 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1575 int minHeight) {
1576 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001577 // We want to account for the extra amount of padding that we are adding to the widget
1578 // to ensure that it gets the full amount of space that it has requested
1579 int requiredWidth = minWidth + padding.left + padding.right;
1580 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001581 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001582 }
1583
Adam Cohen2f093b62012-04-30 18:59:53 -07001584 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1585 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001586 }
1587
Adam Cohen2f093b62012-04-30 18:59:53 -07001588 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1589 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001590 }
1591
Adam Cohen2f093b62012-04-30 18:59:53 -07001592 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1593 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001594 }
1595
Adam Cohen2f093b62012-04-30 18:59:53 -07001596 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1597 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001598 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001599 }
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001602 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001604 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 */
Adam Cohen091440a2015-03-18 14:16:05 -07001606 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001607 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1608
1609 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001611 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1612 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 }
Romain Guycbb89e42009-06-08 15:52:54 -07001614
Adam Cohen59400422014-03-05 18:07:04 -08001615 if (appWidgetInfo.isCustomWidget) {
1616 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001617 }
1618
Adam Cohen59400422014-03-05 18:07:04 -08001619 LauncherAppWidgetInfo launcherInfo;
1620 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1621 launcherInfo.spanX = info.spanX;
1622 launcherInfo.spanY = info.spanY;
1623 launcherInfo.minSpanX = info.minSpanX;
1624 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001625 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001628 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629
1630 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 if (hostView == null) {
1632 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001633 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1634 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 } else {
1636 // The AppWidgetHostView has already been inflated and instantiated
1637 launcherInfo.hostView = hostView;
1638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001640 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001641 launcherInfo.notifyWidgetSizeChanged(this);
1642
Adam Cohen59400422014-03-05 18:07:04 -08001643 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1644 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001645
1646 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001648 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1652 @Override
1653 public void onReceive(Context context, Intent intent) {
1654 final String action = intent.getAction();
1655 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1656 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001657 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001658 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001659
Winson Chungc100e8e2011-08-09 16:02:43 -07001660 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001661 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001662 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001663 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001664 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001665 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1667 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001668 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001669 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1670 mModel.resetLoadedState(false, true);
1671 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1672 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1673 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1674 mModel.resetLoadedState(false, true);
1675 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1676 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1677 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001678 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1679 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1680 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 }
1682 }
1683 };
1684
1685 @Override
1686 public void onAttachedToWindow() {
1687 super.onAttachedToWindow();
1688
1689 // Listen for broadcasts related to user-presence
1690 final IntentFilter filter = new IntentFilter();
1691 filter.addAction(Intent.ACTION_SCREEN_OFF);
1692 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001693 // For handling managed profiles
1694 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1695 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001696 if (ENABLE_DEBUG_INTENTS) {
1697 filter.addAction(DebugIntents.DELETE_DATABASE);
1698 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1699 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001701 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001702 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001703 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 mVisible = true;
1705 }
1706
Adam Cohen0b395352014-06-09 22:54:36 +00001707 /**
1708 * Sets up transparent navigation and status bars in LMP.
1709 * This method is a no-op for other platform versions.
1710 */
Sunny Goyald0091012015-01-20 15:55:34 -08001711 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001712 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001713 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001714 Window window = getWindow();
1715 window.getAttributes().systemUiVisibility |=
1716 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1717 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1718 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1719 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1720 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1721 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1722 window.setStatusBarColor(Color.TRANSPARENT);
1723 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001724 }
1725 }
1726
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 @Override
1728 public void onDetachedFromWindow() {
1729 super.onDetachedFromWindow();
1730 mVisible = false;
1731
Adam Cohend113e0c2010-11-11 10:48:05 -08001732 if (mAttached) {
1733 unregisterReceiver(mReceiver);
1734 mAttached = false;
1735 }
Winson Chungb745afb2015-03-02 11:51:23 -08001736 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 }
1738
1739 public void onWindowVisibilityChanged(int visibility) {
1740 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001741 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001742 // The following code used to be in onResume, but it turns out onResume is called when
1743 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1744 // is a more appropriate event to handle
1745 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001746 if (!mWorkspaceLoading) {
1747 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 // We want to let Launcher draw itself at least once before we force it to build
1749 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001750 // apps is nice and speedy.
1751 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001753 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001754 if (mStarted) return;
1755 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001756 // We delay the layer building a bit in order to give
1757 // other message processing a time to run. In particular
1758 // this avoids a delay in hiding the IME if it was
1759 // currently shown, because doing that may involve
1760 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001761 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001762 final ViewTreeObserver.OnDrawListener listener = this;
1763 mWorkspace.post(new Runnable() {
1764 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001765 if (mWorkspace != null &&
1766 mWorkspace.getViewTreeObserver() != null) {
1767 mWorkspace.getViewTreeObserver().
1768 removeOnDrawListener(listener);
1769 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001770 }
1771 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001772 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001773 }
1774 });
1775 }
1776 clearTypedText();
1777 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
1779
Adam Cohen091440a2015-03-18 14:16:05 -07001780 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 mHandler.removeMessages(ADVANCE_MSG);
1782 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1783 mHandler.sendMessageDelayed(msg, delay);
1784 mAutoAdvanceSentTime = System.currentTimeMillis();
1785 }
1786
Adam Cohen091440a2015-03-18 14:16:05 -07001787 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1789 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1790 mAutoAdvanceRunning = autoAdvanceRunning;
1791 if (autoAdvanceRunning) {
1792 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1793 sendAdvanceMessage(delay);
1794 } else {
1795 if (!mWidgetsToAdvance.isEmpty()) {
1796 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1797 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1798 }
1799 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001800 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 }
1802 }
1803 }
1804
1805 private final Handler mHandler = new Handler() {
1806 @Override
1807 public void handleMessage(Message msg) {
1808 if (msg.what == ADVANCE_MSG) {
1809 int i = 0;
1810 for (View key: mWidgetsToAdvance.keySet()) {
1811 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1812 final int delay = mAdvanceStagger * i;
1813 if (v instanceof Advanceable) {
1814 postDelayed(new Runnable() {
1815 public void run() {
1816 ((Advanceable) v).advance();
1817 }
1818 }, delay);
1819 }
1820 i++;
1821 }
1822 sendAdvanceMessage(mAdvanceInterval);
1823 }
1824 }
1825 };
1826
1827 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001828 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001829 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1830 if (v instanceof Advanceable) {
1831 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001832 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001833 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001834 }
1835 }
1836
1837 void removeWidgetToAutoAdvance(View hostView) {
1838 if (mWidgetsToAdvance.containsKey(hostView)) {
1839 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001840 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001841 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 }
1843
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001845 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 launcherInfo.hostView = null;
1847 }
1848
Hyunyoung Song3f471442015-04-08 19:01:34 -07001849 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001850 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1851 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 }
1853
Winson Chunga6945242014-01-08 14:04:34 -08001854 public DragLayer getDragLayer() {
1855 return mDragLayer;
1856 }
1857
Winson Chungb745afb2015-03-02 11:51:23 -08001858 public AppsContainerView getAppsView() {
1859 return mAppsView;
1860 }
1861
Hyunyoung Song3f471442015-04-08 19:01:34 -07001862 public WidgetsContainerView getWidgetsView() {
1863 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001864 }
1865
Winson Chunga6945242014-01-08 14:04:34 -08001866 public Workspace getWorkspace() {
1867 return mWorkspace;
1868 }
1869
1870 public Hotseat getHotseat() {
1871 return mHotseat;
1872 }
1873
Jorim Jaggid017f882014-01-14 17:08:48 -08001874 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001875 return mOverviewPanel;
1876 }
1877
1878 public SearchDropTargetBar getSearchBar() {
1879 return mSearchDropTargetBar;
1880 }
1881
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001882 public LauncherAppWidgetHost getAppWidgetHost() {
1883 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
Romain Guycbb89e42009-06-08 15:52:54 -07001885
Winson Chunga9abd0e2010-10-27 17:18:37 -07001886 public LauncherModel getModel() {
1887 return mModel;
1888 }
1889
Winson Chunga6945242014-01-08 14:04:34 -08001890 protected SharedPreferences getSharedPrefs() {
1891 return mSharedPrefs;
1892 }
1893
Bjorn Bringertc459e522013-06-07 19:36:01 +01001894 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001895 getWindow().closeAllPanels();
1896
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001897 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001898 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001899 }
1900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 @Override
1902 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001903 long startTime = 0;
1904 if (DEBUG_RESUME_TIME) {
1905 startTime = System.currentTimeMillis();
1906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 super.onNewIntent(intent);
1908
1909 // Close the menu
1910 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001911 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001912 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001913
Adam Cohened307df2013-10-02 09:37:31 -07001914 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1915 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1916 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001917
Adam Cohen6fecd412013-10-02 17:41:50 -07001918 if (mWorkspace == null) {
1919 // Can be cases where mWorkspace is null, this prevents a NPE
1920 return;
1921 }
1922 Folder openFolder = mWorkspace.getOpenFolder();
1923 // In all these cases, only animate if we're already on home
1924 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001925
1926 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1927 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001929 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001931 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001932
Adam Cohen6fecd412013-10-02 17:41:50 -07001933 closeFolder();
1934 exitSpringLoadedDragMode();
1935
1936 // If we are already on home, then just animate back to the workspace,
1937 // otherwise, just wait until onResume to set the state back to Workspace
1938 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001939 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001940 } else {
1941 mOnResumeState = State.WORKSPACE;
1942 }
1943
1944 final View v = getWindow().peekDecorView();
1945 if (v != null && v.getWindowToken() != null) {
1946 InputMethodManager imm = (InputMethodManager)getSystemService(
1947 INPUT_METHOD_SERVICE);
1948 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1949 }
1950
Winson Chungb745afb2015-03-02 11:51:23 -08001951 // Reset the apps view
1952 if (!alreadyOnHome && mAppsView != null) {
1953 mAppsView.scrollToTop();
1954 }
1955
Hyunyoung Song3f471442015-04-08 19:01:34 -07001956 // Reset the widgets view
1957 if (!alreadyOnHome && mWidgetsView != null) {
1958 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001959 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onHomeIntent();
1963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
Adam Cohened307df2013-10-02 09:37:31 -07001965
Michael Jurka447bf842013-05-15 14:52:15 +02001966 if (DEBUG_RESUME_TIME) {
1967 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969
Adam Cohen9211d422014-10-07 18:14:53 -07001970 if (mLauncherCallbacks != null) {
1971 mLauncherCallbacks.onNewIntent(intent);
1972 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001976 public void onRestoreInstanceState(Bundle state) {
1977 super.onRestoreInstanceState(state);
1978 for (int page: mSynchronouslyBoundPages) {
1979 mWorkspace.restoreInstanceStateForChild(page);
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
1983 @Override
1984 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001985 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001986 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1987 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001988 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001989 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990
Michael Jurka883f55b2010-10-21 15:47:14 -07001991 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001992 // We close any open folder since it will not be re-opened, and we need to make sure
1993 // this state is reflected.
1994 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995
Adam Cohendcd297f2013-06-18 13:13:40 -07001996 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 mWaitingForResult) {
1998 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001999 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002000 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2001 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002002 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2004 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002005 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007
2008 if (mFolderInfo != null && mWaitingForResult) {
2009 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2010 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2011 }
Michael Jurka946ad472010-07-09 18:05:18 -07002012
Hyunyoung Song3f471442015-04-08 19:01:34 -07002013 // Save the current widgets tray?
2014 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002015 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002016
2017 if (mLauncherCallbacks != null) {
2018 mLauncherCallbacks.onSaveInstanceState(outState);
2019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
2022 @Override
2023 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002025
Winson Chunge7a03942011-08-05 15:05:12 -07002026 // Remove all pending runnables
2027 mHandler.removeMessages(ADVANCE_MSG);
2028 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002029 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002030
Winson Chungcd2b0142011-06-08 16:02:26 -07002031 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002032 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002033
2034 // It's possible to receive onDestroy after a new Launcher activity has
2035 // been created. In this case, don't interfere with the new Launcher.
2036 if (mModel.isCurrentCallbacks(this)) {
2037 mModel.stopLoader();
2038 app.setLauncher(null);
2039 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002042 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002044 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002046 mAppWidgetHost = null;
2047
2048 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049
2050 TextKeyListener.getInstance().release();
2051
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002052 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002053 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002055 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002056 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002057 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058 mWorkspace = null;
2059 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002060
Michael Jurka2ecf9952012-06-18 12:52:28 -07002061 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002062
2063 if (mLauncherCallbacks != null) {
2064 mLauncherCallbacks.onDestroy();
2065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067
Adam Cohena9cf38f2011-05-02 15:36:58 -07002068 public DragController getDragController() {
2069 return mDragController;
2070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 @Override
2073 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002074 onStartForResult(requestCode);
2075 super.startActivityForResult(intent, requestCode);
2076 }
2077
2078 @Override
2079 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2080 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2081 onStartForResult(requestCode);
2082 try {
2083 super.startIntentSenderForResult(intent, requestCode,
2084 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2085 } catch (IntentSender.SendIntentException e) {
2086 throw new ActivityNotFoundException();
2087 }
2088 }
2089
2090 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002091 if (requestCode >= 0) {
2092 setWaitingForResult(true);
2093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
Winson Chung70d72102011-08-12 11:24:35 -07002096 /**
2097 * Indicates that we want global search for this activity by setting the globalSearch
2098 * argument for {@link #startSearch} to true.
2099 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002101 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002103
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002104 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002105
Karl Rosaen138a0412009-04-23 19:00:21 -07002106 if (initialQuery == null) {
2107 // Use any text typed in the launcher as the initial query
2108 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 if (appSearchData == null) {
2111 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002112 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
Winson Chungadf0c182012-08-23 14:59:07 -07002114 Rect sourceBounds = new Rect();
2115 if (mSearchDropTargetBar != null) {
2116 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2117 }
Romain Guycbb89e42009-06-08 15:52:54 -07002118
Ian Parkinson047036e2014-09-01 15:40:53 +01002119 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002120 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002121 if (clearTextImmediately) {
2122 clearTypedText();
2123 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002124 }
2125
Ian Parkinson047036e2014-09-01 15:40:53 +01002126 /**
2127 * Start a text search.
2128 *
2129 * @return {@code true} if the search will start immediately, so any further keypresses
2130 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2131 * to buffer keypresses.
2132 */
2133 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002134 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002135 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2136 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2137 sourceBounds);
2138 }
2139
Michael Jurkaa33411c2012-06-14 16:18:21 -07002140 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002142 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002143 }
2144
2145 /**
2146 * Starts the global search activity. This code is a copied from SearchManager
2147 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002149 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002150 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002151 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2152 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2153 if (globalSearchActivity == null) {
2154 Log.w(TAG, "No global search activity found.");
2155 return;
2156 }
2157 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2158 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2159 intent.setComponent(globalSearchActivity);
2160 // Make sure that we have a Bundle to put source in
2161 if (appSearchData == null) {
2162 appSearchData = new Bundle();
2163 } else {
2164 appSearchData = new Bundle(appSearchData);
2165 }
Adam Cohen9211d422014-10-07 18:14:53 -07002166 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002167 if (!appSearchData.containsKey("source")) {
2168 appSearchData.putString("source", getPackageName());
2169 }
2170 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2171 if (!TextUtils.isEmpty(initialQuery)) {
2172 intent.putExtra(SearchManager.QUERY, initialQuery);
2173 }
2174 if (selectInitialQuery) {
2175 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2176 }
2177 intent.setSourceBounds(sourceBounds);
2178 try {
2179 startActivity(intent);
2180 } catch (ActivityNotFoundException ex) {
2181 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 }
2184
Yura4f93ec62014-02-04 14:15:21 +00002185 public boolean isOnCustomContent() {
2186 return mWorkspace.isOnOrMovingToCustomContent();
2187 }
2188
Winson Chung70d72102011-08-12 11:24:35 -07002189 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002190 public boolean onPrepareOptionsMenu(Menu menu) {
2191 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002192 if (!isOnCustomContent()) {
2193 // Close any open folders
2194 closeFolder();
2195 // Stop resizing any widgets
2196 mWorkspace.exitWidgetResizeMode();
2197 if (!mWorkspace.isInOverviewMode()) {
2198 // Show the overview mode
2199 showOverviewMode(true);
2200 } else {
2201 showWorkspace(true);
2202 }
Winson Chunge0298742014-01-17 12:03:00 -08002203 }
Adam Cohen9211d422014-10-07 18:14:53 -07002204 if (mLauncherCallbacks != null) {
2205 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2206 }
2207
Michael Jurkab94f3f82013-09-11 18:08:54 +02002208 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002211 @Override
2212 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002213 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002214 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002215 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002216 }
2217
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 public boolean isWorkspaceLocked() {
2219 return mWorkspaceLoading || mWaitingForResult;
2220 }
2221
Adam Cohen517a7f52014-03-01 12:12:59 -08002222 public boolean isWorkspaceLoading() {
2223 return mWorkspaceLoading;
2224 }
2225
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002226 private void setWorkspaceLoading(boolean value) {
2227 boolean isLocked = isWorkspaceLocked();
2228 mWorkspaceLoading = value;
2229 if (isLocked != isWorkspaceLocked()) {
2230 onWorkspaceLockedChanged();
2231 }
2232 }
2233
2234 private void setWaitingForResult(boolean value) {
2235 boolean isLocked = isWorkspaceLocked();
2236 mWaitingForResult = value;
2237 if (isLocked != isWorkspaceLocked()) {
2238 onWorkspaceLockedChanged();
2239 }
2240 }
2241
Adam Cohen9211d422014-10-07 18:14:53 -07002242 protected void onWorkspaceLockedChanged() {
2243 if (mLauncherCallbacks != null) {
2244 mLauncherCallbacks.onWorkspaceLockedChanged();
2245 }
2246 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002247
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2252 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002253 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002256
Sunny Goyale6b63a32015-01-16 16:14:29 -08002257 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002258 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002259 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2260 }
2261
Sunny Goyale6b63a32015-01-16 16:14:29 -08002262 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002263 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2264 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002265 if (appWidgetInfo.configure != null) {
2266 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002267 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002268
Bjorn Bringert7984c942009-12-09 15:38:25 +00002269 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002270 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2271 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002274 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002275 Runnable onComplete = new Runnable() {
2276 @Override
2277 public void run() {
2278 // Exit spring loaded mode if necessary after adding the widget
2279 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2280 null);
2281 }
2282 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002283 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002284 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002285 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 }
2287 }
Romain Guycbb89e42009-06-08 15:52:54 -07002288
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002289 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002290 // Close any folders that may be open.
2291 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002292 mWorkspace.moveToCustomContentScreen(animate);
2293 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002294
2295 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2296 int[] cell, int spanX, int spanY) {
2297 switch (info.itemType) {
2298 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2299 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2300 int span[] = new int[2];
2301 span[0] = spanX;
2302 span[1] = spanY;
2303 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2304 container, screenId, cell, span);
2305 break;
2306 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2307 processShortcutFromDrop(info.componentName, container, screenId, cell);
2308 break;
2309 default:
2310 throw new IllegalStateException("Unknown item type: " + info.itemType);
2311 }
2312 }
2313
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 /**
2315 * Process a shortcut drop.
2316 *
2317 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002318 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002321 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2322 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002323 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002326 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327
2328 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 mPendingAddInfo.cellX = cell[0];
2330 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002332
2333 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2334 createShortcutIntent.setComponent(componentName);
2335 processShortcut(createShortcutIntent);
2336 }
2337
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 /**
2339 * Process a widget drop.
2340 *
2341 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002345 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2346 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002350 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002351 mPendingAddInfo.minSpanX = info.minSpanX;
2352 mPendingAddInfo.minSpanY = info.minSpanY;
2353
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 mPendingAddInfo.cellX = cell[0];
2356 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002358 if (span != null) {
2359 mPendingAddInfo.spanX = span[0];
2360 mPendingAddInfo.spanY = span[1];
2361 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362
Adam Cohened66b2b2012-01-23 17:28:51 -08002363 AppWidgetHostView hostView = info.boundWidget;
2364 int appWidgetId;
2365 if (hostView != null) {
2366 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002367 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002369 // In this case, we either need to start an activity to get permission to bind
2370 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002371 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002372 Bundle options = info.bindOptions;
2373
Sunny Goyalffe83f12014-08-14 17:39:34 -07002374 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2375 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002376 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002378 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002379 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002380 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2382 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002383 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2384 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002385 // TODO: we need to make sure that this accounts for the options bundle.
2386 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002387 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2388 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002389 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002390 }
2391
Joe Onoratodeb98af2010-02-19 14:59:39 -08002392 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002393 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
2395
Winson Chung24ab2f12010-09-16 14:10:47 -07002396 void processWallpaper(Intent intent) {
2397 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2398 }
2399
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002401 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002402 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 folderInfo.title = getText(R.string.folder_name);
2404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002406 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2407 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002408 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409
2410 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002411 FolderIcon newFolder =
2412 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002415 // Force measure the new folder icon
2416 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2417 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002418 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 }
Romain Guycbb89e42009-06-08 15:52:54 -07002420
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002422 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002423 }
2424
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002425 /**
2426 * Registers various content observers. The current implementation registers
2427 * only a favorites observer to keep track of the favorites applications.
2428 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002429 private void registerContentObservers() {
2430 ContentResolver resolver = getContentResolver();
2431 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2432 true, mWidgetObserver);
2433 }
2434
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 @Override
2436 public boolean dispatchKeyEvent(KeyEvent event) {
2437 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2438 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002440 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002441 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002442 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002443 dumpState();
2444 return true;
2445 }
2446 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002447 }
2448 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2449 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002450 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 return true;
2452 }
2453 }
2454
2455 return super.dispatchKeyEvent(event);
2456 }
2457
Joe Onorato88ec0992009-11-19 13:16:06 -08002458 @Override
2459 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002460 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2461 return;
2462 }
2463
Winson Chung3d9490a2015-03-24 17:38:42 -07002464 LauncherAccessibilityDelegate delegate =
2465 LauncherAppState.getInstance().getAccessibilityDelegate();
2466 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002467 return;
2468 }
2469
Winson Chungb745afb2015-03-02 11:51:23 -08002470 if (isAppsViewVisible()) {
2471 showWorkspace(true);
2472 } else if (isWidgetsViewVisible()) {
2473 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002474 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002475 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002477 Folder openFolder = mWorkspace.getOpenFolder();
2478 if (openFolder.isEditingName()) {
2479 openFolder.dismissEditingName();
2480 } else {
2481 closeFolder();
2482 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002484 mWorkspace.exitWidgetResizeMode();
2485
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002486 // Back button is a no-op here, but give at least some feedback for the button press
2487 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002488 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002492 * Re-listen when widgets are reset.
2493 */
Adam Cohen091440a2015-03-18 14:16:05 -07002494 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002495 if (mAppWidgetHost != null) {
2496 mAppWidgetHost.startListening();
2497 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002498 }
2499
2500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 * Launches the intent referred by the clicked shortcut.
2502 *
2503 * @param v The view representing the clicked shortcut.
2504 */
2505 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002506 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2507 // view has detached (it's possible for this to happen if the view is removed mid touch).
2508 if (v.getWindowToken() == null) {
2509 return;
2510 }
2511
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002512 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 return;
2514 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002515
Adam Cohen1697b792013-09-17 19:08:21 -07002516 if (v instanceof Workspace) {
2517 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002518 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 }
2520 return;
2521 }
2522
2523 if (v instanceof CellLayout) {
2524 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002525 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 }
2528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002530 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002533 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002534 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002535 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002536 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002538 } else if (tag instanceof AppInfo) {
2539 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002540 } else if (tag instanceof LauncherAppWidgetInfo) {
2541 if (v instanceof PendingAppWidgetHostView) {
2542 onClickPendingWidget((PendingAppWidgetHostView) v);
2543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 }
2545 }
2546
Sunny Goyal508da152014-08-14 10:53:27 -07002547 public void onClickPagedViewIcon(View v) {
2548 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002549 if (mLauncherCallbacks != null) {
2550 mLauncherCallbacks.onClickPagedViewIcon(v);
2551 }
Sunny Goyal508da152014-08-14 10:53:27 -07002552 }
2553
Michael Jurka0e260592010-06-30 17:07:39 -07002554 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002555 return false;
2556 }
2557
Michael Jurkaaf442092010-06-10 17:01:57 -07002558 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002559 * Event handler for the app widget view which has not fully restored.
2560 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002561 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002562 if (mIsSafeModeEnabled) {
2563 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2564 return;
2565 }
2566
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002568 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002569 int widgetId = info.appWidgetId;
2570 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2571 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002572 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2573 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 mPendingAddInfo.copyFrom(info);
2575 mPendingAddWidgetId = widgetId;
2576
Sunny Goyalffe83f12014-08-14 17:39:34 -07002577 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2578 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002579 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002580 } else if (info.installProgress < 0) {
2581 // The install has not been queued
2582 final String packageName = info.providerName.getPackageName();
2583 showBrokenAppInstallDialog(packageName,
2584 new DialogInterface.OnClickListener() {
2585 public void onClick(DialogInterface dialog, int id) {
2586 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2587 }
2588 });
2589 } else {
2590 // Download has started.
2591 final String packageName = info.providerName.getPackageName();
2592 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002593 }
2594 }
2595
2596 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002597 * Event handler for the "grid" button that appears on the home screen, which
2598 * enters all apps mode.
2599 *
2600 * @param v The view that was clicked.
2601 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 protected void onClickAllAppsButton(View v) {
2603 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002604 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 showWorkspace(true);
2606 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002607 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 }
2609 }
2610
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 private void showBrokenAppInstallDialog(final String packageName,
2612 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002613 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002614 .setTitle(R.string.abandoned_promises_title)
2615 .setMessage(R.string.abandoned_promise_explanation)
2616 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2617 .setNeutralButton(R.string.abandoned_clean_this,
2618 new DialogInterface.OnClickListener() {
2619 public void onClick(DialogInterface dialog, int id) {
2620 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2621 mWorkspace.removeAbandonedPromise(packageName, user);
2622 }
2623 })
2624 .create().show();
2625 return;
2626 }
2627
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002628 /**
2629 * Event handler for an app shortcut click.
2630 *
2631 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2632 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002633 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2635 Object tag = v.getTag();
2636 if (!(tag instanceof ShortcutInfo)) {
2637 throw new IllegalArgumentException("Input must be a Shortcut");
2638 }
2639
2640 // Open shortcut
2641 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002642
2643 if (shortcut.isDisabled != 0) {
2644 int error = R.string.activity_not_available;
2645 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2646 error = R.string.safemode_shortcut_error;
2647 }
2648 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2649 return;
2650 }
2651
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 final Intent intent = shortcut.intent;
2653
2654 // Check for special shortcuts
2655 if (intent.getComponent() != null) {
2656 final String shortcutClass = intent.getComponent().getClassName();
2657
2658 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2659 MemoryDumpActivity.startDump(this);
2660 return;
2661 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2662 toggleShowWeightWatcher();
2663 return;
2664 }
2665 }
2666
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002668 if ((v instanceof BubbleTextView)
2669 && shortcut.isPromise()
2670 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002671 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002672 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 new DialogInterface.OnClickListener() {
2674 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002675 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 return;
2679 }
2680
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002681 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002682 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002683
2684 if (mLauncherCallbacks != null) {
2685 mLauncherCallbacks.onClickAppShortcut(v);
2686 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 }
2688
Adam Cohen091440a2015-03-18 14:16:05 -07002689 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002691 final ShortcutInfo shortcut;
2692 final Intent intent;
2693 if (tag instanceof ShortcutInfo) {
2694 shortcut = (ShortcutInfo) tag;
2695 intent = shortcut.intent;
2696 int[] pos = new int[2];
2697 v.getLocationOnScreen(pos);
2698 intent.setSourceBounds(new Rect(pos[0], pos[1],
2699 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002700
Sunny Goyal508da152014-08-14 10:53:27 -07002701 } else if (tag instanceof AppInfo) {
2702 shortcut = null;
2703 intent = ((AppInfo) tag).intent;
2704 } else {
2705 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2706 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707
2708 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002709 mStats.recordLaunch(intent, shortcut);
2710
2711 if (success && v instanceof BubbleTextView) {
2712 mWaitingForResume = (BubbleTextView) v;
2713 mWaitingForResume.setStayPressed(true);
2714 }
2715 }
2716
2717 /**
2718 * Event handler for a folder icon click.
2719 *
2720 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2721 */
2722 protected void onClickFolderIcon(View v) {
2723 if (LOGD) Log.d(TAG, "onClickFolder");
2724 if (!(v instanceof FolderIcon)){
2725 throw new IllegalArgumentException("Input must be a FolderIcon");
2726 }
2727
2728 FolderIcon folderIcon = (FolderIcon) v;
2729 final FolderInfo info = folderIcon.getFolderInfo();
2730 Folder openFolder = mWorkspace.getFolderForTag(info);
2731
2732 // If the folder info reports that the associated folder is open, then verify that
2733 // it is actually opened. There have been a few instances where this gets out of sync.
2734 if (info.opened && openFolder == null) {
2735 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2736 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2737 info.opened = false;
2738 }
2739
2740 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2741 // Close any open folder
2742 closeFolder();
2743 // Open the requested folder
2744 openFolder(folderIcon);
2745 } else {
2746 // Find the open folder...
2747 int folderScreen;
2748 if (openFolder != null) {
2749 folderScreen = mWorkspace.getPageForView(openFolder);
2750 // .. and close it
2751 closeFolder(openFolder);
2752 if (folderScreen != mWorkspace.getCurrentPage()) {
2753 // Close any folder open on the current screen
2754 closeFolder();
2755 // Pull the folder onto this screen
2756 openFolder(folderIcon);
2757 }
2758 }
2759 }
Adam Cohen9211d422014-10-07 18:14:53 -07002760
2761 if (mLauncherCallbacks != null) {
2762 mLauncherCallbacks.onClickFolderIcon(v);
2763 }
Michael Jurka5130e402011-10-13 04:55:35 -07002764 }
2765
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002766 /**
2767 * Event handler for the (Add) Widgets button that appears after a long press
2768 * on the home screen.
2769 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002770 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002771 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002772 if (mIsSafeModeEnabled) {
2773 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2774 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002775 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002776 if (mLauncherCallbacks != null) {
2777 mLauncherCallbacks.onClickAddWidgetButton(view);
2778 }
Adam Cohen9211d422014-10-07 18:14:53 -07002779 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002780 }
2781
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 /**
2783 * Event handler for the wallpaper picker button that appears after a long press
2784 * on the home screen.
2785 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002786 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002788 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2789 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002790
2791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onClickWallpaperPicker(v);
2793 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 }
2795
2796 /**
2797 * Event handler for a click on the settings button that appears after a long press
2798 * on the home screen.
2799 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002800 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002802 if (mLauncherCallbacks != null) {
2803 mLauncherCallbacks.onClickSettingsButton(v);
2804 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 }
2806
Michael Jurka5130e402011-10-13 04:55:35 -07002807 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002808 // Provide the same haptic feedback that the system offers for virtual keys.
2809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002810 }
2811
Adam Cohen61f560d2013-09-30 15:58:20 -07002812 public void performHapticFeedbackOnTouchDown(View v) {
2813 // Provide the same haptic feedback that the system offers for virtual keys.
2814 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2815 }
2816
2817 public View.OnTouchListener getHapticFeedbackTouchListener() {
2818 if (mHapticFeedbackTouchListener == null) {
2819 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2820 @Override
2821 public boolean onTouch(View v, MotionEvent event) {
2822 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825 return false;
2826 }
2827 };
2828 }
2829 return mHapticFeedbackTouchListener;
2830 }
2831
Adam Cohen9211d422014-10-07 18:14:53 -07002832 public void onDragStarted(View view) {
2833 if (isOnCustomContent()) {
2834 // Custom content screen doesn't participate in drag and drop. If on custom
2835 // content screen, move to default.
2836 moveWorkspaceToDefaultScreen();
2837 }
2838
2839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onDragStarted(view);
2841 }
2842 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002843
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002844 /**
2845 * Called when the user stops interacting with the launcher.
2846 * This implies that the user is now on the homescreen and is not doing housekeeping.
2847 */
Adam Cohen9211d422014-10-07 18:14:53 -07002848 protected void onInteractionEnd() {
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onInteractionEnd();
2851 }
2852 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853
2854 /**
2855 * Called when the user starts interacting with the launcher.
2856 * The possible interactions are:
2857 * - open all apps
2858 * - reorder an app shortcut, or a widget
2859 * - open the overview mode.
2860 * This is a good time to stop doing things that only make sense
2861 * when the user is on the homescreen and not doing housekeeping.
2862 */
Adam Cohen9211d422014-10-07 18:14:53 -07002863 protected void onInteractionBegin() {
2864 if (mLauncherCallbacks != null) {
2865 mLauncherCallbacks.onInteractionBegin();
2866 }
2867 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868
Kenny Guyf07af7b2014-07-31 11:39:16 +01002869 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002870 try {
2871 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002872 launcherApps.showAppDetailsForProfile(componentName, user);
2873 } catch (SecurityException e) {
2874 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2875 Log.e(TAG, "Launcher does not have permission to launch settings");
2876 } catch (ActivityNotFoundException e) {
2877 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2878 Log.e(TAG, "Unable to launch settings");
2879 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002880 }
2881
Michael Jurka1e2f4652013-07-08 18:03:46 -07002882 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002883 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2884 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002885 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002886 // System applications cannot be installed. For now, show a toast explaining that.
2887 // We may give them the option of disabling apps this way.
2888 int messageId = R.string.uninstall_system_app_text;
2889 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 String packageName = componentName.getPackageName();
2893 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 Intent intent = new Intent(
2895 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002896 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2897 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002898 if (user != null) {
2899 user.addToIntent(intent, Intent.EXTRA_USER);
2900 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002902 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002903 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002904 }
2905
Michael Jurka86a720e2012-05-09 11:23:23 -07002906 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002907 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002909 // Only launch using the new animation if the shortcut has not opted out (this is a
2910 // private contract between launcher and may be ignored in the future).
2911 boolean useLaunchAnimation = (v != null) &&
2912 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002913 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2914 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002915
Kenny Guy1317e2d2014-05-08 18:52:50 +01002916 UserHandleCompat user = null;
2917 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2918 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2919 user = userManager.getUserForSerialNumber(serialNumber);
2920 }
2921
2922 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002923 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002924 ActivityOptions opts = null;
2925 if (sClipRevealMethod != null) {
2926 // TODO: call method directly when Launcher3 can depend on M APIs
2927 int left = 0, top = 0;
2928 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2929 if (v instanceof TextView) {
2930 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002931 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2932 if (icon != null) {
2933 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002934 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002935 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002936 width = bounds.width();
2937 height = bounds.height();
2938 }
2939 }
2940 try {
2941 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2942 left, top, width, height);
2943 } catch (IllegalAccessException e) {
2944 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2945 sClipRevealMethod = null;
2946 } catch (InvocationTargetException e) {
2947 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2948 sClipRevealMethod = null;
2949 }
2950 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002951 if (opts == null && !Utilities.isLmpOrAbove()) {
2952 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002953 v.getMeasuredWidth(), v.getMeasuredHeight());
2954 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002955 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002956 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002957
2958 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2959 // Could be launching some bookkeeping activity
2960 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002961 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002962 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002963 launcherApps.startActivityForProfile(intent.getComponent(), user,
2964 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002965 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002966 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 } catch (SecurityException e) {
2968 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002969 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002971 "or use the exported attribute for this activity. "
2972 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002974 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002976
Michael Jurka86a720e2012-05-09 11:23:23 -07002977 boolean startActivitySafely(View v, Intent intent, Object tag) {
2978 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002979 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2980 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2981 return false;
2982 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002983 try {
2984 success = startActivity(v, intent, tag);
2985 } catch (ActivityNotFoundException e) {
2986 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2987 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2988 }
2989 return success;
2990 }
2991
Adam Cohen268c4752012-06-06 17:47:33 -07002992 /**
2993 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2994 * in the DragLayer in the exact absolute location of the original FolderIcon.
2995 */
2996 private void copyFolderIconToImage(FolderIcon fi) {
2997 final int width = fi.getMeasuredWidth();
2998 final int height = fi.getMeasuredHeight();
2999
3000 // Lazy load ImageView, Bitmap and Canvas
3001 if (mFolderIconImageView == null) {
3002 mFolderIconImageView = new ImageView(this);
3003 }
3004 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3005 mFolderIconBitmap.getHeight() != height) {
3006 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3007 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3008 }
3009
3010 DragLayer.LayoutParams lp;
3011 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3012 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3013 } else {
3014 lp = new DragLayer.LayoutParams(width, height);
3015 }
3016
Adam Cohen307fe232012-08-16 17:55:58 -07003017 // The layout from which the folder is being opened may be scaled, adjust the starting
3018 // view size by this scale factor.
3019 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003020 lp.customPosition = true;
3021 lp.x = mRectForFolderAnimation.left;
3022 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003023 lp.width = (int) (scale * width);
3024 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003025
3026 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3027 fi.draw(mFolderIconCanvas);
3028 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003029 if (fi.getFolder() != null) {
3030 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3031 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003032 }
Adam Cohen268c4752012-06-06 17:47:33 -07003033 // Just in case this image view is still in the drag layer from a previous animation,
3034 // we remove it and re-add it.
3035 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3036 mDragLayer.removeView(mFolderIconImageView);
3037 }
3038 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003039 if (fi.getFolder() != null) {
3040 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003041 }
Adam Cohen268c4752012-06-06 17:47:33 -07003042 }
3043
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003045 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003046 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3047 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3048 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3049
Adam Cohenc51934b2011-07-26 21:07:43 -07003050 FolderInfo info = (FolderInfo) fi.getTag();
3051 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3052 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003053 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3054 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003055 }
3056
Adam Cohen268c4752012-06-06 17:47:33 -07003057 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3058 copyFolderIconToImage(fi);
3059 fi.setVisibility(View.INVISIBLE);
3060
Michael Jurka2ecf9952012-06-18 12:52:28 -07003061 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003062 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003063 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003064 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3065 }
3066 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003067 oa.start();
3068 }
3069
Adam Cohen268c4752012-06-06 17:47:33 -07003070 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003071 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003072 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3073 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3074 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3075
Adam Cohen268c4752012-06-06 17:47:33 -07003076 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003077
Adam Cohen268c4752012-06-06 17:47:33 -07003078 // We remove and re-draw the FolderIcon in-case it has changed
3079 mDragLayer.removeView(mFolderIconImageView);
3080 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003081 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003082 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003083 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003084 oa.addListener(new AnimatorListenerAdapter() {
3085 @Override
3086 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003087 if (cl != null) {
3088 cl.clearFolderLeaveBehind();
3089 // Remove the ImageView copy of the FolderIcon and make the original visible.
3090 mDragLayer.removeView(mFolderIconImageView);
3091 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003092 }
3093 }
3094 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003095 oa.start();
3096 }
3097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003099 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 * is animated relative to the specified View. If the View is null, no animation
3101 * is played.
3102 *
3103 * @param folderInfo The FolderInfo describing the folder to open.
3104 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003105 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003106 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003107 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3108 if (openFolder != null && openFolder != folder) {
3109 // Close any open folder before opening a folder.
3110 closeFolder();
3111 }
3112
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114
Adam Cohena9cf38f2011-05-02 15:36:58 -07003115 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116
Sunny Goyalf4066152015-04-15 09:42:19 -07003117 // While the folder is open, the position of the icon cannot change.
3118 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3119
Adam Cohen4554ee12011-08-03 16:13:21 -07003120 // Just verify that the folder hasn't already been added to the DragLayer.
3121 // There was a one-off crash where the folder had a parent already.
3122 if (folder.getParent() == null) {
3123 mDragLayer.addView(folder);
3124 mDragController.addDropTarget((DropTarget) folder);
3125 } else {
3126 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3127 folder.getParent() + ").");
3128 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003129 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003130 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003131
3132 // Notify the accessibility manager that this folder "window" has appeared and occluded
3133 // the workspace items
3134 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3135 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003136 }
3137
3138 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003139 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003140 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003141 if (folder.isEditingName()) {
3142 folder.dismissEditingName();
3143 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 closeFolder(folder);
3145 }
3146 }
3147
3148 void closeFolder(Folder folder) {
3149 folder.getInfo().opened = false;
3150
3151 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3152 if (parent != null) {
3153 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3154 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003155 if (fi != null) {
3156 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3157 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003158 }
3159 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003160
3161 // Notify the accessibility manager that this folder "window" has disappeard and no
3162 // longer occludeds the workspace items
3163 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003166 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003167 if (!isDraggingEnabled()) return false;
3168 if (isWorkspaceLocked()) return false;
3169 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170
Adam Cohen1697b792013-09-17 19:08:21 -07003171 if (v instanceof Workspace) {
3172 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003173
3174 if (!mWorkspace.isTouchActive()) {
3175 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003176 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3177 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3178 return true;
3179 } else {
3180 return false;
3181 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003182 } else {
3183 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003184 }
Adam Cohen1697b792013-09-17 19:08:21 -07003185 }
3186
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003187 CellLayout.CellInfo longClickCellInfo = null;
3188 View itemUnderLongClick = null;
3189 if (v.getTag() instanceof ItemInfo) {
3190 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003191 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003192 itemUnderLongClick = longClickCellInfo.cell;
3193 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 }
3195
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 // The hotseat touch handling does not go through Workspace, and we always allow long press
3197 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003198 final boolean inHotseat = isHotseatLayout(v);
3199 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003201 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003202 // User long pressed on empty space
3203 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3204 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003205 if (mWorkspace.isInOverviewMode()) {
3206 mWorkspace.startReordering(v);
3207 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003208 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003211 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3212 mHotseat.getOrderInHotseat(
3213 longClickCellInfo.cellX,
3214 longClickCellInfo.cellY));
3215 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003217 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 }
3219 }
3220 }
3221 return true;
3222 }
3223
Winson Chung3d503fb2011-07-13 17:25:49 -07003224 boolean isHotseatLayout(View layout) {
3225 return mHotseat != null && layout != null &&
3226 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3227 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003228
Winson Chung3d503fb2011-07-13 17:25:49 -07003229 /**
3230 * Returns the CellLayout of the specified container at the specified screen.
3231 */
Sunny Goyal92820592015-03-02 11:31:35 -08003232 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3234 if (mHotseat != null) {
3235 return mHotseat.getLayout();
3236 } else {
3237 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003238 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003239 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003240 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003241 }
3242 }
3243
Winson Chungb745afb2015-03-02 11:51:23 -08003244 /**
3245 * For overridden classes.
3246 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003247 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003248 return isAppsViewVisible();
3249 }
3250
3251 public boolean isAppsViewVisible() {
3252 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3253 }
3254
3255 public boolean isWidgetsViewVisible() {
3256 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003257 }
3258
Craig Mautner360310b2012-10-26 15:13:08 -07003259 private void setWorkspaceBackground(boolean workspace) {
3260 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003261 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003262 }
3263
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003264 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003265 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3266 int curflags = getWindow().getAttributes().flags
3267 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3268 if (wpflags != curflags) {
3269 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3270 }
Craig Mautner360310b2012-10-26 15:13:08 -07003271 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003272 }
3273
Michael Jurkae326f182011-11-21 14:05:46 -08003274 @Override
3275 public void onTrimMemory(int level) {
3276 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003277 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3278 // The widget preview db can result in holding onto over
3279 // 3MB of memory for caching which isn't necessary.
3280 SQLiteDatabase.releaseMemory();
3281
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003282 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003283 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003284 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003285 if (mLauncherCallbacks != null) {
3286 mLauncherCallbacks.onTrimMemory(level);
3287 }
Michael Jurkae326f182011-11-21 14:05:46 -08003288 }
3289
Winson Chungb745afb2015-03-02 11:51:23 -08003290 @Override
3291 public void onStateTransitionHideSearchBar() {
3292 // Hide the search bar
3293 if (mSearchDropTargetBar != null) {
3294 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3295 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003296 }
3297
Winson Chungb745afb2015-03-02 11:51:23 -08003298 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003299 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3300 true);
Adam Cohened307df2013-10-02 09:37:31 -07003301 }
3302
Winson Chungdc61c4d2015-04-20 18:26:57 -07003303 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3304 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3305 onCompleteRunnable, true);
3306 }
3307
3308 protected void showWorkspace(int snapToPage, boolean animated) {
3309 showWorkspace(snapToPage, animated, null, true);
3310 }
3311
3312 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3313 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003314 boolean changed = mState != State.WORKSPACE ||
3315 mWorkspace.getState() != Workspace.State.NORMAL;
3316 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003317 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003318 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003319 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003320 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321
Winson Chungc7d2b602012-05-16 17:02:20 -07003322 // Show the search bar (only animate if we were showing the drop target bar in spring
3323 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003324 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003325 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003326 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003327
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328 // Set focus to the AppsCustomize button
3329 if (mAllAppsButton != null) {
3330 mAllAppsButton.requestFocus();
3331 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003332 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003333
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003334 // Change the state *after* we've called all the transition code
3335 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003336
Winson Chung5fb63472011-02-02 17:03:37 -08003337 // Resume the auto-advance of widgets
3338 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003339 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003340
Winson Chung0f785722015-04-08 10:27:49 -07003341 if (changed) {
3342 // Send an accessibility event to announce the context change
3343 getWindow().getDecorView()
3344 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003345
Winson Chung0f785722015-04-08 10:27:49 -07003346 onWorkspaceShown(animated);
3347 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003348 }
3349
Adam Cohened307df2013-10-02 09:37:31 -07003350 void showOverviewMode(boolean animated) {
3351 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003352 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003353 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3354 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003355 mState = State.WORKSPACE;
3356 onWorkspaceShown(animated);
3357 }
3358
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003359 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003360 }
3361
Winson Chungb745afb2015-03-02 11:51:23 -08003362 /**
3363 * Shows the apps view.
3364 */
3365 void showAppsView(boolean animated, boolean resetListToTop) {
3366 if (resetListToTop) {
3367 mAppsView.scrollToTop();
3368 }
3369 showAppsOrWidgets(animated, State.APPS);
3370 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003371
Winson Chungb745afb2015-03-02 11:51:23 -08003372 /**
3373 * Shows the widgets view.
3374 */
3375 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003377 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003378 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003379 }
Winson Chungb745afb2015-03-02 11:51:23 -08003380 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003381
3382 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003383 @Override
3384 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003385 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003386 }
3387 });
Winson Chungb745afb2015-03-02 11:51:23 -08003388 }
3389
3390 /**
3391 * Sets up the transition to show the apps/widgets view.
3392 */
3393 private void showAppsOrWidgets(boolean animated, State toState) {
3394 if (mState != State.WORKSPACE) return;
3395 if (toState != State.APPS && toState != State.WIDGETS) return;
3396
3397 if (toState == State.APPS) {
3398 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003399 if (mLauncherCallbacks != null) {
3400 mLauncherCallbacks.onAllAppsShown();
3401 }
Winson Chungb745afb2015-03-02 11:51:23 -08003402 } else {
3403 mStateTransitionAnimation.startAnimationToWidgets(animated);
3404 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003407 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408
3409 // Pause the auto-advance of widgets until we are out of AllApps
3410 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003411 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412 closeFolder();
3413
3414 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003415 getWindow().getDecorView()
3416 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417 }
3418
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 public void enterSpringLoadedDragMode() {
3420 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3421 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003422 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3423 mState == State.WIDGETS_SPRING_LOADED) {
3424 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003425 }
Winson Chungb745afb2015-03-02 11:51:23 -08003426
3427 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003428 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3429 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003430 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003431 }
Adam Cohen7777d962011-08-18 18:58:38 -07003432
Hyunyoung Song3f471442015-04-08 19:01:34 -07003433 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003434 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003435 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003436
Winson Chunge7a03942011-08-05 15:05:12 -07003437 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003438 @Override
3439 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003440 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003441 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3442 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003443 // Before we show workspace, hide all apps again because
3444 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3445 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003446 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003447 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003448 } else {
3449 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003450 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003451 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003452 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003453 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454
Michael Jurkad3ef3062010-11-23 16:23:58 -08003455 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003456 if (mState == State.APPS_SPRING_LOADED) {
3457 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3458 mState = State.APPS;
3459 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3460 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3461 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003462 }
3463 // Otherwise, we are not in spring loaded mode, so don't do anything.
3464 }
3465
Michael Jurkab3e22d92011-10-31 15:58:33 -07003466 void lockAllApps() {
3467 // TODO
3468 }
3469
3470 void unlockAllApps() {
3471 // TODO
3472 }
3473
Sunny Goyal594d76d2014-11-06 10:12:54 -08003474 protected void disableVoiceButtonProxy(boolean disable) {
3475 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003476 }
3477
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003478 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003479 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3480 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003481 }
3482
Adam Cohen24ce0b32014-01-14 16:18:14 -08003483 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003484 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3485 if (searchProvider == null) {
3486 return null;
3487 }
3488
3489 Bundle opts = new Bundle();
3490 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003491 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003492
3493 SharedPreferences sp = getSharedPreferences(
3494 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3495 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003496 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003497 if (!searchProvider.provider.flattenToString().equals(
3498 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003499 || (widgetInfo == null)
3500 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003501 // A valid widget is not already bound.
3502 if (widgetId > -1) {
3503 mAppWidgetHost.deleteAppWidgetId(widgetId);
3504 widgetId = -1;
3505 }
3506
3507 // Try to bind a new widget
3508 widgetId = mAppWidgetHost.allocateAppWidgetId();
3509
3510 if (!AppWidgetManagerCompat.getInstance(this)
3511 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3512 mAppWidgetHost.deleteAppWidgetId(widgetId);
3513 widgetId = -1;
3514 }
3515
3516 sp.edit()
3517 .putInt(QSB_WIDGET_ID, widgetId)
3518 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3519 .commit();
3520 }
3521
3522 if (widgetId != -1) {
3523 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3524 mQsb.updateAppWidgetOptions(opts);
3525 mQsb.setPadding(0, 0, 0, 0);
3526 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003527 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003528 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003529 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003530 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003531 }
3532
Sunny Goyal22235bc2015-04-03 09:23:43 -07003533 private void reinflateQSBIfNecessary() {
3534 if (mQsb instanceof LauncherAppWidgetHostView &&
3535 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3536 mSearchDropTargetBar.removeView(mQsb);
3537 mQsb = null;
3538 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3539 }
3540 }
3541
Michael Jurkad7c28052012-04-27 15:43:36 -07003542 @Override
3543 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003544 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003545 final List<CharSequence> text = event.getText();
3546 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003547 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003548 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003549 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003550 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003551 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003552 } else {
3553 text.add(getString(R.string.all_apps_home_button_label));
3554 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003555 return result;
3556 }
3557
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003558 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003559 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003560 */
Adam Cohen091440a2015-03-18 14:16:05 -07003561 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003562 @Override
3563 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003564 closeSystemDialogs();
3565 }
3566 }
3567
3568 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003569 * Receives notifications whenever the appwidgets are reset.
3570 */
3571 private class AppWidgetResetObserver extends ContentObserver {
3572 public AppWidgetResetObserver() {
3573 super(new Handler());
3574 }
3575
3576 @Override
3577 public void onChange(boolean selfChange) {
3578 onAppWidgetReset();
3579 }
3580 }
3581
3582 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003583 * If the activity is currently paused, signal that we need to run the passed Runnable
3584 * in onResume.
3585 *
3586 * This needs to be called from incoming places where resources might have been loaded
3587 * while we are paused. That is becaues the Configuration might be wrong
3588 * when we're not running, and if it comes back to what it was when we
3589 * were paused, we are not restarted.
3590 *
3591 * Implementation of the method from LauncherModel.Callbacks.
3592 *
3593 * @return true if we are currently paused. The caller might be able to
3594 * skip some work in that case since we will come back again.
3595 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003596 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003597 if (mPaused) {
3598 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003599 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003600 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003601 }
3602 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003603 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 return true;
3605 } else {
3606 return false;
3607 }
3608 }
3609
Michael Jurkac402cd92013-05-20 15:49:32 +02003610 private boolean waitUntilResume(Runnable run) {
3611 return waitUntilResume(run, false);
3612 }
3613
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003615 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003616 }
3617
Michael Jurka7607c2f2013-04-03 14:33:19 -07003618 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003619 * If the activity is currently paused, signal that we need to re-run the loader
3620 * in onResume.
3621 *
3622 * This needs to be called from incoming places where resources might have been loaded
3623 * while we are paused. That is becaues the Configuration might be wrong
3624 * when we're not running, and if it comes back to what it was when we
3625 * were paused, we are not restarted.
3626 *
3627 * Implementation of the method from LauncherModel.Callbacks.
3628 *
3629 * @return true if we are currently paused. The caller might be able to
3630 * skip some work in that case since we will come back again.
3631 */
3632 public boolean setLoadOnResume() {
3633 if (mPaused) {
3634 Log.i(TAG, "setLoadOnResume");
3635 mOnResumeNeedsLoad = true;
3636 return true;
3637 } else {
3638 return false;
3639 }
3640 }
3641
3642 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003644 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003646 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003647 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003648 } else {
3649 return SCREEN_COUNT / 2;
3650 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003652
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 /**
3654 * Refreshes the shortcuts shown on the workspace.
3655 *
3656 * Implementation of the method from LauncherModel.Callbacks.
3657 */
3658 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003659 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003660
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 // If we're starting binding all over again, clear any bind calls we'd postponed in
3662 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3663 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003664 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003665
Winson Chungd64d1762013-08-20 14:37:16 -07003666 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003667 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003668 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003669
Michael Jurka05bf6442011-11-30 20:28:53 -08003670 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003671 if (mHotseat != null) {
3672 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003673 }
3674 }
3675
Adam Cohendcd297f2013-06-18 13:13:40 -07003676 @Override
3677 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003678 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003679
Adam Cohen5084cba2013-09-03 12:01:16 -07003680 // If there are no screens, we need to have an empty screen
3681 if (orderedScreenIds.size() == 0) {
3682 mWorkspace.addExtraEmptyScreen();
3683 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003684
3685 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003686 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003687 if (hasCustomContentToLeft()) {
3688 mWorkspace.createCustomContentContainer();
3689 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003690 }
Winson Chung64359a52013-07-08 17:17:08 -07003691 }
3692
3693 @Override
3694 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003695 // Log to disk
3696 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3697 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3698 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003699 int count = orderedScreenIds.size();
3700 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003701 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003702 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003703 }
3704
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003705 @Override
3706 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3707 final long screenId, final int[] cell, final int spanX, final int spanY) {
3708 showWorkspace(true, new Runnable() {
3709
3710 @Override
3711 public void run() {
3712 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3713 addPendingItem(info, container, screenId, cell, spanX, spanY);
3714 }
3715 });
3716 }
3717
Adam Cohen39a06042013-07-19 14:30:12 -07003718 private boolean shouldShowWeightWatcher() {
3719 String spKey = LauncherAppState.getSharedPreferencesKey();
3720 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003721 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003722
3723 return show;
3724 }
3725
3726 private void toggleShowWeightWatcher() {
3727 String spKey = LauncherAppState.getSharedPreferencesKey();
3728 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3729 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3730
3731 show = !show;
3732
3733 SharedPreferences.Editor editor = sp.edit();
3734 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3735 editor.commit();
3736
3737 if (mWeightWatcher != null) {
3738 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3739 }
3740 }
3741
Winson Chungd64d1762013-08-20 14:37:16 -07003742 public void bindAppsAdded(final ArrayList<Long> newScreens,
3743 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003744 final ArrayList<ItemInfo> addAnimated,
3745 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003746 Runnable r = new Runnable() {
3747 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003748 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003749 }
3750 };
3751 if (waitUntilResume(r)) {
3752 return;
3753 }
3754
Winson Chungd64d1762013-08-20 14:37:16 -07003755 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003756 if (newScreens != null) {
3757 bindAddScreens(newScreens);
3758 }
Winson Chungd64d1762013-08-20 14:37:16 -07003759
3760 // We add the items without animation on non-visible pages, and with
3761 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003762 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003763 bindItems(addNotAnimated, 0,
3764 addNotAnimated.size(), false);
3765 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003766 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003767 bindItems(addAnimated, 0,
3768 addAnimated.size(), true);
3769 }
Winson Chungc58497e2013-09-03 17:48:37 -07003770
Winson Chung87412982013-10-03 18:34:14 -07003771 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003772 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003773
Winson Chungb745afb2015-03-02 11:51:23 -08003774 if (addedApps != null && mAppsView != null) {
3775 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003776 }
Winson Chungd64d1762013-08-20 14:37:16 -07003777 }
3778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003779 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 * Bind the items start-end from the list.
3781 *
3782 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003783 */
Winson Chung64359a52013-07-08 17:17:08 -07003784 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3785 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003786 Runnable r = new Runnable() {
3787 public void run() {
3788 bindItems(shortcuts, start, end, forceAnimateIcons);
3789 }
3790 };
3791 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003792 return;
3793 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003794
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003796 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3797 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003798 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003800 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003801 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003803
3804 // Short circuit if we are loading dock items for a configuration which has no dock
3805 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3806 mHotseat == null) {
3807 continue;
3808 }
3809
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 switch (item.itemType) {
3811 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3812 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003813 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003814 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003815
Winson Chung64359a52013-07-08 17:17:08 -07003816 /*
3817 * TODO: FIX collision case
3818 */
Winson Chungce376632013-07-11 16:12:41 -07003819 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3820 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3821 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003822 View v = cl.getChildAt(item.cellX, item.cellY);
3823 Object tag = v.getTag();
3824 String desc = "Collision while binding workspace item: " + item
3825 + ". Collides with " + tag;
3826 if (LauncherAppState.isDogfoodBuild()) {
3827 throw (new RuntimeException(desc));
3828 } else {
3829 Log.d(TAG, desc);
3830 }
Winson Chungce376632013-07-11 16:12:41 -07003831 }
Winson Chung64359a52013-07-08 17:17:08 -07003832 }
3833
Adam Cohendcd297f2013-06-18 13:13:40 -07003834 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3835 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003836 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003837 // Animate all the applications up now
3838 shortcut.setAlpha(0f);
3839 shortcut.setScaleX(0f);
3840 shortcut.setScaleY(0f);
3841 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003842 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003845 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003846 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003847 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003848 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003849 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3850 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003852 default:
3853 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003855 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003856
Winson Chung997a9232013-07-24 15:33:46 -07003857 if (animateIcons) {
3858 // Animate to the correct page
3859 if (newShortcutsScreenId > -1) {
3860 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003861 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003862 final Runnable startBounceAnimRunnable = new Runnable() {
3863 public void run() {
3864 anim.playTogether(bounceAnims);
3865 anim.start();
3866 }
3867 };
Winson Chung997a9232013-07-24 15:33:46 -07003868 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003869 // We post the animation slightly delayed to prevent slowdowns
3870 // when we are loading right after we return to launcher.
3871 mWorkspace.postDelayed(new Runnable() {
3872 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003873 if (mWorkspace != null) {
3874 mWorkspace.snapToPage(newScreenIndex);
3875 mWorkspace.postDelayed(startBounceAnimRunnable,
3876 NEW_APPS_ANIMATION_DELAY);
3877 }
Winson Chung94d67682013-09-25 16:29:40 -07003878 }
3879 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003880 } else {
3881 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003882 }
3883 }
Winson Chung64359a52013-07-08 17:17:08 -07003884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003886 }
3887
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 /**
3889 * Implementation of the method from LauncherModel.Callbacks.
3890 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003891 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003892 Runnable r = new Runnable() {
3893 public void run() {
3894 bindFolders(folders);
3895 }
3896 };
3897 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003898 return;
3899 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003900 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902
3903 /**
3904 * Add the views for a widget to the workspace.
3905 *
3906 * Implementation of the method from LauncherModel.Callbacks.
3907 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003908 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003909 Runnable r = new Runnable() {
3910 public void run() {
3911 bindAppWidget(item);
3912 }
3913 };
3914 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003915 return;
3916 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003917
Daniel Sandler843e8602010-06-07 14:59:01 -04003918 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3919 if (DEBUG_WIDGETS) {
3920 Log.d(TAG, "bindAppWidget: " + item);
3921 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 final Workspace workspace = mWorkspace;
3923
Adam Cohen59400422014-03-05 18:07:04 -08003924 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003925 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003926
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003927 if (!mIsSafeModeEnabled
3928 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3929 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003930 if (appWidgetInfo == null) {
3931 if (DEBUG_WIDGETS) {
3932 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3933 + " belongs to component " + item.providerName
3934 + ", as the povider is null");
3935 }
3936 LauncherModel.deleteItemFromDatabase(this, item);
3937 return;
3938 }
3939 // Note: This assumes that the id remap broadcast is received before this step.
3940 // If that is not the case, the id remap will be ignored and user may see the
3941 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003942 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003943 pendingInfo.spanX = item.spanX;
3944 pendingInfo.spanY = item.spanY;
3945 pendingInfo.minSpanX = item.minSpanX;
3946 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003947 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003948 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003949
Sunny Goyalff572272014-07-23 13:58:07 -07003950 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003951 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3952 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003953
3954 // TODO consider showing a permission dialog when the widget is clicked.
3955 if (!success) {
3956 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3959 + " belongs to component " + item.providerName
3960 + ", as the launcher is unable to bing a new widget id");
3961 }
3962 LauncherModel.deleteItemFromDatabase(this, item);
3963 return;
3964 }
3965
3966 item.appWidgetId = newWidgetId;
3967
3968 // If the widget has a configure activity, it is still needs to set it up, otherwise
3969 // the widget is ready to go.
3970 item.restoreStatus = (appWidgetInfo.configure == null)
3971 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3972 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3973
3974 LauncherModel.updateItemInDatabase(this, item);
3975 }
3976
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003977 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003978 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003979 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003980 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3981 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003982 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003983
Sunny Goyal651077b2014-06-30 14:15:31 -07003984 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3985 } else {
3986 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003987 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3988 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003989 view.updateIcon(mIconCache);
3990 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003991 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003992 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003996 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997
Adam Cohendcd297f2013-06-18 13:13:40 -07003998 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004000 if (!item.isCustomWidget()) {
4001 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4002 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004003
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 workspace.requestLayout();
4005
Daniel Sandler843e8602010-06-07 14:59:01 -04004006 if (DEBUG_WIDGETS) {
4007 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4008 + (SystemClock.uptimeMillis()-start) + "ms");
4009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 }
4011
Sunny Goyalff572272014-07-23 13:58:07 -07004012 /**
4013 * Restores a pending widget.
4014 *
4015 * @param appWidgetId The app widget id
4016 * @param cellInfo The position on screen where to create the widget.
4017 */
4018 private void completeRestoreAppWidget(final int appWidgetId) {
4019 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4020 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4021 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4022 return;
4023 }
4024
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004025 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004026 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4027
4028 mWorkspace.reinflateWidgetsIfNecessary();
4029 LauncherModel.updateItemInDatabase(this, info);
4030 }
4031
Adam Cohen1462de32012-07-24 22:34:36 -07004032 public void onPageBoundSynchronously(int page) {
4033 mSynchronouslyBoundPages.add(page);
4034 }
4035
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 /**
4037 * Callback saying that there aren't any more items to bind.
4038 *
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004041 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004042 Runnable r = new Runnable() {
4043 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004044 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004045 }
4046 };
4047 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004048 return;
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 if (mSavedState != null) {
4051 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004052 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 mSavedState = null;
4055 }
4056
Adam Cohen1462de32012-07-24 22:34:36 -07004057 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004059 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004060 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004061
4062 // If we received the result of any pending adds while the loader was running (e.g. the
4063 // widget configuration forced an orientation change), process them now.
4064 if (sPendingAddItem != null) {
4065 final long screenId = completeAdd(sPendingAddItem);
4066
4067 // TODO: this moves the user to the page where the pending item was added. Ideally,
4068 // the screen would be guaranteed to exist after bind, and the page would be set through
4069 // the workspace restore process.
4070 mWorkspace.post(new Runnable() {
4071 @Override
4072 public void run() {
4073 mWorkspace.snapToScreenId(screenId);
4074 }
4075 });
4076 sPendingAddItem = null;
4077 }
4078
Sunny Goyal756adbc2015-04-16 15:20:51 -07004079 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004080
4081 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004082 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004083 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004084 }
4085
Adam Cohen3ed316a2014-07-23 18:21:20 -07004086 private void sendLoadingCompleteBroadcastIfNecessary() {
4087 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4088 String permission =
4089 getResources().getString(R.string.receive_first_load_broadcast_permission);
4090 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4091 sendBroadcast(intent, permission);
4092 SharedPreferences.Editor editor = mSharedPrefs.edit();
4093 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4094 editor.apply();
4095 }
4096 }
4097
Winson Chunga0b7e862013-09-05 16:03:15 -07004098 public boolean isAllAppsButtonRank(int rank) {
4099 if (mHotseat != null) {
4100 return mHotseat.isAllAppsButtonRank(rank);
4101 }
4102 return false;
4103 }
4104
Winson Chunga2413752012-04-03 14:22:34 -07004105 private boolean canRunNewAppsAnimation() {
4106 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4107 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4108 }
4109
Winson Chungc9168342013-06-26 14:54:55 -07004110 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4111 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4112 PropertyValuesHolder.ofFloat("alpha", 1f),
4113 PropertyValuesHolder.ofFloat("scaleX", 1f),
4114 PropertyValuesHolder.ofFloat("scaleY", 1f));
4115 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4116 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4117 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4118 return bounceAnim;
4119 }
4120
Adam Cohen27772732013-11-11 14:00:56 +00004121 public boolean useVerticalBarLayout() {
4122 return LauncherAppState.getInstance().getDynamicGrid().
4123 getDeviceProfile().isVerticalBarLayout();
4124 }
4125
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004126 protected Rect getSearchBarBounds() {
4127 return LauncherAppState.getInstance().getDynamicGrid().
4128 getDeviceProfile().getSearchBarBounds();
4129 }
4130
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004131 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004132 if (mSearchDropTargetBar == null) {
4133 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004134 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004135 if (mQsb != null) {
4136 mSearchDropTargetBar.removeView(mQsb);
4137 mQsb = null;
4138 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004139 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004140 }
4141
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004142 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143 * Add the icons for all apps.
4144 *
4145 * Implementation of the method from LauncherModel.Callbacks.
4146 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004147 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004148 if (mAppsView != null) {
4149 mAppsView.setApps(apps);
4150 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004151 if (mWidgetsView != null) {
4152 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4153 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004154 }
Adam Cohen9211d422014-10-07 18:14:53 -07004155 if (mLauncherCallbacks != null) {
4156 mLauncherCallbacks.bindAllApplications(apps);
4157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004158 }
4159
4160 /**
4161 * A package was updated.
4162 *
4163 * Implementation of the method from LauncherModel.Callbacks.
4164 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004165 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004166 Runnable r = new Runnable() {
4167 public void run() {
4168 bindAppsUpdated(apps);
4169 }
4170 };
4171 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004172 return;
4173 }
4174
Winson Chungb745afb2015-03-02 11:51:23 -08004175 if (mAppsView != null) {
4176 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004178 }
4179
Sunny Goyal4390ace2014-10-13 11:33:11 -07004180 @Override
4181 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4182 Runnable r = new Runnable() {
4183 public void run() {
4184 bindWidgetsRestored(widgets);
4185 }
4186 };
4187 if (waitUntilResume(r)) {
4188 return;
4189 }
4190 mWorkspace.widgetsRestored(widgets);
4191 }
4192
Joe Onorato9c1289c2009-08-17 11:03:03 -04004193 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004194 * Some shortcuts were updated in the background.
4195 *
4196 * Implementation of the method from LauncherModel.Callbacks.
4197 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004198 @Override
4199 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4200 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004201 Runnable r = new Runnable() {
4202 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004204 }
4205 };
4206 if (waitUntilResume(r)) {
4207 return;
4208 }
4209
Sunny Goyal4390ace2014-10-13 11:33:11 -07004210 if (!updated.isEmpty()) {
4211 mWorkspace.updateShortcuts(updated);
4212 }
4213
4214 if (!removed.isEmpty()) {
4215 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4216 for (ShortcutInfo si : removed) {
4217 removedComponents.add(si.getTargetComponent());
4218 }
4219 mWorkspace.removeItemsByComponentName(removedComponents, user);
4220 // Notify the drag controller
4221 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004222 }
4223 }
4224
4225 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004226 * Update the state of a package, typically related to install state.
4227 *
4228 * Implementation of the method from LauncherModel.Callbacks.
4229 */
Sunny Goyale755d462014-07-22 13:48:29 -07004230 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004231 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4232 Runnable r = new Runnable() {
4233 public void run() {
4234 bindRestoreItemsChange(updates);
4235 }
4236 };
4237 if (waitUntilResume(r)) {
4238 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004239 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004240
Sunny Goyal756adbc2015-04-16 15:20:51 -07004241 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004242 }
4243
4244 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004245 * A package was uninstalled. We take both the super set of packageNames
4246 * in addition to specific applications to remove, the reason being that
4247 * this can be called when a package is updated as well. In that scenario,
4248 * we only remove specific components from the workspace, where as
4249 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004250 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004251 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004252 * Implementation of the method from LauncherModel.Callbacks.
4253 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004254 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004255 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004256 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004257 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004258 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004260 }
Winson Chungd64d1762013-08-20 14:37:16 -07004261 };
4262 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004263 return;
4264 }
4265
Sunny Goyal1a745e82014-10-02 15:58:31 -07004266 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004267 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4268 for (AppInfo info : appInfos) {
4269 removedComponents.add(info.componentName);
4270 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004271 if (!packageNames.isEmpty()) {
4272 mWorkspace.removeItemsByPackageName(packageNames, user);
4273 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004274 if (!removedComponents.isEmpty()) {
4275 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004276 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004277 // Notify the drag controller
4278 mDragController.onAppsRemoved(packageNames, removedComponents);
4279
Sunny Goyal1a745e82014-10-02 15:58:31 -07004280 } else {
4281 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004282 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004283
Winson Chungdf95eb12013-10-16 14:57:07 -07004284 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004285 if (mAppsView != null) {
4286 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004287 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004288 }
Joe Onoratobe386092009-11-17 17:32:16 -08004289
4290 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004291 * A number of packages were updated.
4292 */
Adam Cohen091440a2015-03-18 14:16:05 -07004293 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004294 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004295 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004296 bindPackagesUpdated(mWidgetsAndShortcuts);
4297 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004298 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004299 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004300
Michael Jurkac402cd92013-05-20 15:49:32 +02004301 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4302 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4303 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004304 return;
4305 }
4306
Hyunyoung Song3f471442015-04-08 19:01:34 -07004307 if (mWidgetsView != null) {
4308 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4309 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004310 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004311 }
4312
Winson Chung400438b2011-01-16 17:53:48 -08004313 private int mapConfigurationOriActivityInfoOri(int configOri) {
4314 final Display d = getWindowManager().getDefaultDisplay();
4315 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4316 switch (d.getRotation()) {
4317 case Surface.ROTATION_0:
4318 case Surface.ROTATION_180:
4319 // We are currently in the same basic orientation as the natural orientation
4320 naturalOri = configOri;
4321 break;
4322 case Surface.ROTATION_90:
4323 case Surface.ROTATION_270:
4324 // We are currently in the other basic orientation to the natural orientation
4325 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4326 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4327 break;
4328 }
4329
4330 int[] oriMap = {
4331 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4332 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4333 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4334 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4335 };
4336 // Since the map starts at portrait, we need to offset if this device's natural orientation
4337 // is landscape.
4338 int indexOffset = 0;
4339 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4340 indexOffset = 1;
4341 }
4342 return oriMap[(d.getRotation() + indexOffset) % 4];
4343 }
Adam Cohen446e9402011-09-15 18:21:21 -07004344
Winson Chung4b919f82012-05-01 10:44:08 -07004345 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004346 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004347 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4348 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4349 .getConfiguration().orientation));
4350 } else {
4351 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4352 }
Winson Chung4b919f82012-05-01 10:44:08 -07004353 }
4354 }
4355 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004356 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004357 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004358 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004359 } else {
4360 mHandler.postDelayed(new Runnable() {
4361 public void run() {
4362 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4363 }
4364 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004365 }
Winson Chung4b919f82012-05-01 10:44:08 -07004366 }
Winson Chung400438b2011-01-16 17:53:48 -08004367 }
4368
Winson Chunge43a1e72014-01-15 10:33:02 -08004369 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004370 if (mLauncherCallbacks != null) {
4371 return mLauncherCallbacks.isLauncherPreinstalled();
4372 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004373 PackageManager pm = getPackageManager();
4374 try {
4375 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4376 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4377 return true;
4378 } else {
4379 return false;
4380 }
4381 } catch (NameNotFoundException e) {
4382 e.printStackTrace();
4383 return false;
4384 }
4385 }
4386
Adam Cohenea90f832014-05-21 15:03:34 -07004387 /**
4388 * This method indicates whether or not we should suggest default wallpaper dimensions
4389 * when our wallpaper cropper was not yet used to set a wallpaper.
4390 */
4391 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004392 if (mLauncherCallbacks != null) {
4393 return mLauncherCallbacks.overrideWallpaperDimensions();
4394 }
Adam Cohenea90f832014-05-21 15:03:34 -07004395 return true;
4396 }
4397
Adam Cohen432609a2014-03-13 17:03:22 -07004398 /**
4399 * To be overridden by subclasses to indicate that there is an activity to launch
4400 * before showing the standard launcher experience.
4401 */
4402 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004403 if (mLauncherCallbacks != null) {
4404 return mLauncherCallbacks.hasFirstRunActivity();
4405 }
Adam Cohen432609a2014-03-13 17:03:22 -07004406 return false;
4407 }
4408
4409 /**
4410 * To be overridden by subclasses to launch any first run activity
4411 */
4412 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004413 if (mLauncherCallbacks != null) {
4414 return mLauncherCallbacks.getFirstRunActivity();
4415 }
Adam Cohen432609a2014-03-13 17:03:22 -07004416 return null;
4417 }
4418
Winson Chung2826a402015-04-17 16:18:53 -07004419 /**
4420 * Returns whether the launcher callbacks overrides search in all apps.
4421 * @return
4422 */
4423 @Thunk boolean isAllAppsSearchOverridden() {
4424 if (mLauncherCallbacks != null) {
4425 return mLauncherCallbacks.overrideAllAppsSearch();
4426 }
4427 return false;
4428 }
4429
Winson Chunga6945242014-01-08 14:04:34 -08004430 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004431 return !ActivityManager.isRunningInTestHarness() &&
4432 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004433 }
4434
Adam Cohen4a9423d2014-03-28 14:22:31 -07004435 protected boolean hasRunFirstRunActivity() {
4436 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4437 }
4438
Adam Cohen432609a2014-03-13 17:03:22 -07004439 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004440 if (shouldRunFirstRunActivity() &&
4441 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004442 Intent firstRunIntent = getFirstRunActivity();
4443 if (firstRunIntent != null) {
4444 startActivity(firstRunIntent);
4445 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004446 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004447 }
4448 }
Adam Cohen432609a2014-03-13 17:03:22 -07004449 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004450 }
4451
4452 private void markFirstRunActivityShown() {
4453 SharedPreferences.Editor editor = mSharedPrefs.edit();
4454 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4455 editor.apply();
4456 }
4457
Adam Cohen432609a2014-03-13 17:03:22 -07004458 /**
4459 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4460 * screen that must be displayed and dismissed.
4461 */
4462 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004463 if (mLauncherCallbacks != null) {
4464 return mLauncherCallbacks.hasDismissableIntroScreen();
4465 }
Adam Cohen432609a2014-03-13 17:03:22 -07004466 return false;
4467 }
4468
4469 /**
4470 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4471 */
4472 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004473 if (mLauncherCallbacks != null) {
4474 return mLauncherCallbacks.getIntroScreen();
4475 }
Adam Cohen432609a2014-03-13 17:03:22 -07004476 return null;
4477 }
4478
4479 /**
4480 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4481 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4482 */
4483 private boolean shouldShowIntroScreen() {
4484 return hasDismissableIntroScreen() &&
4485 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4486 }
4487
4488 protected void showIntroScreen() {
4489 View introScreen = getIntroScreen();
4490 changeWallpaperVisiblity(false);
4491 if (introScreen != null) {
4492 mDragLayer.showOverlayView(introScreen);
4493 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004494 if (mLauncherOverlayContainer != null) {
4495 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4496 }
Adam Cohen432609a2014-03-13 17:03:22 -07004497 }
4498
4499 public void dismissIntroScreen() {
4500 markIntroScreenDismissed();
4501 if (showFirstRunActivity()) {
4502 // We delay hiding the intro view until the first run activity is showing. This
4503 // avoids a blip.
4504 mWorkspace.postDelayed(new Runnable() {
4505 @Override
4506 public void run() {
4507 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004508 if (mLauncherOverlayContainer != null) {
4509 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4510 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004511 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004512 }
4513 }, ACTIVITY_START_DELAY);
4514 } else {
4515 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004516 if (mLauncherOverlayContainer != null) {
4517 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4518 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004519 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004520 }
4521 changeWallpaperVisiblity(true);
4522 }
4523
4524 private void markIntroScreenDismissed() {
4525 SharedPreferences.Editor editor = mSharedPrefs.edit();
4526 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4527 editor.apply();
4528 }
4529
Adam Cohen091440a2015-03-18 14:16:05 -07004530 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004531 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4532 // on the device, then we always show the first run cling experience (or if there is no
4533 // launcher2). Otherwise, we prompt the user upon started for migration
4534 LauncherClings launcherClings = new LauncherClings(this);
4535 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4536 if (mModel.canMigrateFromOldLauncherDb(this)) {
4537 launcherClings.showMigrationCling();
4538 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004539 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004540 }
4541 }
4542 }
4543
Winson Chunga6945242014-01-08 14:04:34 -08004544 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004545 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4546 if (mHotseat != null) mHotseat.setAlpha(1f);
4547 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4548 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004549 }
4550
4551 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004552 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4553 if (mHotseat != null) mHotseat.setAlpha(0f);
4554 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4555 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004556 }
4557
Mathew Inwood72fbec12013-11-19 15:45:07 +00004558 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004559 // Called from search suggestion, not supported in other profiles.
4560 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4561 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4562 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4563 myUser);
4564 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004565 return null;
4566 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004567 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004568 }
4569
4570 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4571 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004572 // Called from search suggestion, not supported in other profiles.
4573 return createShortcutDragInfo(shortcutIntent, caption, icon,
4574 UserHandleCompat.myUserHandle());
4575 }
4576
4577 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4578 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004579 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004580 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004581 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004582 }
4583
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004584 protected void moveWorkspaceToDefaultScreen() {
4585 mWorkspace.moveToDefaultScreen(false);
4586 }
4587
Mathew Inwood72fbec12013-11-19 15:45:07 +00004588 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4589 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004590 mWorkspace.onExternalDragStartedWithItem(dragView);
4591 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004592 }
4593
Anjali Koppalf5d29132014-02-28 13:33:27 -08004594 @Override
4595 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004596 if (mLauncherCallbacks != null) {
4597 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4598 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004599 }
4600
Winson Chung80baf5a2010-08-09 16:03:15 -07004601 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004602 * Prints out out state for debugging.
4603 */
4604 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004605 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004606 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004607 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4608 Log.d(TAG, "mRestoring=" + mRestoring);
4609 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4610 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004611 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004612 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004613 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004614
Daniel Sandler325dc232013-06-05 22:57:57 -04004615 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004616 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004617
4618 @Override
4619 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4620 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004621 synchronized (sDumpLogs) {
4622 writer.println(" ");
4623 writer.println("Debug logs: ");
4624 for (int i = 0; i < sDumpLogs.size(); i++) {
4625 writer.println(" " + sDumpLogs.get(i));
4626 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004627 }
Adam Cohen9211d422014-10-07 18:14:53 -07004628 if (mLauncherCallbacks != null) {
4629 mLauncherCallbacks.dump(prefix, fd, writer, args);
4630 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004631 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004632
4633 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004634 if (DEBUG_DUMP_LOG) {
4635 synchronized (sDumpLogs) {
4636 Log.d(TAG, "");
4637 Log.d(TAG, "*********************");
4638 Log.d(TAG, "Launcher debug logs: ");
4639 for (int i = 0; i < sDumpLogs.size(); i++) {
4640 Log.d(TAG, " " + sDumpLogs.get(i));
4641 }
4642 Log.d(TAG, "*********************");
4643 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004644 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004645 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004646 }
4647
4648 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004649 addDumpLog(tag, log, null, debugLog);
4650 }
4651
4652 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004653 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004654 if (e != null) {
4655 Log.d(tag, log, e);
4656 } else {
4657 Log.d(tag, log);
4658 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004659 }
Winson Chungede41292013-09-19 16:27:36 -07004660 if (DEBUG_DUMP_LOG) {
4661 sDateStamp.setTime(System.currentTimeMillis());
4662 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004663 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4664 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004665 }
Winson Chungede41292013-09-19 16:27:36 -07004666 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004667 }
4668
Adam Cohen59400422014-03-05 18:07:04 -08004669 public static CustomAppWidget getCustomAppWidget(String name) {
4670 return sCustomAppWidgets.get(name);
4671 }
4672
4673 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4674 return sCustomAppWidgets;
4675 }
4676
Winson Chungede41292013-09-19 16:27:36 -07004677 public void dumpLogsToLocalData() {
4678 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004679 new AsyncTask<Void, Void, Void>() {
4680 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004681 boolean success = false;
4682 sDateStamp.setTime(sRunStart);
4683 String FILENAME = sDateStamp.getMonth() + "-"
4684 + sDateStamp.getDay() + "_"
4685 + sDateStamp.getHours() + "-"
4686 + sDateStamp.getMinutes() + "_"
4687 + sDateStamp.getSeconds() + ".txt";
4688
4689 FileOutputStream fos = null;
4690 File outFile = null;
4691 try {
4692 outFile = new File(getFilesDir(), FILENAME);
4693 outFile.createNewFile();
4694 fos = new FileOutputStream(outFile);
4695 } catch (Exception e) {
4696 e.printStackTrace();
4697 }
4698 if (fos != null) {
4699 PrintWriter writer = new PrintWriter(fos);
4700
4701 writer.println(" ");
4702 writer.println("Debug logs: ");
4703 synchronized (sDumpLogs) {
4704 for (int i = 0; i < sDumpLogs.size(); i++) {
4705 writer.println(" " + sDumpLogs.get(i));
4706 }
4707 }
4708 writer.close();
4709 }
4710 try {
4711 if (fos != null) {
4712 fos.close();
4713 success = true;
4714 }
4715 } catch (IOException e) {
4716 e.printStackTrace();
4717 }
Michael Jurka43467462013-11-14 12:03:58 +01004718 return null;
Winson Chungede41292013-09-19 16:27:36 -07004719 }
Michael Jurka43467462013-11-14 12:03:58 +01004720 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004721 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004723}
Michael Jurka2763be32011-02-24 11:19:57 -08004724
Michael Jurkaabded662011-03-04 12:06:57 -08004725interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004726 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004727 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004728 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004729 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004730 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004731}
Dan Sandlerd5024042014-01-09 15:01:33 -05004732
4733interface DebugIntents {
4734 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4735 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004736}