blob: 20844777f6c5aa1d2c48862dc38483da9b912159 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -0700103import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700104import com.android.launcher3.widget.PendingAddWidgetInfo;
105import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700106
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.DataInputStream;
108import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700110import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800115import java.lang.reflect.InvocationTargetException;
116import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700119import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700122import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700123import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000124import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126/**
127 * Default launcher application.
128 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100129public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700130 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800131 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
132 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700133 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700134 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700150 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Mathew Inwood876a8462013-06-14 14:12:41 +0100152 /**
153 * IntentStarter uses request codes starting with this. This must be greater than all activity
154 * request codes used internally.
155 */
156 protected static final int REQUEST_LAST = 100;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
159
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Winson Chung2672ff92012-05-04 16:22:30 -0700166 // The Intent extra that defines whether to ignore the launch animation
167 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400168 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
171 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700172 // Type: int
173 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
176 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
178 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: boolean
183 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
184 // Type: long
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700186 // Type: int
187 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: int[]
195 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Adam Cohen3ed316a2014-07-23 18:21:20 -0700200 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
201 static final String ACTION_FIRST_LOAD_COMPLETE =
202 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
203
Adam Cohen39a06042013-07-19 14:30:12 -0700204 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700205 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700206
Sunny Goyal594d76d2014-11-06 10:12:54 -0800207 private static final String QSB_WIDGET_ID = "qsb_widget_id";
208 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
209
Winson Chunga6945242014-01-08 14:04:34 -0800210 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
211
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800213 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217 private boolean mIsSafeModeEnabled;
218
Adam Cohenc2d6e892014-10-16 09:49:24 -0700219 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
220 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700221 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000228 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
229 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
230
Winson Chunga2413752012-04-03 14:22:34 -0700231 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700232 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
233 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700235
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800236 private final BroadcastReceiver mCloseSystemDialogsReceiver
237 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private LayoutInflater mInflater;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700243 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800244 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800246 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700247 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalffe83f12014-08-14 17:39:34 -0700249 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800253 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800254 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255
Michael Jurka0280c3b2010-09-17 15:00:07 -0700256 private int[] mTmpAddItemCellCoordinates = new int[2];
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private FolderInfo mFolderInfo;
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
267 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the widget tray screen.
271 private WidgetsContainerView mWidgetsView;
272
Winson Chungf0ea4d32011-06-06 14:27:16 -0700273 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800274 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700277 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
278 // scroll issues (because the workspace may not have been measured yet) and extra work.
279 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
280 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
282 private SpannableStringBuilder mDefaultKeySsb = null;
283
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800286 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private boolean mRestoring;
288 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700289 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Michael Jurka1e2f4652013-07-08 18:03:46 -0700291 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700292 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 private Bundle mSavedInstanceState;
295
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700298 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700300 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Adam Cohen61f560d2013-09-30 15:58:20 -0700307 private View.OnTouchListener mHapticFeedbackTouchListener;
308
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 // Related to the auto-advancing of widgets
310 private final int ADVANCE_MSG = 1;
311 private final int mAdvanceInterval = 20000;
312 private final int mAdvanceStagger = 250;
313 private long mAutoAdvanceSentTime;
314 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 new HashMap<View, AppWidgetProviderInfo>();
317
Winson Chung400438b2011-01-16 17:53:48 -0800318 // Determines how long to wait after a rotation before restoring the screen orientation to
319 // match the sensor state.
320 private final int mRestoreScreenOrientationDelay = 500;
321
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700323
Adam Cohen1462de32012-07-24 22:34:36 -0700324 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700326
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700327 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700328 static Date sDateStamp = new Date();
329 static DateFormat sDateFormat =
330 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
331 static long sRunStart = System.currentTimeMillis();
332 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333
Winson Chung46353de2012-02-16 14:05:10 -0800334 // We only want to get the SharedPreferences once since it does an FS stat each time we get
335 // it from the context.
336 private SharedPreferences mSharedPrefs;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700340 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Michael Jurkaddd62e92011-02-16 17:49:14 -0800345 private BubbleTextView mWaitingForResume;
346
Adam Cohen59400422014-03-05 18:07:04 -0800347 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
348 new HashMap<String, CustomAppWidget>();
349
350 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
351 static {
352 if (ENABLE_CUSTOM_WIDGET_TEST) {
353 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
354 }
355 }
356
Chet Haasea8f996d2015-02-17 12:56:04 -0800357 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
358 private static Method sClipRevealMethod = null;
359 static {
360 Class<?> activityOptionsClass = ActivityOptions.class;
361 try {
362 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
363 View.class, int.class, int.class, int.class, int.class);
364 } catch (Exception e) {
365 // Earlier version
366 }
367 }
368
Adam Cohen091440a2015-03-18 14:16:05 -0700369 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700370 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700371 if (mWorkspace != null) {
372 mWorkspace.buildPageHardwareLayers();
373 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700374 }
375 };
376
Adam Cohendb364c32014-05-20 14:23:40 -0700377 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800378
Adam Cohen091440a2015-03-18 14:16:05 -0700379 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int requestCode;
381 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700382 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700383 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 int cellX;
385 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700386 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800387 }
388
Daniel Sandlerff02d492013-08-05 02:12:05 -0400389 private Stats mStats;
390
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700391 FocusIndicatorView mFocusHandler;
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 @Override
394 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700395 if (DEBUG_STRICT_MODE) {
396 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
397 .detectDiskReads()
398 .detectDiskWrites()
399 .detectNetwork() // or .detectAll() for all detectable problems
400 .penaltyLog()
401 .build());
402 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
403 .detectLeakedSqlLiteObjects()
404 .detectLeakedClosableObjects()
405 .penaltyLog()
406 .penaltyDeath()
407 .build());
408 }
409
Adam Cohen9211d422014-10-07 18:14:53 -0700410 if (mLauncherCallbacks != null) {
411 mLauncherCallbacks.preOnCreate();
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400415
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400417 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700418 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700421 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700422
423 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400424 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700425 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700426 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800427 mModel = app.setLauncher(this);
428 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700429 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400430 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800432 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Daniel Sandlerff02d492013-08-05 02:12:05 -0400434 mStats = new Stats(this);
435
Sunny Goyalffe83f12014-08-14 17:39:34 -0700436 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200447 android.os.Debug.startMethodTracing(
448 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
451 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100455 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800457 registerContentObservers();
458
Joe Onorato7c312c12009-08-13 21:36:53 -0700459 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 mSavedState = savedInstanceState;
462 restoreState(mSavedState);
463
464 if (PROFILE_STARTUP) {
465 android.os.Debug.stopMethodTracing();
466 }
467
468 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700469 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 // If the user leaves launcher, then we should just load items asynchronously when
471 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500472 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 } else {
474 // We only load the page synchronously if the user rotates (or triggers a
475 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800476 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
480 // For handling default keys
481 mDefaultKeySsb = new SpannableStringBuilder();
482 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800483
484 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
485 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800486
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 // On large interfaces, we want the screen to auto-rotate based on the current orientation
488 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen9211d422014-10-07 18:14:53 -0700490 if (mLauncherCallbacks != null) {
491 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 if (mLauncherCallbacks.hasLauncherOverlay()) {
493 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700494 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
495 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
496 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 mWorkspace.setLauncherOverlay(mLauncherOverlay);
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700500
501 if (shouldShowIntroScreen()) {
502 showIntroScreen();
503 } else {
504 showFirstRunActivity();
505 showFirstRunClings();
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
508
509 private LauncherCallbacks mLauncherCallbacks;
510
511 public void onPostCreate(Bundle savedInstanceState) {
512 super.onPostCreate(savedInstanceState);
513 if (mLauncherCallbacks != null) {
514 mLauncherCallbacks.onPostCreate(savedInstanceState);
515 }
516 }
517
518 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
519 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700520 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
521 @Override
522 public void onAllAppsBoundsChanged(Rect bounds) {
523 mAppsView.setFixedBounds(Launcher.this, bounds);
524 }
525
526 @Override
527 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700528 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
529 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700530 }
531 });
Adam Cohen9211d422014-10-07 18:14:53 -0700532 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700533 }
534
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700535 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700536 public void onLauncherProviderChange() {
537 if (mLauncherCallbacks != null) {
538 mLauncherCallbacks.onLauncherProviderChange();
539 }
540 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700541
Adam Cohen9211d422014-10-07 18:14:53 -0700542 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700543 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700544 if (mLauncherCallbacks != null) {
545 return mLauncherCallbacks.hasCustomContentToLeft();
546 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700547 return false;
548 }
549
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500551 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 * {@link #addToCustomContentPage}. This will only be invoked if
553 * {@link #hasCustomContentToLeft()} is {@code true}.
554 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500555 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700556 if (mLauncherCallbacks != null) {
557 mLauncherCallbacks.populateCustomContentContainer();
558 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600559 }
560
561 /**
562 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
563 * ensure the custom content page is added or removed if necessary.
564 */
565 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600566 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 // Not bound yet, wait for bindScreens to be called.
568 return;
569 }
570
571 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
572 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500573 mWorkspace.createCustomContentContainer();
574 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600575 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
576 mWorkspace.removeCustomContentPage();
577 }
578 }
579
Adam Cohen091440a2015-03-18 14:16:05 -0700580 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 if (sLocaleConfiguration == null) {
582 new AsyncTask<Void, Void, LocaleConfiguration>() {
583 @Override
584 protected LocaleConfiguration doInBackground(Void... unused) {
585 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
586 readConfiguration(Launcher.this, localeConfiguration);
587 return localeConfiguration;
588 }
589
590 @Override
591 protected void onPostExecute(LocaleConfiguration result) {
592 sLocaleConfiguration = result;
593 checkForLocaleChange(); // recursive, but now with a locale configuration
594 }
595 }.execute();
596 return;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final Configuration configuration = getResources().getConfiguration();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final String locale = configuration.locale.toString();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mcc = configuration.mcc;
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mnc = configuration.mnc;
609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 sLocaleConfiguration.locale = locale;
614 sLocaleConfiguration.mcc = mcc;
615 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700616
Joe Onorato0589f0f2010-02-08 13:44:00 -0800617 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618
619 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100620 new AsyncTask<Void, Void, Void>() {
621 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100623 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 }
Michael Jurka43467462013-11-14 12:03:58 +0100625 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700626 }
627 }
628
Adam Cohen091440a2015-03-18 14:16:05 -0700629 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700630 public String locale;
631 public int mcc = -1;
632 public int mnc = -1;
633 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700634
Adam Cohen091440a2015-03-18 14:16:05 -0700635 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700636 DataInputStream in = null;
637 try {
Helena Josol28db2802014-10-09 17:04:09 +0100638 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700639 configuration.locale = in.readUTF();
640 configuration.mcc = in.readInt();
641 configuration.mnc = in.readInt();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 // Ignore
646 } finally {
647 if (in != null) {
648 try {
649 in.close();
650 } catch (IOException e) {
651 // Ignore
652 }
653 }
654 }
655 }
656
Adam Cohen091440a2015-03-18 14:16:05 -0700657 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700658 DataOutputStream out = null;
659 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100660 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100661 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700662 out.writeUTF(configuration.locale);
663 out.writeInt(configuration.mcc);
664 out.writeInt(configuration.mnc);
665 out.flush();
666 } catch (FileNotFoundException e) {
667 // Ignore
668 } catch (IOException e) {
669 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100670 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700671 } finally {
672 if (out != null) {
673 try {
674 out.close();
675 } catch (IOException e) {
676 // Ignore
677 }
678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680 }
681
Anton Hanssona2f665f2013-09-26 12:18:32 +0100682 public Stats getStats() {
683 return mStats;
684 }
685
Bjorn Bringertc459e522013-06-07 19:36:01 +0100686 public LayoutInflater getInflater() {
687 return mInflater;
688 }
689
Hyunyoung Song3f471442015-04-08 19:01:34 -0700690 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700691 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
692 // that is subsequently removed from the workspace in startBinding().
693 return !mModel.isLoadingWorkspace();
694 }
695
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800696 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000697 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100698 if (Build.VERSION.SDK_INT >= 17) {
699 return View.generateViewId();
700 } else {
701 // View.generateViewId() is not available. The following fallback logic is a copy
702 // of its implementation.
703 for (;;) {
704 final int result = sNextGeneratedId.get();
705 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
706 int newValue = result + 1;
707 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
708 if (sNextGeneratedId.compareAndSet(result, newValue)) {
709 return result;
710 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000711 }
712 }
713 }
714
715 public int getViewIdForItem(ItemInfo info) {
716 // This cast is safe given the > 2B range for int.
717 int itemId = (int) info.id;
718 if (mItemIdToViewId.containsKey(itemId)) {
719 return mItemIdToViewId.get(itemId);
720 }
721 int viewId = generateViewId();
722 mItemIdToViewId.put(itemId, viewId);
723 return viewId;
724 }
725
726 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
728 * a configuration step, this allows the proper animations to run after other transitions.
729 */
Adam Cohendb364c32014-05-20 14:23:40 -0700730 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700731 long screenId = args.screenId;
732 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
733 // When the screen id represents an actual screen (as opposed to a rank) we make sure
734 // that the drop page actually exists.
735 screenId = ensurePendingDropLayoutExists(args.screenId);
736 }
Adam Cohendb364c32014-05-20 14:23:40 -0700737
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700740 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700742 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700744 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700745 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700746 case REQUEST_RECONFIGURE_APPWIDGET:
747 completeRestoreAppWidget(args.appWidgetId);
748 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800749 }
Michael Jurka27614d22012-04-02 06:40:22 -0700750 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
751 // if you turned the screen off and then back while in All Apps, Launcher would not
752 // return to the workspace. Clearing mAddInfo.container here fixes this issue
753 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700754 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 }
756
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800757 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700759 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700760 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700763
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 Runnable exitSpringLoaded = new Runnable() {
765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
768 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
769 }
770 };
771
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
775 if (resultCode == RESULT_CANCELED) {
776 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700777 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800780 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 }
783 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
785 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700786 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200787 }
788 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200790
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
792 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700793
Adam Cohendb364c32014-05-20 14:23:40 -0700794 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 // We have special handling for widgets
796 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800797 final int appWidgetId;
798 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
799 : -1;
800 if (widgetId < 0) {
801 appWidgetId = pendingAddWidgetId;
802 } else {
803 appWidgetId = widgetId;
804 }
805
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800807 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700808 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
809 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 result = RESULT_CANCELED;
811 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700812 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 @Override
814 public void run() {
815 exitSpringLoadedDragModeDelayed(false, 0, null);
816 }
817 };
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (workspaceLocked) {
819 // No need to remove the empty screen if we're mid-binding, as the
820 // the bind will not add the empty screen.
821 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
822 } else {
823 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825 }
Winson Chung5aaab772012-04-27 15:24:02 -0700826 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700828 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
829 // When the screen id represents an actual screen (as opposed to a rank)
830 // we make sure that the drop page actually exists.
831 mPendingAddInfo.screenId =
832 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
833 }
Adam Cohendb364c32014-05-20 14:23:40 -0700834 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
835
836 dropLayout.setDropPending(true);
837 final Runnable onComplete = new Runnable() {
838 @Override
839 public void run() {
840 completeTwoStageWidgetDrop(resultCode, appWidgetId);
841 dropLayout.setDropPending(false);
842 }
843 };
844 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
845 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
846 } else {
847 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
848 mPendingAddInfo);
849 sPendingAddItem = args;
850 }
Winson Chung5aaab772012-04-27 15:24:02 -0700851 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 return;
853 }
854
Sunny Goyalff572272014-07-23 13:58:07 -0700855 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
856 if (resultCode == RESULT_OK) {
857 // Update the widget view.
858 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
859 pendingAddWidgetId, mPendingAddInfo);
860 if (workspaceLocked) {
861 sPendingAddItem = args;
862 } else {
863 completeAdd(args);
864 }
865 }
866 // Leave the widget in the pending state if the user canceled the configure.
867 return;
868 }
869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 // The pattern used here is that a user PICKs a specific application,
871 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
874 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700875 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700876 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
877 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800878 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700879 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800880 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700882 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
883 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
Adam Cohen00074722013-10-11 17:46:09 -0700885 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700886 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700887 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800890
891 }
892
893 @Override
894 protected void onActivityResult(
895 final int requestCode, final int resultCode, final Intent data) {
896 handleActivityResult(requestCode, resultCode, data);
897 if (mLauncherCallbacks != null) {
898 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
899 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 }
901
Adam Cohendb364c32014-05-20 14:23:40 -0700902 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
903 appWidgetId, ItemInfo info) {
904 PendingAddArguments args = new PendingAddArguments();
905 args.requestCode = requestCode;
906 args.intent = data;
907 args.container = info.container;
908 args.screenId = info.screenId;
909 args.cellX = info.cellX;
910 args.cellY = info.cellY;
911 args.appWidgetId = appWidgetId;
912 return args;
913 }
914
915 /**
916 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
917 *
918 * @param screenId the screen id to check
919 * @return the new screen, or screenId if it exists
920 */
921 private long ensurePendingDropLayoutExists(long screenId) {
922 CellLayout dropLayout =
923 (CellLayout) mWorkspace.getScreenWithId(screenId);
924 if (dropLayout == null) {
925 // it's possible that the add screen was removed because it was
926 // empty and a re-bind occurred
927 mWorkspace.addExtraEmptyScreen();
928 return mWorkspace.commitExtraEmptyScreen();
929 } else {
930 return screenId;
931 }
932 }
933
Adam Cohen091440a2015-03-18 14:16:05 -0700934 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700935 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 Runnable onCompleteRunnable = null;
938 int animationType = 0;
939
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 if (resultCode == RESULT_OK) {
942 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
943 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 mPendingAddWidgetInfo);
945 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 onCompleteRunnable = new Runnable() {
947 @Override
948 public void run() {
949 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700951 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
952 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 }
954 };
955 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800956 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800957 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 if (mDragLayer.getAnimatedView() != null) {
960 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
961 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
962 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700963 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700964 // The animated view may be null in the case of a rotation during widget configuration
965 onCompleteRunnable.run();
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 protected void onStop() {
971 super.onStop();
972 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onStop();
976 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700977 }
978
979 @Override
980 protected void onStart() {
981 super.onStart();
982 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700983
984 if (mLauncherCallbacks != null) {
985 mLauncherCallbacks.onStart();
986 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700987 }
988
989 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200991 long startTime = 0;
992 if (DEBUG_RESUME_TIME) {
993 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400994 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200995 }
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.preOnResume();
999 }
1000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001002
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001004 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.APPS) {
1007 showAppsView(false /* animated */, false /* resetListToTop */);
1008 } else if (mOnResumeState == State.WIDGETS) {
1009 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 }
1011 mOnResumeState = State.NONE;
1012
Craig Mautner360310b2012-10-26 15:13:08 -07001013 // Background was set to gradient in onPause(), restore to black if in all apps.
1014 setWorkspaceBackground(mState == State.WORKSPACE);
1015
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001016 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001017 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001018 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001019 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1025 // execute them here
1026 long startTimeCallbacks = 0;
1027 if (DEBUG_RESUME_TIME) {
1028 startTimeCallbacks = System.currentTimeMillis();
1029 }
1030
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1032 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001033 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 if (DEBUG_RESUME_TIME) {
1036 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1037 (System.currentTimeMillis() - startTimeCallbacks));
1038 }
Michael Jurka447bf842013-05-15 14:52:15 +02001039 }
Michael Jurka54554252013-08-01 12:52:23 +02001040 if (mOnResumeCallbacks.size() > 0) {
1041 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1042 mOnResumeCallbacks.get(i).run();
1043 }
1044 mOnResumeCallbacks.clear();
1045 }
Winson Chunge4e50662012-01-23 14:45:13 -08001046
1047 // Reset the pressed state of icons that were locked in the press state while activities
1048 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001049 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001050 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001051 mWaitingForResume.setStayPressed(false);
1052 }
Winson Chung82963d52013-10-09 11:20:57 -07001053
Adam Cohen06dff352012-06-01 17:17:08 -07001054 // It is possible that widgets can receive updates while launcher is not in the foreground.
1055 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1056 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1057 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001058 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001059 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001060
Michael Jurka447bf842013-05-15 14:52:15 +02001061 if (DEBUG_RESUME_TIME) {
1062 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1063 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001064
1065 if (mWorkspace.getCustomContentCallbacks() != null) {
1066 // If we are resuming and the custom content is the current page, we call onShow().
1067 // It is also poassible that onShow will instead be called slightly after first layout
1068 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1069 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001070 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001071 }
1072 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001073 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001074 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001075
Sunny Goyal756adbc2015-04-16 15:20:51 -07001076 if (!isWorkspaceLoading()) {
1077 // Process any items that were added while Launcher was away.
1078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1079 }
Adam Cohen9211d422014-10-07 18:14:53 -07001080
1081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onResume();
1083 }
Adam Cohen06dff352012-06-01 17:17:08 -07001084 }
1085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001088 // Ensure that items added to Launcher are queued until Launcher returns
1089 InstallShortcutReceiver.enableInstallQueue();
1090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001093 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001094 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001095
1096 // We call onHide() aggressively. The custom content callbacks should be able to
1097 // debounce excess onHide calls.
1098 if (mWorkspace.getCustomContentCallbacks() != null) {
1099 mWorkspace.getCustomContentCallbacks().onHide();
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Adam Cohen9211d422014-10-07 18:14:53 -07001102 if (mLauncherCallbacks != null) {
1103 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
1107 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001108 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1109 // by a onResume or by scrolling otherwise.
1110 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111
1112 // Custom content is completely hidden
1113 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001114
1115 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1116 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001117
1118 // Indicates whether the user is allowed to scroll away from the custom content.
1119 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 }
1121
Adam Cohenc2d6e892014-10-16 09:49:24 -07001122 public interface LauncherOverlay {
1123
1124 /**
1125 * Touch interaction leading to overscroll has begun
1126 */
1127 public void onScrollInteractionBegin();
1128
1129 /**
1130 * Touch interaction related to overscroll has ended
1131 */
1132 public void onScrollInteractionEnd();
1133
1134 /**
1135 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1136 * screen (or in the case of RTL, the rightmost screen).
1137 */
1138 public void onScrollChange(int progress, boolean rtl);
1139
1140 /**
1141 * Screen has stopped scrolling
1142 */
1143 public void onScrollSettled();
1144
1145 /**
1146 * This method can be called by the Launcher in order to force the LauncherOverlay
1147 * to exit fully immersive mode.
1148 */
1149 public void forceExitFullImmersion();
1150 }
1151
Winson Chung0f785722015-04-08 10:27:49 -07001152 public interface LauncherAppsCallbacks {
1153 /**
1154 * Updates launcher to the available space that AllApps can take so as not to overlap with
1155 * any other views.
1156 */
1157 public void onAllAppsBoundsChanged(Rect bounds);
1158
1159 /**
1160 * Called to dismiss all apps if it is showing.
1161 */
1162 public void dismissAllApps();
1163 }
1164
Adam Cohenc2d6e892014-10-16 09:49:24 -07001165 public interface LauncherOverlayCallbacks {
1166 /**
1167 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1168 * however it doesn't modify any state within the launcher.
1169 */
1170 public boolean canEnterFullImmersion();
1171
1172 /**
1173 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1174 * eg. by occupying the full screen and handling all touch events.
1175 *
1176 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1177 * case, Launcher will modify any necessary state and assumes the overlay is
1178 * handling all interaction. If false, the LauncherOverlay should cancel any
1179 *
1180 */
1181 public boolean enterFullImmersion();
1182
1183 /**
1184 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1185 * full control over UI and state.
1186 */
1187 public void exitFullImmersion();
1188 }
1189
1190 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1191
1192 @Override
1193 public boolean canEnterFullImmersion() {
1194 return mState == State.WORKSPACE;
1195 }
1196
1197 @Override
1198 public boolean enterFullImmersion() {
1199 if (mState == State.WORKSPACE) {
1200 // When fully immersed, disregard any touches which fall through.
1201 mDragLayer.setBlockTouch(true);
1202 return true;
1203 }
1204 return false;
1205 }
1206
1207 @Override
1208 public void exitFullImmersion() {
1209 mDragLayer.setBlockTouch(false);
1210 }
1211 }
1212
Jorim Jaggid017f882014-01-14 17:08:48 -08001213 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001214 if (mLauncherCallbacks != null) {
1215 return mLauncherCallbacks.hasSettings();
1216 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001217 return false;
1218 }
1219
Adam Cohen9211d422014-10-07 18:14:53 -07001220 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001221 CustomContentCallbacks callbacks, String description) {
1222 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001223 }
1224
Adam Cohenedb40762013-07-18 16:45:45 -07001225 // The custom content needs to offset its content to account for the QSB
1226 public int getTopOffsetForCustomContent() {
1227 return mWorkspace.getPaddingTop();
1228 }
1229
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001230 @Override
1231 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001233 if (mModel.isCurrentCallbacks(this)) {
1234 mModel.stopLoader();
1235 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001236 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1237
Romain Guy13c2e7b2010-03-10 19:45:00 -08001238 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001239 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001241 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001242 @Override
1243 public void onWindowFocusChanged(boolean hasFocus) {
1244 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001245 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001246
1247 if (mLauncherCallbacks != null) {
1248 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1249 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001250 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 private boolean acceptFilter() {
1253 final InputMethodManager inputManager = (InputMethodManager)
1254 getSystemService(Context.INPUT_METHOD_SERVICE);
1255 return !inputManager.isFullscreenMode();
1256 }
1257
1258 @Override
1259 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001260 final int uniChar = event.getUnicodeChar();
1261 final boolean handled = super.onKeyDown(keyCode, event);
1262 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1263 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1265 keyCode, event);
1266 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001268 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001269 // showSearchDialog()
1270 // If there are multiple keystrokes before the search dialog takes focus,
1271 // onSearchRequested() will be called for every keystroke,
1272 // but it is idempotent, so it's fine.
1273 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275 }
1276
Joe Onorato8a9625e2010-01-28 15:55:35 -08001277 // Eat the long press event so the keyboard doesn't come up.
1278 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1279 return true;
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 return handled;
1283 }
1284
Karl Rosaen138a0412009-04-23 19:00:21 -07001285 private String getTypedText() {
1286 return mDefaultKeySsb.toString();
1287 }
1288
1289 private void clearTypedText() {
1290 mDefaultKeySsb.clear();
1291 mDefaultKeySsb.clearSpans();
1292 Selection.setSelection(mDefaultKeySsb, 0);
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001296 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1297 * State
1298 */
1299 private static State intToState(int stateOrdinal) {
1300 State state = State.WORKSPACE;
1301 final State[] stateValues = State.values();
1302 for (int i = 0; i < stateValues.length; i++) {
1303 if (stateValues[i].ordinal() == stateOrdinal) {
1304 state = stateValues[i];
1305 break;
1306 }
1307 }
1308 return state;
1309 }
1310
1311 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 * Restores the previous state, if it exists.
1313 *
1314 * @param savedState The previous state.
1315 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001316 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 private void restoreState(Bundle savedState) {
1318 if (savedState == null) {
1319 return;
1320 }
1321
Michael Jurka883f55b2010-10-21 15:47:14 -07001322 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001323 if (state == State.APPS || state == State.WIDGETS) {
1324 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 }
1326
Adam Cohen21cd0022013-10-09 18:57:02 -07001327 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1328 PagedView.INVALID_RESTORE_PAGE);
1329 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001330 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001335
Winson Chung3d503fb2011-07-13 17:25:49 -07001336 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1337 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1340 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001341 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1342 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001343 AppWidgetProviderInfo info = savedState.getParcelable(
1344 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1345 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001346 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001347 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 mRestoring = true;
1349 }
1350
1351 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1352 if (renameFolder) {
1353 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001354 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 mRestoring = true;
1356 }
Winson Chunga12a2502010-12-20 14:41:35 -08001357
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001358 mItemIdToViewId = (HashMap<Integer, Integer>)
1359 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 /**
1363 * Finds all the views we need and configure them properly.
1364 */
1365 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001366 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001367
Craig Mautner360310b2012-10-26 15:13:08 -07001368 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001369 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001370 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1371 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001372 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001373 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001374
John Spurlock77e1f472013-09-11 10:09:51 -04001375 mLauncherView.setSystemUiVisibility(
1376 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001377 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Winson Chung4c98d922011-05-31 16:50:48 -07001379 // Setup the drag layer
1380 mDragLayer.setup(this, dragController);
1381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 // Setup the hotseat
1383 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1384 if (mHotseat != null) {
1385 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001386 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 }
1388
Jorim Jaggid017f882014-01-14 17:08:48 -08001389 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001390 View widgetButton = findViewById(R.id.widget_button);
1391 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 @Override
1393 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001394 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001395 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001396 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 }
1398 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001399 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1400
1401 View wallpaperButton = findViewById(R.id.wallpaper_button);
1402 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 @Override
1404 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001405 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001406 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001407 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408 }
1409 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001410 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1411
1412 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 if (hasSettings()) {
1414 settingsButton.setOnClickListener(new OnClickListener() {
1415 @Override
1416 public void onClick(View arg0) {
1417 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001418 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001419 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001420 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001421 });
1422 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423 } else {
1424 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001425 }
1426
Adam Cohendbdff6b2013-09-18 19:09:15 -07001427 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001428
Winson Chung4c98d922011-05-31 16:50:48 -07001429 // Setup the workspace
1430 mWorkspace.setHapticFeedbackEnabled(false);
1431 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001432 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001433 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001434
Winson Chungf0ea4d32011-06-06 14:27:16 -07001435 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001436 mSearchDropTargetBar = (SearchDropTargetBar)
1437 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001438
Winson Chungb745afb2015-03-02 11:51:23 -08001439 // Setup Apps
1440 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001441 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1442 mAppsView.hideSearchBar();
1443 }
Winson Chungb745afb2015-03-02 11:51:23 -08001444
Winson Chungf0ea4d32011-06-06 14:27:16 -07001445 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001446 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001447
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001449 dragController.setDragScoller(mWorkspace);
1450 dragController.setScrollView(mDragLayer);
1451 dragController.setMoveTarget(mWorkspace);
1452 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001453 if (mSearchDropTargetBar != null) {
1454 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001455 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001456 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001457
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001458 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001460 mWeightWatcher = new WeightWatcher(this);
1461 mWeightWatcher.setAlpha(0.5f);
1462 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 new FrameLayout.LayoutParams(
1464 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001465 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001466 Gravity.BOTTOM)
1467 );
Adam Cohen39a06042013-07-19 14:30:12 -07001468
1469 boolean show = shouldShowWeightWatcher();
1470 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001475 * Sets the all apps button. This method is called from {@link Hotseat}.
1476 */
1477 public void setAllAppsButton(View allAppsButton) {
1478 mAllAppsButton = allAppsButton;
1479 }
1480
1481 public View getAllAppsButton() {
1482 return mAllAppsButton;
1483 }
1484
1485 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 * Creates a view representing a shortcut.
1487 *
1488 * @param info The data structure describing the shortcut.
1489 *
1490 * @return A View inflated from R.layout.application.
1491 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001492 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001494 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 /**
1498 * Creates a view representing a shortcut inflated from the specified resource.
1499 *
1500 * @param layoutResId The id of the XML layout used to create the shortcut.
1501 * @param parent The group the shortcut belongs to.
1502 * @param info The data structure describing the shortcut.
1503 *
1504 * @return A View inflated from layoutResId.
1505 */
Adam Cohen59400422014-03-05 18:07:04 -08001506 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001507 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001508 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001510 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 return favorite;
1512 }
1513
1514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 * Add a shortcut to the workspace.
1516 *
1517 * @param data The intent describing the shortcut.
1518 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001520 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 int cellY) {
1522 int[] cellXY = mTmpAddItemCellCoordinates;
1523 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527
Sunny Goyal2350bc92014-10-14 16:42:54 -07001528 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001529 if (info == null) {
1530 return;
1531 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 final View view = createShortcut(info);
1533
Adam Cohenfbba09b2011-07-18 21:43:05 -07001534 // First we check if we already know the exact location where we want to add this item.
1535 if (cellX >= 0 && cellY >= 0) {
1536 cellXY[0] = cellX;
1537 cellXY[1] = cellY;
1538 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001539
1540 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001541 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 true, null,null)) {
1543 return;
1544 }
1545 DragObject dragObject = new DragObject();
1546 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001547 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1548 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001549 return;
1550 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001552 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001554 foundCellSpan = (result != null);
1555 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001557 }
1558
1559 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001560 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 return;
1562 }
1563
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001564 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1573 int minHeight) {
1574 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001575 // We want to account for the extra amount of padding that we are adding to the widget
1576 // to ensure that it gets the full amount of space that it has requested
1577 int requiredWidth = minWidth + padding.left + padding.right;
1578 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001579 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001580 }
1581
Adam Cohen2f093b62012-04-30 18:59:53 -07001582 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1583 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1587 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001588 }
1589
Adam Cohen2f093b62012-04-30 18:59:53 -07001590 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1591 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001592 }
1593
Adam Cohen2f093b62012-04-30 18:59:53 -07001594 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1595 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001596 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001597 }
1598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001600 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001602 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 */
Adam Cohen091440a2015-03-18 14:16:05 -07001604 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001605 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1606
1607 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001608 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001609 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1610 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 }
Romain Guycbb89e42009-06-08 15:52:54 -07001612
Adam Cohen59400422014-03-05 18:07:04 -08001613 if (appWidgetInfo.isCustomWidget) {
1614 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001615 }
1616
Adam Cohen59400422014-03-05 18:07:04 -08001617 LauncherAppWidgetInfo launcherInfo;
1618 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1619 launcherInfo.spanX = info.spanX;
1620 launcherInfo.spanY = info.spanY;
1621 launcherInfo.minSpanX = info.minSpanX;
1622 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001623 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001626 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
1628 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001629 if (hostView == null) {
1630 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001631 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1632 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 } else {
1634 // The AppWidgetHostView has already been inflated and instantiated
1635 launcherInfo.hostView = hostView;
1636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001638 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001639 launcherInfo.notifyWidgetSizeChanged(this);
1640
Adam Cohen59400422014-03-05 18:07:04 -08001641 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1642 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001643
1644 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001646 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1650 @Override
1651 public void onReceive(Context context, Intent intent) {
1652 final String action = intent.getAction();
1653 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1654 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001655 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001657
Winson Chungc100e8e2011-08-09 16:02:43 -07001658 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001659 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001660 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001661 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001662 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1665 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001666 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001667 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1668 mModel.resetLoadedState(false, true);
1669 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1670 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1671 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1672 mModel.resetLoadedState(false, true);
1673 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1674 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1675 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001676 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1677 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1678 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680 }
1681 };
1682
1683 @Override
1684 public void onAttachedToWindow() {
1685 super.onAttachedToWindow();
1686
1687 // Listen for broadcasts related to user-presence
1688 final IntentFilter filter = new IntentFilter();
1689 filter.addAction(Intent.ACTION_SCREEN_OFF);
1690 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001691 // For handling managed profiles
1692 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1693 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 if (ENABLE_DEBUG_INTENTS) {
1695 filter.addAction(DebugIntents.DELETE_DATABASE);
1696 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1697 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001700 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001701 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 mVisible = true;
1703 }
1704
Adam Cohen0b395352014-06-09 22:54:36 +00001705 /**
1706 * Sets up transparent navigation and status bars in LMP.
1707 * This method is a no-op for other platform versions.
1708 */
Sunny Goyald0091012015-01-20 15:55:34 -08001709 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001710 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001711 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001712 Window window = getWindow();
1713 window.getAttributes().systemUiVisibility |=
1714 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1715 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1716 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1717 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1718 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1719 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1720 window.setStatusBarColor(Color.TRANSPARENT);
1721 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001722 }
1723 }
1724
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 @Override
1726 public void onDetachedFromWindow() {
1727 super.onDetachedFromWindow();
1728 mVisible = false;
1729
Adam Cohend113e0c2010-11-11 10:48:05 -08001730 if (mAttached) {
1731 unregisterReceiver(mReceiver);
1732 mAttached = false;
1733 }
Winson Chungb745afb2015-03-02 11:51:23 -08001734 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
1737 public void onWindowVisibilityChanged(int visibility) {
1738 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001739 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 // The following code used to be in onResume, but it turns out onResume is called when
1741 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1742 // is a more appropriate event to handle
1743 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 if (!mWorkspaceLoading) {
1745 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001746 // We want to let Launcher draw itself at least once before we force it to build
1747 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001748 // apps is nice and speedy.
1749 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001750 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001751 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 if (mStarted) return;
1753 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001754 // We delay the layer building a bit in order to give
1755 // other message processing a time to run. In particular
1756 // this avoids a delay in hiding the IME if it was
1757 // currently shown, because doing that may involve
1758 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001759 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 final ViewTreeObserver.OnDrawListener listener = this;
1761 mWorkspace.post(new Runnable() {
1762 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001763 if (mWorkspace != null &&
1764 mWorkspace.getViewTreeObserver() != null) {
1765 mWorkspace.getViewTreeObserver().
1766 removeOnDrawListener(listener);
1767 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 }
1769 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001770 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001771 }
1772 });
1773 }
1774 clearTypedText();
1775 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777
Adam Cohen091440a2015-03-18 14:16:05 -07001778 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 mHandler.removeMessages(ADVANCE_MSG);
1780 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1781 mHandler.sendMessageDelayed(msg, delay);
1782 mAutoAdvanceSentTime = System.currentTimeMillis();
1783 }
1784
Adam Cohen091440a2015-03-18 14:16:05 -07001785 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1787 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1788 mAutoAdvanceRunning = autoAdvanceRunning;
1789 if (autoAdvanceRunning) {
1790 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1791 sendAdvanceMessage(delay);
1792 } else {
1793 if (!mWidgetsToAdvance.isEmpty()) {
1794 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1795 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1796 }
1797 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001798 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 }
1800 }
1801 }
1802
1803 private final Handler mHandler = new Handler() {
1804 @Override
1805 public void handleMessage(Message msg) {
1806 if (msg.what == ADVANCE_MSG) {
1807 int i = 0;
1808 for (View key: mWidgetsToAdvance.keySet()) {
1809 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1810 final int delay = mAdvanceStagger * i;
1811 if (v instanceof Advanceable) {
1812 postDelayed(new Runnable() {
1813 public void run() {
1814 ((Advanceable) v).advance();
1815 }
1816 }, delay);
1817 }
1818 i++;
1819 }
1820 sendAdvanceMessage(mAdvanceInterval);
1821 }
1822 }
1823 };
1824
1825 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001826 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1828 if (v instanceof Advanceable) {
1829 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001830 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833 }
1834
1835 void removeWidgetToAutoAdvance(View hostView) {
1836 if (mWidgetsToAdvance.containsKey(hostView)) {
1837 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001838 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 launcherInfo.hostView = null;
1845 }
1846
Hyunyoung Song3f471442015-04-08 19:01:34 -07001847 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001848 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1849 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 }
1851
Winson Chunga6945242014-01-08 14:04:34 -08001852 public DragLayer getDragLayer() {
1853 return mDragLayer;
1854 }
1855
Winson Chungb745afb2015-03-02 11:51:23 -08001856 public AppsContainerView getAppsView() {
1857 return mAppsView;
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 public WidgetsContainerView getWidgetsView() {
1861 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001862 }
1863
Winson Chunga6945242014-01-08 14:04:34 -08001864 public Workspace getWorkspace() {
1865 return mWorkspace;
1866 }
1867
1868 public Hotseat getHotseat() {
1869 return mHotseat;
1870 }
1871
Jorim Jaggid017f882014-01-14 17:08:48 -08001872 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001873 return mOverviewPanel;
1874 }
1875
1876 public SearchDropTargetBar getSearchBar() {
1877 return mSearchDropTargetBar;
1878 }
1879
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001880 public LauncherAppWidgetHost getAppWidgetHost() {
1881 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Romain Guycbb89e42009-06-08 15:52:54 -07001883
Winson Chunga9abd0e2010-10-27 17:18:37 -07001884 public LauncherModel getModel() {
1885 return mModel;
1886 }
1887
Winson Chunga6945242014-01-08 14:04:34 -08001888 protected SharedPreferences getSharedPrefs() {
1889 return mSharedPrefs;
1890 }
1891
Bjorn Bringertc459e522013-06-07 19:36:01 +01001892 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001893 getWindow().closeAllPanels();
1894
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001895 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001896 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 @Override
1900 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001901 long startTime = 0;
1902 if (DEBUG_RESUME_TIME) {
1903 startTime = System.currentTimeMillis();
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 super.onNewIntent(intent);
1906
1907 // Close the menu
1908 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001909 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001910 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001911
Adam Cohened307df2013-10-02 09:37:31 -07001912 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1913 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1914 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001915
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 if (mWorkspace == null) {
1917 // Can be cases where mWorkspace is null, this prevents a NPE
1918 return;
1919 }
1920 Folder openFolder = mWorkspace.getOpenFolder();
1921 // In all these cases, only animate if we're already on home
1922 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001923
1924 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1925 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001927 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001929 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001930
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 closeFolder();
1932 exitSpringLoadedDragMode();
1933
1934 // If we are already on home, then just animate back to the workspace,
1935 // otherwise, just wait until onResume to set the state back to Workspace
1936 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001937 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 } else {
1939 mOnResumeState = State.WORKSPACE;
1940 }
1941
1942 final View v = getWindow().peekDecorView();
1943 if (v != null && v.getWindowToken() != null) {
1944 InputMethodManager imm = (InputMethodManager)getSystemService(
1945 INPUT_METHOD_SERVICE);
1946 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1947 }
1948
Winson Chungb745afb2015-03-02 11:51:23 -08001949 // Reset the apps view
1950 if (!alreadyOnHome && mAppsView != null) {
1951 mAppsView.scrollToTop();
1952 }
1953
Hyunyoung Song3f471442015-04-08 19:01:34 -07001954 // Reset the widgets view
1955 if (!alreadyOnHome && mWidgetsView != null) {
1956 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001957 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onHomeIntent();
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
Adam Cohened307df2013-10-02 09:37:31 -07001963
Michael Jurka447bf842013-05-15 14:52:15 +02001964 if (DEBUG_RESUME_TIME) {
1965 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Adam Cohen9211d422014-10-07 18:14:53 -07001968 if (mLauncherCallbacks != null) {
1969 mLauncherCallbacks.onNewIntent(intent);
1970 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001974 public void onRestoreInstanceState(Bundle state) {
1975 super.onRestoreInstanceState(state);
1976 for (int page: mSynchronouslyBoundPages) {
1977 mWorkspace.restoreInstanceStateForChild(page);
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
1981 @Override
1982 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001983 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001984 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1985 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001987 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
Michael Jurka883f55b2010-10-21 15:47:14 -07001989 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001990 // We close any open folder since it will not be re-opened, and we need to make sure
1991 // this state is reflected.
1992 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993
Adam Cohendcd297f2013-06-18 13:13:40 -07001994 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001995 mWaitingForResult) {
1996 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1999 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002000 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2001 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2002 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
2006 if (mFolderInfo != null && mWaitingForResult) {
2007 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2008 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2009 }
Michael Jurka946ad472010-07-09 18:05:18 -07002010
Hyunyoung Song3f471442015-04-08 19:01:34 -07002011 // Save the current widgets tray?
2012 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002013 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002014
2015 if (mLauncherCallbacks != null) {
2016 mLauncherCallbacks.onSaveInstanceState(outState);
2017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
2019
2020 @Override
2021 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002023
Winson Chunge7a03942011-08-05 15:05:12 -07002024 // Remove all pending runnables
2025 mHandler.removeMessages(ADVANCE_MSG);
2026 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002027 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002028
Winson Chungcd2b0142011-06-08 16:02:26 -07002029 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002030 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002031
2032 // It's possible to receive onDestroy after a new Launcher activity has
2033 // been created. In this case, don't interfere with the new Launcher.
2034 if (mModel.isCurrentCallbacks(this)) {
2035 mModel.stopLoader();
2036 app.setLauncher(null);
2037 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002040 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002042 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002044 mAppWidgetHost = null;
2045
2046 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047
2048 TextKeyListener.getInstance().release();
2049
Winson Chung81b52252012-08-27 15:34:29 -07002050 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2051 // to prevent leaking Launcher activities on orientation change.
2052 if (mModel != null) {
2053 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2054 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002055
2056 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002057 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002059 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002061 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062 mWorkspace = null;
2063 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002064
Michael Jurka2ecf9952012-06-18 12:52:28 -07002065 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002066
2067 if (mLauncherCallbacks != null) {
2068 mLauncherCallbacks.onDestroy();
2069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
Adam Cohena9cf38f2011-05-02 15:36:58 -07002072 public DragController getDragController() {
2073 return mDragController;
2074 }
2075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 @Override
2077 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002078 onStartForResult(requestCode);
2079 super.startActivityForResult(intent, requestCode);
2080 }
2081
2082 @Override
2083 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2084 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2085 onStartForResult(requestCode);
2086 try {
2087 super.startIntentSenderForResult(intent, requestCode,
2088 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2089 } catch (IntentSender.SendIntentException e) {
2090 throw new ActivityNotFoundException();
2091 }
2092 }
2093
2094 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002095 if (requestCode >= 0) {
2096 setWaitingForResult(true);
2097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 }
2099
Winson Chung70d72102011-08-12 11:24:35 -07002100 /**
2101 * Indicates that we want global search for this activity by setting the globalSearch
2102 * argument for {@link #startSearch} to true.
2103 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002105 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002107
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002108 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002109
Karl Rosaen138a0412009-04-23 19:00:21 -07002110 if (initialQuery == null) {
2111 // Use any text typed in the launcher as the initial query
2112 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 if (appSearchData == null) {
2115 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002116 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
Winson Chungadf0c182012-08-23 14:59:07 -07002118 Rect sourceBounds = new Rect();
2119 if (mSearchDropTargetBar != null) {
2120 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2121 }
Romain Guycbb89e42009-06-08 15:52:54 -07002122
Ian Parkinson047036e2014-09-01 15:40:53 +01002123 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002124 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002125 if (clearTextImmediately) {
2126 clearTypedText();
2127 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 }
2129
Ian Parkinson047036e2014-09-01 15:40:53 +01002130 /**
2131 * Start a text search.
2132 *
2133 * @return {@code true} if the search will start immediately, so any further keypresses
2134 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2135 * to buffer keypresses.
2136 */
2137 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002138 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002139 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2140 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2141 sourceBounds);
2142 }
2143
Michael Jurkaa33411c2012-06-14 16:18:21 -07002144 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002145 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002146 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 }
2148
2149 /**
2150 * Starts the global search activity. This code is a copied from SearchManager
2151 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002152 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002153 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002154 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002155 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2156 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2157 if (globalSearchActivity == null) {
2158 Log.w(TAG, "No global search activity found.");
2159 return;
2160 }
2161 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2162 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2163 intent.setComponent(globalSearchActivity);
2164 // Make sure that we have a Bundle to put source in
2165 if (appSearchData == null) {
2166 appSearchData = new Bundle();
2167 } else {
2168 appSearchData = new Bundle(appSearchData);
2169 }
Adam Cohen9211d422014-10-07 18:14:53 -07002170 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002171 if (!appSearchData.containsKey("source")) {
2172 appSearchData.putString("source", getPackageName());
2173 }
2174 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2175 if (!TextUtils.isEmpty(initialQuery)) {
2176 intent.putExtra(SearchManager.QUERY, initialQuery);
2177 }
2178 if (selectInitialQuery) {
2179 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2180 }
2181 intent.setSourceBounds(sourceBounds);
2182 try {
2183 startActivity(intent);
2184 } catch (ActivityNotFoundException ex) {
2185 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
2188
Yura4f93ec62014-02-04 14:15:21 +00002189 public boolean isOnCustomContent() {
2190 return mWorkspace.isOnOrMovingToCustomContent();
2191 }
2192
Winson Chung70d72102011-08-12 11:24:35 -07002193 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002194 public boolean onPrepareOptionsMenu(Menu menu) {
2195 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002196 if (!isOnCustomContent()) {
2197 // Close any open folders
2198 closeFolder();
2199 // Stop resizing any widgets
2200 mWorkspace.exitWidgetResizeMode();
2201 if (!mWorkspace.isInOverviewMode()) {
2202 // Show the overview mode
2203 showOverviewMode(true);
2204 } else {
2205 showWorkspace(true);
2206 }
Winson Chunge0298742014-01-17 12:03:00 -08002207 }
Adam Cohen9211d422014-10-07 18:14:53 -07002208 if (mLauncherCallbacks != null) {
2209 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2210 }
2211
Michael Jurkab94f3f82013-09-11 18:08:54 +02002212 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002215 @Override
2216 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002217 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002218 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002219 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002220 }
2221
Joe Onorato9c1289c2009-08-17 11:03:03 -04002222 public boolean isWorkspaceLocked() {
2223 return mWorkspaceLoading || mWaitingForResult;
2224 }
2225
Adam Cohen517a7f52014-03-01 12:12:59 -08002226 public boolean isWorkspaceLoading() {
2227 return mWorkspaceLoading;
2228 }
2229
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002230 private void setWorkspaceLoading(boolean value) {
2231 boolean isLocked = isWorkspaceLocked();
2232 mWorkspaceLoading = value;
2233 if (isLocked != isWorkspaceLocked()) {
2234 onWorkspaceLockedChanged();
2235 }
2236 }
2237
2238 private void setWaitingForResult(boolean value) {
2239 boolean isLocked = isWorkspaceLocked();
2240 mWaitingForResult = value;
2241 if (isLocked != isWorkspaceLocked()) {
2242 onWorkspaceLockedChanged();
2243 }
2244 }
2245
Adam Cohen9211d422014-10-07 18:14:53 -07002246 protected void onWorkspaceLockedChanged() {
2247 if (mLauncherCallbacks != null) {
2248 mLauncherCallbacks.onWorkspaceLockedChanged();
2249 }
2250 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002251
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2256 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002257 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002260
Sunny Goyale6b63a32015-01-16 16:14:29 -08002261 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002262 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002263 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2264 }
2265
Sunny Goyale6b63a32015-01-16 16:14:29 -08002266 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002267 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2268 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002269 if (appWidgetInfo.configure != null) {
2270 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002271 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002272
Bjorn Bringert7984c942009-12-09 15:38:25 +00002273 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002274 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2275 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002278 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002279 Runnable onComplete = new Runnable() {
2280 @Override
2281 public void run() {
2282 // Exit spring loaded mode if necessary after adding the widget
2283 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2284 null);
2285 }
2286 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002288 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002289 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
2291 }
Romain Guycbb89e42009-06-08 15:52:54 -07002292
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002293 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002294 // Close any folders that may be open.
2295 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002296 mWorkspace.moveToCustomContentScreen(animate);
2297 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002298
2299 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2300 int[] cell, int spanX, int spanY) {
2301 switch (info.itemType) {
2302 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2303 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2304 int span[] = new int[2];
2305 span[0] = spanX;
2306 span[1] = spanY;
2307 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2308 container, screenId, cell, span);
2309 break;
2310 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2311 processShortcutFromDrop(info.componentName, container, screenId, cell);
2312 break;
2313 default:
2314 throw new IllegalStateException("Unknown item type: " + info.itemType);
2315 }
2316 }
2317
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 /**
2319 * Process a shortcut drop.
2320 *
2321 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002325 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2326 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002327 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002330 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331
2332 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mPendingAddInfo.cellX = cell[0];
2334 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002336
2337 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2338 createShortcutIntent.setComponent(componentName);
2339 processShortcut(createShortcutIntent);
2340 }
2341
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 /**
2343 * Process a widget drop.
2344 *
2345 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002346 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002349 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2350 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002352 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002354 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002355 mPendingAddInfo.minSpanX = info.minSpanX;
2356 mPendingAddInfo.minSpanY = info.minSpanY;
2357
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002359 mPendingAddInfo.cellX = cell[0];
2360 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002362 if (span != null) {
2363 mPendingAddInfo.spanX = span[0];
2364 mPendingAddInfo.spanY = span[1];
2365 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002366
Adam Cohened66b2b2012-01-23 17:28:51 -08002367 AppWidgetHostView hostView = info.boundWidget;
2368 int appWidgetId;
2369 if (hostView != null) {
2370 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002371 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002372 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002373 // In this case, we either need to start an activity to get permission to bind
2374 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002375 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002376 Bundle options = info.bindOptions;
2377
Sunny Goyalffe83f12014-08-14 17:39:34 -07002378 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2379 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002380 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002381 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002382 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002383 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002384 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2385 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2386 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002387 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2388 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002389 // TODO: we need to make sure that this accounts for the options bundle.
2390 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002391 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2392 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002393 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002394 }
2395
Joe Onoratodeb98af2010-02-19 14:59:39 -08002396 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002397 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 }
2399
Winson Chung24ab2f12010-09-16 14:10:47 -07002400 void processWallpaper(Intent intent) {
2401 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2402 }
2403
Adam Cohendcd297f2013-06-18 13:13:40 -07002404 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002405 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002406 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 folderInfo.title = getText(R.string.folder_name);
2408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002410 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2411 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002412 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413
2414 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 FolderIcon newFolder =
2416 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002419 // Force measure the new folder icon
2420 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2421 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002422 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 }
Romain Guycbb89e42009-06-08 15:52:54 -07002424
Joe Onorato9c1289c2009-08-17 11:03:03 -04002425 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002426 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002427 }
2428
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002429 /**
2430 * Registers various content observers. The current implementation registers
2431 * only a favorites observer to keep track of the favorites applications.
2432 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002433 private void registerContentObservers() {
2434 ContentResolver resolver = getContentResolver();
2435 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2436 true, mWidgetObserver);
2437 }
2438
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 @Override
2440 public boolean dispatchKeyEvent(KeyEvent event) {
2441 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2442 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002444 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002445 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002446 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002447 dumpState();
2448 return true;
2449 }
2450 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002451 }
2452 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2453 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 return true;
2456 }
2457 }
2458
2459 return super.dispatchKeyEvent(event);
2460 }
2461
Joe Onorato88ec0992009-11-19 13:16:06 -08002462 @Override
2463 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002464 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2465 return;
2466 }
2467
Winson Chung3d9490a2015-03-24 17:38:42 -07002468 LauncherAccessibilityDelegate delegate =
2469 LauncherAppState.getInstance().getAccessibilityDelegate();
2470 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002471 return;
2472 }
2473
Winson Chungb745afb2015-03-02 11:51:23 -08002474 if (isAppsViewVisible()) {
2475 showWorkspace(true);
2476 } else if (isWidgetsViewVisible()) {
2477 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002478 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002479 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002480 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002481 Folder openFolder = mWorkspace.getOpenFolder();
2482 if (openFolder.isEditingName()) {
2483 openFolder.dismissEditingName();
2484 } else {
2485 closeFolder();
2486 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002487 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002488 mWorkspace.exitWidgetResizeMode();
2489
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 // Back button is a no-op here, but give at least some feedback for the button press
2491 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002492 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002496 * Re-listen when widgets are reset.
2497 */
Adam Cohen091440a2015-03-18 14:16:05 -07002498 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002499 if (mAppWidgetHost != null) {
2500 mAppWidgetHost.startListening();
2501 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002502 }
2503
2504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 * Launches the intent referred by the clicked shortcut.
2506 *
2507 * @param v The view representing the clicked shortcut.
2508 */
2509 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002510 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2511 // view has detached (it's possible for this to happen if the view is removed mid touch).
2512 if (v.getWindowToken() == null) {
2513 return;
2514 }
2515
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002516 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002517 return;
2518 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002519
Adam Cohen1697b792013-09-17 19:08:21 -07002520 if (v instanceof Workspace) {
2521 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002522 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002523 }
2524 return;
2525 }
2526
2527 if (v instanceof CellLayout) {
2528 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002529 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002530 }
2531 }
2532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002534 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002537 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002539 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002540 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002542 } else if (tag instanceof AppInfo) {
2543 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002544 } else if (tag instanceof LauncherAppWidgetInfo) {
2545 if (v instanceof PendingAppWidgetHostView) {
2546 onClickPendingWidget((PendingAppWidgetHostView) v);
2547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 }
2549 }
2550
Sunny Goyal508da152014-08-14 10:53:27 -07002551 public void onClickPagedViewIcon(View v) {
2552 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002553 if (mLauncherCallbacks != null) {
2554 mLauncherCallbacks.onClickPagedViewIcon(v);
2555 }
Sunny Goyal508da152014-08-14 10:53:27 -07002556 }
2557
Michael Jurka0e260592010-06-30 17:07:39 -07002558 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002559 return false;
2560 }
2561
Michael Jurkaaf442092010-06-10 17:01:57 -07002562 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002563 * Event handler for the app widget view which has not fully restored.
2564 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002566 if (mIsSafeModeEnabled) {
2567 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2568 return;
2569 }
2570
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002572 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002573 int widgetId = info.appWidgetId;
2574 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2575 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002576 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2577 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002578 mPendingAddInfo.copyFrom(info);
2579 mPendingAddWidgetId = widgetId;
2580
Sunny Goyalffe83f12014-08-14 17:39:34 -07002581 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2582 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002583 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 } else if (info.installProgress < 0) {
2585 // The install has not been queued
2586 final String packageName = info.providerName.getPackageName();
2587 showBrokenAppInstallDialog(packageName,
2588 new DialogInterface.OnClickListener() {
2589 public void onClick(DialogInterface dialog, int id) {
2590 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2591 }
2592 });
2593 } else {
2594 // Download has started.
2595 final String packageName = info.providerName.getPackageName();
2596 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002597 }
2598 }
2599
2600 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002601 * Event handler for the "grid" button that appears on the home screen, which
2602 * enters all apps mode.
2603 *
2604 * @param v The view that was clicked.
2605 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 protected void onClickAllAppsButton(View v) {
2607 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002608 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 showWorkspace(true);
2610 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002611 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 }
2613 }
2614
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002615 private void showBrokenAppInstallDialog(final String packageName,
2616 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002617 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002618 .setTitle(R.string.abandoned_promises_title)
2619 .setMessage(R.string.abandoned_promise_explanation)
2620 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2621 .setNeutralButton(R.string.abandoned_clean_this,
2622 new DialogInterface.OnClickListener() {
2623 public void onClick(DialogInterface dialog, int id) {
2624 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2625 mWorkspace.removeAbandonedPromise(packageName, user);
2626 }
2627 })
2628 .create().show();
2629 return;
2630 }
2631
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 /**
2633 * Event handler for an app shortcut click.
2634 *
2635 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2636 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2639 Object tag = v.getTag();
2640 if (!(tag instanceof ShortcutInfo)) {
2641 throw new IllegalArgumentException("Input must be a Shortcut");
2642 }
2643
2644 // Open shortcut
2645 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002646
2647 if (shortcut.isDisabled != 0) {
2648 int error = R.string.activity_not_available;
2649 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2650 error = R.string.safemode_shortcut_error;
2651 }
2652 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2653 return;
2654 }
2655
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 final Intent intent = shortcut.intent;
2657
2658 // Check for special shortcuts
2659 if (intent.getComponent() != null) {
2660 final String shortcutClass = intent.getComponent().getClassName();
2661
2662 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2663 MemoryDumpActivity.startDump(this);
2664 return;
2665 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2666 toggleShowWeightWatcher();
2667 return;
2668 }
2669 }
2670
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002672 if ((v instanceof BubbleTextView)
2673 && shortcut.isPromise()
2674 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002675 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002676 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 new DialogInterface.OnClickListener() {
2678 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 return;
2683 }
2684
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002685 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002686 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002687
2688 if (mLauncherCallbacks != null) {
2689 mLauncherCallbacks.onClickAppShortcut(v);
2690 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 }
2692
Adam Cohen091440a2015-03-18 14:16:05 -07002693 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002695 final ShortcutInfo shortcut;
2696 final Intent intent;
2697 if (tag instanceof ShortcutInfo) {
2698 shortcut = (ShortcutInfo) tag;
2699 intent = shortcut.intent;
2700 int[] pos = new int[2];
2701 v.getLocationOnScreen(pos);
2702 intent.setSourceBounds(new Rect(pos[0], pos[1],
2703 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002704
Sunny Goyal508da152014-08-14 10:53:27 -07002705 } else if (tag instanceof AppInfo) {
2706 shortcut = null;
2707 intent = ((AppInfo) tag).intent;
2708 } else {
2709 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2710 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711
2712 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 mStats.recordLaunch(intent, shortcut);
2714
2715 if (success && v instanceof BubbleTextView) {
2716 mWaitingForResume = (BubbleTextView) v;
2717 mWaitingForResume.setStayPressed(true);
2718 }
2719 }
2720
2721 /**
2722 * Event handler for a folder icon click.
2723 *
2724 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2725 */
2726 protected void onClickFolderIcon(View v) {
2727 if (LOGD) Log.d(TAG, "onClickFolder");
2728 if (!(v instanceof FolderIcon)){
2729 throw new IllegalArgumentException("Input must be a FolderIcon");
2730 }
2731
2732 FolderIcon folderIcon = (FolderIcon) v;
2733 final FolderInfo info = folderIcon.getFolderInfo();
2734 Folder openFolder = mWorkspace.getFolderForTag(info);
2735
2736 // If the folder info reports that the associated folder is open, then verify that
2737 // it is actually opened. There have been a few instances where this gets out of sync.
2738 if (info.opened && openFolder == null) {
2739 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2740 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2741 info.opened = false;
2742 }
2743
2744 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2745 // Close any open folder
2746 closeFolder();
2747 // Open the requested folder
2748 openFolder(folderIcon);
2749 } else {
2750 // Find the open folder...
2751 int folderScreen;
2752 if (openFolder != null) {
2753 folderScreen = mWorkspace.getPageForView(openFolder);
2754 // .. and close it
2755 closeFolder(openFolder);
2756 if (folderScreen != mWorkspace.getCurrentPage()) {
2757 // Close any folder open on the current screen
2758 closeFolder();
2759 // Pull the folder onto this screen
2760 openFolder(folderIcon);
2761 }
2762 }
2763 }
Adam Cohen9211d422014-10-07 18:14:53 -07002764
2765 if (mLauncherCallbacks != null) {
2766 mLauncherCallbacks.onClickFolderIcon(v);
2767 }
Michael Jurka5130e402011-10-13 04:55:35 -07002768 }
2769
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002770 /**
2771 * Event handler for the (Add) Widgets button that appears after a long press
2772 * on the home screen.
2773 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002774 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002775 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002776 if (mIsSafeModeEnabled) {
2777 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2778 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002779 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onClickAddWidgetButton(view);
2782 }
Adam Cohen9211d422014-10-07 18:14:53 -07002783 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002784 }
2785
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 /**
2787 * Event handler for the wallpaper picker button that appears after a long press
2788 * on the home screen.
2789 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002790 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002792 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2793 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002794
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onClickWallpaperPicker(v);
2797 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 }
2799
2800 /**
2801 * Event handler for a click on the settings button that appears after a long press
2802 * on the home screen.
2803 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002804 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002806 if (mLauncherCallbacks != null) {
2807 mLauncherCallbacks.onClickSettingsButton(v);
2808 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 }
2810
Michael Jurka5130e402011-10-13 04:55:35 -07002811 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002812 // Provide the same haptic feedback that the system offers for virtual keys.
2813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002814 }
2815
Adam Cohen61f560d2013-09-30 15:58:20 -07002816 public void performHapticFeedbackOnTouchDown(View v) {
2817 // Provide the same haptic feedback that the system offers for virtual keys.
2818 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2819 }
2820
2821 public View.OnTouchListener getHapticFeedbackTouchListener() {
2822 if (mHapticFeedbackTouchListener == null) {
2823 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2824 @Override
2825 public boolean onTouch(View v, MotionEvent event) {
2826 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2827 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2828 }
2829 return false;
2830 }
2831 };
2832 }
2833 return mHapticFeedbackTouchListener;
2834 }
2835
Adam Cohen9211d422014-10-07 18:14:53 -07002836 public void onDragStarted(View view) {
2837 if (isOnCustomContent()) {
2838 // Custom content screen doesn't participate in drag and drop. If on custom
2839 // content screen, move to default.
2840 moveWorkspaceToDefaultScreen();
2841 }
2842
2843 if (mLauncherCallbacks != null) {
2844 mLauncherCallbacks.onDragStarted(view);
2845 }
2846 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002847
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002848 /**
2849 * Called when the user stops interacting with the launcher.
2850 * This implies that the user is now on the homescreen and is not doing housekeeping.
2851 */
Adam Cohen9211d422014-10-07 18:14:53 -07002852 protected void onInteractionEnd() {
2853 if (mLauncherCallbacks != null) {
2854 mLauncherCallbacks.onInteractionEnd();
2855 }
2856 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002857
2858 /**
2859 * Called when the user starts interacting with the launcher.
2860 * The possible interactions are:
2861 * - open all apps
2862 * - reorder an app shortcut, or a widget
2863 * - open the overview mode.
2864 * This is a good time to stop doing things that only make sense
2865 * when the user is on the homescreen and not doing housekeeping.
2866 */
Adam Cohen9211d422014-10-07 18:14:53 -07002867 protected void onInteractionBegin() {
2868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onInteractionBegin();
2870 }
2871 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002872
Kenny Guyf07af7b2014-07-31 11:39:16 +01002873 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002874 try {
2875 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002876 launcherApps.showAppDetailsForProfile(componentName, user);
2877 } catch (SecurityException e) {
2878 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2879 Log.e(TAG, "Launcher does not have permission to launch settings");
2880 } catch (ActivityNotFoundException e) {
2881 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2882 Log.e(TAG, "Unable to launch settings");
2883 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002884 }
2885
Michael Jurka1e2f4652013-07-08 18:03:46 -07002886 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002887 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2888 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002889 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002890 // System applications cannot be installed. For now, show a toast explaining that.
2891 // We may give them the option of disabling apps this way.
2892 int messageId = R.string.uninstall_system_app_text;
2893 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002894 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002895 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002896 String packageName = componentName.getPackageName();
2897 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002898 Intent intent = new Intent(
2899 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002900 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2901 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002902 if (user != null) {
2903 user.addToIntent(intent, Intent.EXTRA_USER);
2904 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002905 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002906 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002907 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002908 }
2909
Michael Jurka86a720e2012-05-09 11:23:23 -07002910 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002911 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002913 // Only launch using the new animation if the shortcut has not opted out (this is a
2914 // private contract between launcher and may be ignored in the future).
2915 boolean useLaunchAnimation = (v != null) &&
2916 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002917 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2918 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002919
Kenny Guy1317e2d2014-05-08 18:52:50 +01002920 UserHandleCompat user = null;
2921 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2922 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2923 user = userManager.getUserForSerialNumber(serialNumber);
2924 }
2925
2926 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002927 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002928 ActivityOptions opts = null;
2929 if (sClipRevealMethod != null) {
2930 // TODO: call method directly when Launcher3 can depend on M APIs
2931 int left = 0, top = 0;
2932 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2933 if (v instanceof TextView) {
2934 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002935 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2936 if (icon != null) {
2937 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002938 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002939 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002940 width = bounds.width();
2941 height = bounds.height();
2942 }
2943 }
2944 try {
2945 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2946 left, top, width, height);
2947 } catch (IllegalAccessException e) {
2948 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2949 sClipRevealMethod = null;
2950 } catch (InvocationTargetException e) {
2951 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2952 sClipRevealMethod = null;
2953 }
2954 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002955 if (opts == null && !Utilities.isLmpOrAbove()) {
2956 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002957 v.getMeasuredWidth(), v.getMeasuredHeight());
2958 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002959 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002960 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002961
2962 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2963 // Could be launching some bookkeeping activity
2964 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002965 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002966 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002967 launcherApps.startActivityForProfile(intent.getComponent(), user,
2968 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002969 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002970 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 } catch (SecurityException e) {
2972 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002973 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002975 "or use the exported attribute for this activity. "
2976 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002978 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002980
Michael Jurka86a720e2012-05-09 11:23:23 -07002981 boolean startActivitySafely(View v, Intent intent, Object tag) {
2982 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002983 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2984 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2985 return false;
2986 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002987 try {
2988 success = startActivity(v, intent, tag);
2989 } catch (ActivityNotFoundException e) {
2990 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2991 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2992 }
2993 return success;
2994 }
2995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 /**
2997 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2998 * in the DragLayer in the exact absolute location of the original FolderIcon.
2999 */
3000 private void copyFolderIconToImage(FolderIcon fi) {
3001 final int width = fi.getMeasuredWidth();
3002 final int height = fi.getMeasuredHeight();
3003
3004 // Lazy load ImageView, Bitmap and Canvas
3005 if (mFolderIconImageView == null) {
3006 mFolderIconImageView = new ImageView(this);
3007 }
3008 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3009 mFolderIconBitmap.getHeight() != height) {
3010 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3011 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3012 }
3013
3014 DragLayer.LayoutParams lp;
3015 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3016 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3017 } else {
3018 lp = new DragLayer.LayoutParams(width, height);
3019 }
3020
Adam Cohen307fe232012-08-16 17:55:58 -07003021 // The layout from which the folder is being opened may be scaled, adjust the starting
3022 // view size by this scale factor.
3023 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003024 lp.customPosition = true;
3025 lp.x = mRectForFolderAnimation.left;
3026 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003027 lp.width = (int) (scale * width);
3028 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003029
3030 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3031 fi.draw(mFolderIconCanvas);
3032 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003033 if (fi.getFolder() != null) {
3034 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3035 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003036 }
Adam Cohen268c4752012-06-06 17:47:33 -07003037 // Just in case this image view is still in the drag layer from a previous animation,
3038 // we remove it and re-add it.
3039 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3040 mDragLayer.removeView(mFolderIconImageView);
3041 }
3042 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003043 if (fi.getFolder() != null) {
3044 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003045 }
Adam Cohen268c4752012-06-06 17:47:33 -07003046 }
3047
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003049 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3051 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3052 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3053
Adam Cohenc51934b2011-07-26 21:07:43 -07003054 FolderInfo info = (FolderInfo) fi.getTag();
3055 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3056 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003057 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3058 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003059 }
3060
Adam Cohen268c4752012-06-06 17:47:33 -07003061 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3062 copyFolderIconToImage(fi);
3063 fi.setVisibility(View.INVISIBLE);
3064
Michael Jurka2ecf9952012-06-18 12:52:28 -07003065 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003066 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003067 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003068 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3069 }
3070 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003071 oa.start();
3072 }
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003075 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003076 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3077 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3078 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3079
Adam Cohen268c4752012-06-06 17:47:33 -07003080 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003081
Adam Cohen268c4752012-06-06 17:47:33 -07003082 // We remove and re-draw the FolderIcon in-case it has changed
3083 mDragLayer.removeView(mFolderIconImageView);
3084 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003085 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003086 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003087 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003088 oa.addListener(new AnimatorListenerAdapter() {
3089 @Override
3090 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003091 if (cl != null) {
3092 cl.clearFolderLeaveBehind();
3093 // Remove the ImageView copy of the FolderIcon and make the original visible.
3094 mDragLayer.removeView(mFolderIconImageView);
3095 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003096 }
3097 }
3098 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003099 oa.start();
3100 }
3101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003103 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 * is animated relative to the specified View. If the View is null, no animation
3105 * is played.
3106 *
3107 * @param folderInfo The FolderInfo describing the folder to open.
3108 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003109 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003110 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003111 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3112 if (openFolder != null && openFolder != folder) {
3113 // Close any open folder before opening a folder.
3114 closeFolder();
3115 }
3116
Adam Cohena9cf38f2011-05-02 15:36:58 -07003117 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Adam Cohena9cf38f2011-05-02 15:36:58 -07003119 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120
Sunny Goyalf4066152015-04-15 09:42:19 -07003121 // While the folder is open, the position of the icon cannot change.
3122 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3123
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 // Just verify that the folder hasn't already been added to the DragLayer.
3125 // There was a one-off crash where the folder had a parent already.
3126 if (folder.getParent() == null) {
3127 mDragLayer.addView(folder);
3128 mDragController.addDropTarget((DropTarget) folder);
3129 } else {
3130 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3131 folder.getParent() + ").");
3132 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003133 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003134 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003135
3136 // Notify the accessibility manager that this folder "window" has appeared and occluded
3137 // the workspace items
3138 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3139 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003140 }
3141
3142 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003143 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003145 if (folder.isEditingName()) {
3146 folder.dismissEditingName();
3147 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003148 closeFolder(folder);
3149 }
3150 }
3151
3152 void closeFolder(Folder folder) {
3153 folder.getInfo().opened = false;
3154
3155 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3156 if (parent != null) {
3157 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3158 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003159 if (fi != null) {
3160 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3161 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 }
3163 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003164
3165 // Notify the accessibility manager that this folder "window" has disappeard and no
3166 // longer occludeds the workspace items
3167 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 }
3169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003171 if (!isDraggingEnabled()) return false;
3172 if (isWorkspaceLocked()) return false;
3173 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174
Adam Cohen1697b792013-09-17 19:08:21 -07003175 if (v instanceof Workspace) {
3176 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003177
3178 if (!mWorkspace.isTouchActive()) {
3179 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003180 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3181 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3182 return true;
3183 } else {
3184 return false;
3185 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003186 } else {
3187 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003188 }
Adam Cohen1697b792013-09-17 19:08:21 -07003189 }
3190
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003191 CellLayout.CellInfo longClickCellInfo = null;
3192 View itemUnderLongClick = null;
3193 if (v.getTag() instanceof ItemInfo) {
3194 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003195 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003196 itemUnderLongClick = longClickCellInfo.cell;
3197 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003198 }
3199
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 // The hotseat touch handling does not go through Workspace, and we always allow long press
3201 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003202 final boolean inHotseat = isHotseatLayout(v);
3203 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003205 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003206 // User long pressed on empty space
3207 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3208 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003209 if (mWorkspace.isInOverviewMode()) {
3210 mWorkspace.startReordering(v);
3211 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003212 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003215 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3216 mHotseat.getOrderInHotseat(
3217 longClickCellInfo.cellX,
3218 longClickCellInfo.cellY));
3219 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003221 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 }
3223 }
3224 }
3225 return true;
3226 }
3227
Winson Chung3d503fb2011-07-13 17:25:49 -07003228 boolean isHotseatLayout(View layout) {
3229 return mHotseat != null && layout != null &&
3230 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3231 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003232
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 /**
3234 * Returns the CellLayout of the specified container at the specified screen.
3235 */
Sunny Goyal92820592015-03-02 11:31:35 -08003236 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3238 if (mHotseat != null) {
3239 return mHotseat.getLayout();
3240 } else {
3241 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003242 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003243 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003244 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003245 }
3246 }
3247
Winson Chungb745afb2015-03-02 11:51:23 -08003248 /**
3249 * For overridden classes.
3250 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003251 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003252 return isAppsViewVisible();
3253 }
3254
3255 public boolean isAppsViewVisible() {
3256 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3257 }
3258
3259 public boolean isWidgetsViewVisible() {
3260 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003261 }
3262
Craig Mautner360310b2012-10-26 15:13:08 -07003263 private void setWorkspaceBackground(boolean workspace) {
3264 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003265 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003266 }
3267
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003268 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003269 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3270 int curflags = getWindow().getAttributes().flags
3271 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3272 if (wpflags != curflags) {
3273 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3274 }
Craig Mautner360310b2012-10-26 15:13:08 -07003275 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003276 }
3277
Michael Jurkae326f182011-11-21 14:05:46 -08003278 @Override
3279 public void onTrimMemory(int level) {
3280 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003281 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3282 // The widget preview db can result in holding onto over
3283 // 3MB of memory for caching which isn't necessary.
3284 SQLiteDatabase.releaseMemory();
3285
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003286 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003287 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003288 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003289 if (mLauncherCallbacks != null) {
3290 mLauncherCallbacks.onTrimMemory(level);
3291 }
Michael Jurkae326f182011-11-21 14:05:46 -08003292 }
3293
Winson Chungb745afb2015-03-02 11:51:23 -08003294 @Override
3295 public void onStateTransitionHideSearchBar() {
3296 // Hide the search bar
3297 if (mSearchDropTargetBar != null) {
3298 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3299 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003300 }
3301
Winson Chungb745afb2015-03-02 11:51:23 -08003302 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003303 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3304 true);
Adam Cohened307df2013-10-02 09:37:31 -07003305 }
3306
Winson Chungdc61c4d2015-04-20 18:26:57 -07003307 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3308 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3309 onCompleteRunnable, true);
3310 }
3311
3312 protected void showWorkspace(int snapToPage, boolean animated) {
3313 showWorkspace(snapToPage, animated, null, true);
3314 }
3315
3316 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3317 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003318 boolean changed = mState != State.WORKSPACE ||
3319 mWorkspace.getState() != Workspace.State.NORMAL;
3320 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003321 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003323 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003324 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325
Winson Chungc7d2b602012-05-16 17:02:20 -07003326 // Show the search bar (only animate if we were showing the drop target bar in spring
3327 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003328 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003329 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003330 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003331
Michael Jurkab3e22d92011-10-31 15:58:33 -07003332 // Set focus to the AppsCustomize button
3333 if (mAllAppsButton != null) {
3334 mAllAppsButton.requestFocus();
3335 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003336 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003337
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003338 // Change the state *after* we've called all the transition code
3339 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003340
Winson Chung5fb63472011-02-02 17:03:37 -08003341 // Resume the auto-advance of widgets
3342 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003343 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003344
Winson Chung0f785722015-04-08 10:27:49 -07003345 if (changed) {
3346 // Send an accessibility event to announce the context change
3347 getWindow().getDecorView()
3348 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003349
Winson Chung0f785722015-04-08 10:27:49 -07003350 onWorkspaceShown(animated);
3351 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003352 }
3353
Adam Cohened307df2013-10-02 09:37:31 -07003354 void showOverviewMode(boolean animated) {
3355 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003356 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003357 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3358 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003359 mState = State.WORKSPACE;
3360 onWorkspaceShown(animated);
3361 }
3362
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003363 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003364 }
3365
Winson Chungb745afb2015-03-02 11:51:23 -08003366 /**
3367 * Shows the apps view.
3368 */
3369 void showAppsView(boolean animated, boolean resetListToTop) {
3370 if (resetListToTop) {
3371 mAppsView.scrollToTop();
3372 }
3373 showAppsOrWidgets(animated, State.APPS);
3374 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375
Winson Chungb745afb2015-03-02 11:51:23 -08003376 /**
3377 * Shows the widgets view.
3378 */
3379 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003380 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003381 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003382 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003383 }
Winson Chungb745afb2015-03-02 11:51:23 -08003384 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003385
3386 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003387 @Override
3388 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003390 }
3391 });
Winson Chungb745afb2015-03-02 11:51:23 -08003392 }
3393
3394 /**
3395 * Sets up the transition to show the apps/widgets view.
3396 */
3397 private void showAppsOrWidgets(boolean animated, State toState) {
3398 if (mState != State.WORKSPACE) return;
3399 if (toState != State.APPS && toState != State.WIDGETS) return;
3400
3401 if (toState == State.APPS) {
3402 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003403 if (mLauncherCallbacks != null) {
3404 mLauncherCallbacks.onAllAppsShown();
3405 }
Winson Chungb745afb2015-03-02 11:51:23 -08003406 } else {
3407 mStateTransitionAnimation.startAnimationToWidgets(animated);
3408 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003409
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003411 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
3413 // Pause the auto-advance of widgets until we are out of AllApps
3414 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003415 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416 closeFolder();
3417
3418 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003419 getWindow().getDecorView()
3420 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421 }
3422
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423 public void enterSpringLoadedDragMode() {
3424 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3425 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003426 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3427 mState == State.WIDGETS_SPRING_LOADED) {
3428 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003429 }
Winson Chungb745afb2015-03-02 11:51:23 -08003430
3431 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003432 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3433 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003434 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003435 }
Adam Cohen7777d962011-08-18 18:58:38 -07003436
Hyunyoung Song3f471442015-04-08 19:01:34 -07003437 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003438 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003439 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003440
Winson Chunge7a03942011-08-05 15:05:12 -07003441 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003442 @Override
3443 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003444 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003445 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3446 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003447 // Before we show workspace, hide all apps again because
3448 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3449 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003450 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003451 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003452 } else {
3453 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003454 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003455 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003456 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003457 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458
Michael Jurkad3ef3062010-11-23 16:23:58 -08003459 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003460 if (mState == State.APPS_SPRING_LOADED) {
3461 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3462 mState = State.APPS;
3463 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3464 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3465 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003466 }
3467 // Otherwise, we are not in spring loaded mode, so don't do anything.
3468 }
3469
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470 void lockAllApps() {
3471 // TODO
3472 }
3473
3474 void unlockAllApps() {
3475 // TODO
3476 }
3477
Sunny Goyal594d76d2014-11-06 10:12:54 -08003478 protected void disableVoiceButtonProxy(boolean disable) {
3479 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003480 }
3481
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003482 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003483 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3484 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003485 }
3486
Adam Cohen24ce0b32014-01-14 16:18:14 -08003487 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003488 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3489 if (searchProvider == null) {
3490 return null;
3491 }
3492
3493 Bundle opts = new Bundle();
3494 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003495 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003496
3497 SharedPreferences sp = getSharedPreferences(
3498 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3499 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003500 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003501 if (!searchProvider.provider.flattenToString().equals(
3502 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003503 || (widgetInfo == null)
3504 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003505 // A valid widget is not already bound.
3506 if (widgetId > -1) {
3507 mAppWidgetHost.deleteAppWidgetId(widgetId);
3508 widgetId = -1;
3509 }
3510
3511 // Try to bind a new widget
3512 widgetId = mAppWidgetHost.allocateAppWidgetId();
3513
3514 if (!AppWidgetManagerCompat.getInstance(this)
3515 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3516 mAppWidgetHost.deleteAppWidgetId(widgetId);
3517 widgetId = -1;
3518 }
3519
3520 sp.edit()
3521 .putInt(QSB_WIDGET_ID, widgetId)
3522 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3523 .commit();
3524 }
3525
3526 if (widgetId != -1) {
3527 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3528 mQsb.updateAppWidgetOptions(opts);
3529 mQsb.setPadding(0, 0, 0, 0);
3530 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003531 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003532 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003533 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003534 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003535 }
3536
Sunny Goyal22235bc2015-04-03 09:23:43 -07003537 private void reinflateQSBIfNecessary() {
3538 if (mQsb instanceof LauncherAppWidgetHostView &&
3539 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3540 mSearchDropTargetBar.removeView(mQsb);
3541 mQsb = null;
3542 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3543 }
3544 }
3545
Michael Jurkad7c28052012-04-27 15:43:36 -07003546 @Override
3547 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003548 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003549 final List<CharSequence> text = event.getText();
3550 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003551 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003552 if (mState == State.APPS) {
3553 text.add("Apps");
3554 } else if (mState == State.WIDGETS) {
3555 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003556 } else {
3557 text.add(getString(R.string.all_apps_home_button_label));
3558 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003559 return result;
3560 }
3561
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003562 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003563 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003564 */
Adam Cohen091440a2015-03-18 14:16:05 -07003565 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003566 @Override
3567 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003568 closeSystemDialogs();
3569 }
3570 }
3571
3572 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003573 * Receives notifications whenever the appwidgets are reset.
3574 */
3575 private class AppWidgetResetObserver extends ContentObserver {
3576 public AppWidgetResetObserver() {
3577 super(new Handler());
3578 }
3579
3580 @Override
3581 public void onChange(boolean selfChange) {
3582 onAppWidgetReset();
3583 }
3584 }
3585
3586 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003587 * If the activity is currently paused, signal that we need to run the passed Runnable
3588 * in onResume.
3589 *
3590 * This needs to be called from incoming places where resources might have been loaded
3591 * while we are paused. That is becaues the Configuration might be wrong
3592 * when we're not running, and if it comes back to what it was when we
3593 * were paused, we are not restarted.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 *
3597 * @return true if we are currently paused. The caller might be able to
3598 * skip some work in that case since we will come back again.
3599 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003600 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 if (mPaused) {
3602 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003603 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003604 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003605 }
3606 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003608 return true;
3609 } else {
3610 return false;
3611 }
3612 }
3613
Michael Jurkac402cd92013-05-20 15:49:32 +02003614 private boolean waitUntilResume(Runnable run) {
3615 return waitUntilResume(run, false);
3616 }
3617
Michael Jurka1e2f4652013-07-08 18:03:46 -07003618 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003619 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003620 }
3621
Michael Jurka7607c2f2013-04-03 14:33:19 -07003622 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003623 * If the activity is currently paused, signal that we need to re-run the loader
3624 * in onResume.
3625 *
3626 * This needs to be called from incoming places where resources might have been loaded
3627 * while we are paused. That is becaues the Configuration might be wrong
3628 * when we're not running, and if it comes back to what it was when we
3629 * were paused, we are not restarted.
3630 *
3631 * Implementation of the method from LauncherModel.Callbacks.
3632 *
3633 * @return true if we are currently paused. The caller might be able to
3634 * skip some work in that case since we will come back again.
3635 */
3636 public boolean setLoadOnResume() {
3637 if (mPaused) {
3638 Log.i(TAG, "setLoadOnResume");
3639 mOnResumeNeedsLoad = true;
3640 return true;
3641 } else {
3642 return false;
3643 }
3644 }
3645
3646 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003648 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003650 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003651 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003652 } else {
3653 return SCREEN_COUNT / 2;
3654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003656
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 /**
3658 * Refreshes the shortcuts shown on the workspace.
3659 *
3660 * Implementation of the method from LauncherModel.Callbacks.
3661 */
3662 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003663 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003664
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 // If we're starting binding all over again, clear any bind calls we'd postponed in
3666 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3667 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003668 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003669
Winson Chungd64d1762013-08-20 14:37:16 -07003670 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003671 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003672 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003673
Michael Jurka05bf6442011-11-30 20:28:53 -08003674 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003675 if (mHotseat != null) {
3676 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003677 }
3678 }
3679
Adam Cohendcd297f2013-06-18 13:13:40 -07003680 @Override
3681 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003682 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003683
Adam Cohen5084cba2013-09-03 12:01:16 -07003684 // If there are no screens, we need to have an empty screen
3685 if (orderedScreenIds.size() == 0) {
3686 mWorkspace.addExtraEmptyScreen();
3687 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003688
3689 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003690 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003691 if (hasCustomContentToLeft()) {
3692 mWorkspace.createCustomContentContainer();
3693 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003694 }
Winson Chung64359a52013-07-08 17:17:08 -07003695 }
3696
3697 @Override
3698 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003699 // Log to disk
3700 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3701 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3702 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003703 int count = orderedScreenIds.size();
3704 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003705 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003706 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003707 }
3708
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003709 @Override
3710 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3711 final long screenId, final int[] cell, final int spanX, final int spanY) {
3712 showWorkspace(true, new Runnable() {
3713
3714 @Override
3715 public void run() {
3716 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3717 addPendingItem(info, container, screenId, cell, spanX, spanY);
3718 }
3719 });
3720 }
3721
Adam Cohen39a06042013-07-19 14:30:12 -07003722 private boolean shouldShowWeightWatcher() {
3723 String spKey = LauncherAppState.getSharedPreferencesKey();
3724 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003725 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003726
3727 return show;
3728 }
3729
3730 private void toggleShowWeightWatcher() {
3731 String spKey = LauncherAppState.getSharedPreferencesKey();
3732 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3733 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3734
3735 show = !show;
3736
3737 SharedPreferences.Editor editor = sp.edit();
3738 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3739 editor.commit();
3740
3741 if (mWeightWatcher != null) {
3742 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3743 }
3744 }
3745
Winson Chungd64d1762013-08-20 14:37:16 -07003746 public void bindAppsAdded(final ArrayList<Long> newScreens,
3747 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003748 final ArrayList<ItemInfo> addAnimated,
3749 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003750 Runnable r = new Runnable() {
3751 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003752 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003753 }
3754 };
3755 if (waitUntilResume(r)) {
3756 return;
3757 }
3758
Winson Chungd64d1762013-08-20 14:37:16 -07003759 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003760 if (newScreens != null) {
3761 bindAddScreens(newScreens);
3762 }
Winson Chungd64d1762013-08-20 14:37:16 -07003763
3764 // We add the items without animation on non-visible pages, and with
3765 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003766 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003767 bindItems(addNotAnimated, 0,
3768 addNotAnimated.size(), false);
3769 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003770 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 bindItems(addAnimated, 0,
3772 addAnimated.size(), true);
3773 }
Winson Chungc58497e2013-09-03 17:48:37 -07003774
Winson Chung87412982013-10-03 18:34:14 -07003775 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003776 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003777
Winson Chungb745afb2015-03-02 11:51:23 -08003778 if (addedApps != null && mAppsView != null) {
3779 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003780 }
Winson Chungd64d1762013-08-20 14:37:16 -07003781 }
3782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003783 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003784 * Bind the items start-end from the list.
3785 *
3786 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003787 */
Winson Chung64359a52013-07-08 17:17:08 -07003788 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3789 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003790 Runnable r = new Runnable() {
3791 public void run() {
3792 bindItems(shortcuts, start, end, forceAnimateIcons);
3793 }
3794 };
3795 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 return;
3797 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003798
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003800 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3801 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003802 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003803 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003804 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003805 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003807
3808 // Short circuit if we are loading dock items for a configuration which has no dock
3809 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3810 mHotseat == null) {
3811 continue;
3812 }
3813
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814 switch (item.itemType) {
3815 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3816 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003817 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003818 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003819
Winson Chung64359a52013-07-08 17:17:08 -07003820 /*
3821 * TODO: FIX collision case
3822 */
Winson Chungce376632013-07-11 16:12:41 -07003823 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3824 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3825 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003826 View v = cl.getChildAt(item.cellX, item.cellY);
3827 Object tag = v.getTag();
3828 String desc = "Collision while binding workspace item: " + item
3829 + ". Collides with " + tag;
3830 if (LauncherAppState.isDogfoodBuild()) {
3831 throw (new RuntimeException(desc));
3832 } else {
3833 Log.d(TAG, desc);
3834 }
Winson Chungce376632013-07-11 16:12:41 -07003835 }
Winson Chung64359a52013-07-08 17:17:08 -07003836 }
3837
Adam Cohendcd297f2013-06-18 13:13:40 -07003838 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3839 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003840 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003841 // Animate all the applications up now
3842 shortcut.setAlpha(0f);
3843 shortcut.setScaleX(0f);
3844 shortcut.setScaleY(0f);
3845 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003846 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003849 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003850 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003851 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003852 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003853 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3854 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003856 default:
3857 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003859 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003860
Winson Chung997a9232013-07-24 15:33:46 -07003861 if (animateIcons) {
3862 // Animate to the correct page
3863 if (newShortcutsScreenId > -1) {
3864 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003865 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003866 final Runnable startBounceAnimRunnable = new Runnable() {
3867 public void run() {
3868 anim.playTogether(bounceAnims);
3869 anim.start();
3870 }
3871 };
Winson Chung997a9232013-07-24 15:33:46 -07003872 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003873 // We post the animation slightly delayed to prevent slowdowns
3874 // when we are loading right after we return to launcher.
3875 mWorkspace.postDelayed(new Runnable() {
3876 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003877 if (mWorkspace != null) {
3878 mWorkspace.snapToPage(newScreenIndex);
3879 mWorkspace.postDelayed(startBounceAnimRunnable,
3880 NEW_APPS_ANIMATION_DELAY);
3881 }
Winson Chung94d67682013-09-25 16:29:40 -07003882 }
3883 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003884 } else {
3885 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003886 }
3887 }
Winson Chung64359a52013-07-08 17:17:08 -07003888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003890 }
3891
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892 /**
3893 * Implementation of the method from LauncherModel.Callbacks.
3894 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003895 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003896 Runnable r = new Runnable() {
3897 public void run() {
3898 bindFolders(folders);
3899 }
3900 };
3901 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003902 return;
3903 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003904 sFolders.clear();
3905 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907
3908 /**
3909 * Add the views for a widget to the workspace.
3910 *
3911 * Implementation of the method from LauncherModel.Callbacks.
3912 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003913 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003914 Runnable r = new Runnable() {
3915 public void run() {
3916 bindAppWidget(item);
3917 }
3918 };
3919 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003920 return;
3921 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003922
Daniel Sandler843e8602010-06-07 14:59:01 -04003923 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3924 if (DEBUG_WIDGETS) {
3925 Log.d(TAG, "bindAppWidget: " + item);
3926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 final Workspace workspace = mWorkspace;
3928
Adam Cohen59400422014-03-05 18:07:04 -08003929 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003930 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003931
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003932 if (!mIsSafeModeEnabled
3933 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3934 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003935 if (appWidgetInfo == null) {
3936 if (DEBUG_WIDGETS) {
3937 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3938 + " belongs to component " + item.providerName
3939 + ", as the povider is null");
3940 }
3941 LauncherModel.deleteItemFromDatabase(this, item);
3942 return;
3943 }
3944 // Note: This assumes that the id remap broadcast is received before this step.
3945 // If that is not the case, the id remap will be ignored and user may see the
3946 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003947 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003948 pendingInfo.spanX = item.spanX;
3949 pendingInfo.spanY = item.spanY;
3950 pendingInfo.minSpanX = item.minSpanX;
3951 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003952 Bundle options = null;
3953 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003954
Sunny Goyalff572272014-07-23 13:58:07 -07003955 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003956 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3957 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003958
3959 // TODO consider showing a permission dialog when the widget is clicked.
3960 if (!success) {
3961 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3962 if (DEBUG_WIDGETS) {
3963 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3964 + " belongs to component " + item.providerName
3965 + ", as the launcher is unable to bing a new widget id");
3966 }
3967 LauncherModel.deleteItemFromDatabase(this, item);
3968 return;
3969 }
3970
3971 item.appWidgetId = newWidgetId;
3972
3973 // If the widget has a configure activity, it is still needs to set it up, otherwise
3974 // the widget is ready to go.
3975 item.restoreStatus = (appWidgetInfo.configure == null)
3976 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3977 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3978
3979 LauncherModel.updateItemInDatabase(this, item);
3980 }
3981
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003982 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003983 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003984 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003985 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3986 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003988
Sunny Goyal651077b2014-06-30 14:15:31 -07003989 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3990 } else {
3991 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003992 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3993 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003994 view.updateIcon(mIconCache);
3995 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003996 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003997 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999
Joe Onorato9c1289c2009-08-17 11:03:03 -04004000 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004001 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002
Adam Cohendcd297f2013-06-18 13:13:40 -07004003 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004005 if (!item.isCustomWidget()) {
4006 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4007 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004008
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 workspace.requestLayout();
4010
Daniel Sandler843e8602010-06-07 14:59:01 -04004011 if (DEBUG_WIDGETS) {
4012 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4013 + (SystemClock.uptimeMillis()-start) + "ms");
4014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 }
4016
Sunny Goyalff572272014-07-23 13:58:07 -07004017 /**
4018 * Restores a pending widget.
4019 *
4020 * @param appWidgetId The app widget id
4021 * @param cellInfo The position on screen where to create the widget.
4022 */
4023 private void completeRestoreAppWidget(final int appWidgetId) {
4024 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4025 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4026 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4027 return;
4028 }
4029
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004030 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004031 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4032
4033 mWorkspace.reinflateWidgetsIfNecessary();
4034 LauncherModel.updateItemInDatabase(this, info);
4035 }
4036
Adam Cohen1462de32012-07-24 22:34:36 -07004037 public void onPageBoundSynchronously(int page) {
4038 mSynchronouslyBoundPages.add(page);
4039 }
4040
Joe Onorato9c1289c2009-08-17 11:03:03 -04004041 /**
4042 * Callback saying that there aren't any more items to bind.
4043 *
4044 * Implementation of the method from LauncherModel.Callbacks.
4045 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004046 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004047 Runnable r = new Runnable() {
4048 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004049 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004050 }
4051 };
4052 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004053 return;
4054 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004055 if (mSavedState != null) {
4056 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004057 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 mSavedState = null;
4060 }
4061
Adam Cohen1462de32012-07-24 22:34:36 -07004062 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004064 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004065 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004066
4067 // If we received the result of any pending adds while the loader was running (e.g. the
4068 // widget configuration forced an orientation change), process them now.
4069 if (sPendingAddItem != null) {
4070 final long screenId = completeAdd(sPendingAddItem);
4071
4072 // TODO: this moves the user to the page where the pending item was added. Ideally,
4073 // the screen would be guaranteed to exist after bind, and the page would be set through
4074 // the workspace restore process.
4075 mWorkspace.post(new Runnable() {
4076 @Override
4077 public void run() {
4078 mWorkspace.snapToScreenId(screenId);
4079 }
4080 });
4081 sPendingAddItem = null;
4082 }
4083
Sunny Goyal756adbc2015-04-16 15:20:51 -07004084 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004085
4086 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004087 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004088 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004089 }
4090
Adam Cohen3ed316a2014-07-23 18:21:20 -07004091 private void sendLoadingCompleteBroadcastIfNecessary() {
4092 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4093 String permission =
4094 getResources().getString(R.string.receive_first_load_broadcast_permission);
4095 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4096 sendBroadcast(intent, permission);
4097 SharedPreferences.Editor editor = mSharedPrefs.edit();
4098 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4099 editor.apply();
4100 }
4101 }
4102
Winson Chunga0b7e862013-09-05 16:03:15 -07004103 public boolean isAllAppsButtonRank(int rank) {
4104 if (mHotseat != null) {
4105 return mHotseat.isAllAppsButtonRank(rank);
4106 }
4107 return false;
4108 }
4109
Winson Chunga2413752012-04-03 14:22:34 -07004110 private boolean canRunNewAppsAnimation() {
4111 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4112 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4113 }
4114
Winson Chungc9168342013-06-26 14:54:55 -07004115 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4116 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4117 PropertyValuesHolder.ofFloat("alpha", 1f),
4118 PropertyValuesHolder.ofFloat("scaleX", 1f),
4119 PropertyValuesHolder.ofFloat("scaleY", 1f));
4120 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4121 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4122 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4123 return bounceAnim;
4124 }
4125
Adam Cohen27772732013-11-11 14:00:56 +00004126 public boolean useVerticalBarLayout() {
4127 return LauncherAppState.getInstance().getDynamicGrid().
4128 getDeviceProfile().isVerticalBarLayout();
4129 }
4130
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004131 protected Rect getSearchBarBounds() {
4132 return LauncherAppState.getInstance().getDynamicGrid().
4133 getDeviceProfile().getSearchBarBounds();
4134 }
4135
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004136 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004137 if (mSearchDropTargetBar == null) {
4138 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004139 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004140 if (mQsb != null) {
4141 mSearchDropTargetBar.removeView(mQsb);
4142 mQsb = null;
4143 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004144 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004145 }
4146
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004147 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004148 * Add the icons for all apps.
4149 *
4150 * Implementation of the method from LauncherModel.Callbacks.
4151 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004152 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004153 if (mAppsView != null) {
4154 mAppsView.setApps(apps);
4155 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004156 if (mWidgetsView != null) {
4157 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4158 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004159 }
Adam Cohen9211d422014-10-07 18:14:53 -07004160 if (mLauncherCallbacks != null) {
4161 mLauncherCallbacks.bindAllApplications(apps);
4162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 }
4164
4165 /**
4166 * A package was updated.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004170 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004171 Runnable r = new Runnable() {
4172 public void run() {
4173 bindAppsUpdated(apps);
4174 }
4175 };
4176 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004177 return;
4178 }
4179
Winson Chungb745afb2015-03-02 11:51:23 -08004180 if (mAppsView != null) {
4181 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004183 }
4184
Sunny Goyal4390ace2014-10-13 11:33:11 -07004185 @Override
4186 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4187 Runnable r = new Runnable() {
4188 public void run() {
4189 bindWidgetsRestored(widgets);
4190 }
4191 };
4192 if (waitUntilResume(r)) {
4193 return;
4194 }
4195 mWorkspace.widgetsRestored(widgets);
4196 }
4197
Joe Onorato9c1289c2009-08-17 11:03:03 -04004198 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004199 * Some shortcuts were updated in the background.
4200 *
4201 * Implementation of the method from LauncherModel.Callbacks.
4202 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004203 @Override
4204 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4205 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004206 Runnable r = new Runnable() {
4207 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004208 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004209 }
4210 };
4211 if (waitUntilResume(r)) {
4212 return;
4213 }
4214
Sunny Goyal4390ace2014-10-13 11:33:11 -07004215 if (!updated.isEmpty()) {
4216 mWorkspace.updateShortcuts(updated);
4217 }
4218
4219 if (!removed.isEmpty()) {
4220 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4221 for (ShortcutInfo si : removed) {
4222 removedComponents.add(si.getTargetComponent());
4223 }
4224 mWorkspace.removeItemsByComponentName(removedComponents, user);
4225 // Notify the drag controller
4226 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004227 }
4228 }
4229
4230 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004231 * Update the state of a package, typically related to install state.
4232 *
4233 * Implementation of the method from LauncherModel.Callbacks.
4234 */
Sunny Goyale755d462014-07-22 13:48:29 -07004235 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004236 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4237 Runnable r = new Runnable() {
4238 public void run() {
4239 bindRestoreItemsChange(updates);
4240 }
4241 };
4242 if (waitUntilResume(r)) {
4243 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004244 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004245
Sunny Goyal756adbc2015-04-16 15:20:51 -07004246 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004247 }
4248
4249 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004250 * A package was uninstalled. We take both the super set of packageNames
4251 * in addition to specific applications to remove, the reason being that
4252 * this can be called when a package is updated as well. In that scenario,
4253 * we only remove specific components from the workspace, where as
4254 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004255 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004256 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004257 * Implementation of the method from LauncherModel.Callbacks.
4258 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004260 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004261 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004262 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004263 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004264 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004265 }
Winson Chungd64d1762013-08-20 14:37:16 -07004266 };
4267 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004268 return;
4269 }
4270
Sunny Goyal1a745e82014-10-02 15:58:31 -07004271 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004272 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4273 for (AppInfo info : appInfos) {
4274 removedComponents.add(info.componentName);
4275 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004276 if (!packageNames.isEmpty()) {
4277 mWorkspace.removeItemsByPackageName(packageNames, user);
4278 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004279 if (!removedComponents.isEmpty()) {
4280 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004281 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004282 // Notify the drag controller
4283 mDragController.onAppsRemoved(packageNames, removedComponents);
4284
Sunny Goyal1a745e82014-10-02 15:58:31 -07004285 } else {
4286 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004287 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004288
Winson Chungdf95eb12013-10-16 14:57:07 -07004289 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004290 if (mAppsView != null) {
4291 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004293 }
Joe Onoratobe386092009-11-17 17:32:16 -08004294
4295 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004296 * A number of packages were updated.
4297 */
Adam Cohen091440a2015-03-18 14:16:05 -07004298 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004299 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004300 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004301 bindPackagesUpdated(mWidgetsAndShortcuts);
4302 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004304 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004305
Michael Jurkac402cd92013-05-20 15:49:32 +02004306 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4307 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4308 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004309 return;
4310 }
4311
Hyunyoung Song3f471442015-04-08 19:01:34 -07004312 if (mWidgetsView != null) {
4313 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4314 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004315 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004316 }
4317
Winson Chung400438b2011-01-16 17:53:48 -08004318 private int mapConfigurationOriActivityInfoOri(int configOri) {
4319 final Display d = getWindowManager().getDefaultDisplay();
4320 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4321 switch (d.getRotation()) {
4322 case Surface.ROTATION_0:
4323 case Surface.ROTATION_180:
4324 // We are currently in the same basic orientation as the natural orientation
4325 naturalOri = configOri;
4326 break;
4327 case Surface.ROTATION_90:
4328 case Surface.ROTATION_270:
4329 // We are currently in the other basic orientation to the natural orientation
4330 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4331 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4332 break;
4333 }
4334
4335 int[] oriMap = {
4336 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4337 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4338 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4339 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4340 };
4341 // Since the map starts at portrait, we need to offset if this device's natural orientation
4342 // is landscape.
4343 int indexOffset = 0;
4344 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4345 indexOffset = 1;
4346 }
4347 return oriMap[(d.getRotation() + indexOffset) % 4];
4348 }
Adam Cohen446e9402011-09-15 18:21:21 -07004349
Winson Chung4b919f82012-05-01 10:44:08 -07004350 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004351 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004352 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4353 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4354 .getConfiguration().orientation));
4355 } else {
4356 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4357 }
Winson Chung4b919f82012-05-01 10:44:08 -07004358 }
4359 }
4360 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004361 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004362 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004363 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004364 } else {
4365 mHandler.postDelayed(new Runnable() {
4366 public void run() {
4367 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4368 }
4369 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004370 }
Winson Chung4b919f82012-05-01 10:44:08 -07004371 }
Winson Chung400438b2011-01-16 17:53:48 -08004372 }
4373
Winson Chunge43a1e72014-01-15 10:33:02 -08004374 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004375 if (mLauncherCallbacks != null) {
4376 return mLauncherCallbacks.isLauncherPreinstalled();
4377 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004378 PackageManager pm = getPackageManager();
4379 try {
4380 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4381 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4382 return true;
4383 } else {
4384 return false;
4385 }
4386 } catch (NameNotFoundException e) {
4387 e.printStackTrace();
4388 return false;
4389 }
4390 }
4391
Adam Cohenea90f832014-05-21 15:03:34 -07004392 /**
4393 * This method indicates whether or not we should suggest default wallpaper dimensions
4394 * when our wallpaper cropper was not yet used to set a wallpaper.
4395 */
4396 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004397 if (mLauncherCallbacks != null) {
4398 return mLauncherCallbacks.overrideWallpaperDimensions();
4399 }
Adam Cohenea90f832014-05-21 15:03:34 -07004400 return true;
4401 }
4402
Adam Cohen432609a2014-03-13 17:03:22 -07004403 /**
4404 * To be overridden by subclasses to indicate that there is an activity to launch
4405 * before showing the standard launcher experience.
4406 */
4407 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004408 if (mLauncherCallbacks != null) {
4409 return mLauncherCallbacks.hasFirstRunActivity();
4410 }
Adam Cohen432609a2014-03-13 17:03:22 -07004411 return false;
4412 }
4413
4414 /**
4415 * To be overridden by subclasses to launch any first run activity
4416 */
4417 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004418 if (mLauncherCallbacks != null) {
4419 return mLauncherCallbacks.getFirstRunActivity();
4420 }
Adam Cohen432609a2014-03-13 17:03:22 -07004421 return null;
4422 }
4423
Winson Chung2826a402015-04-17 16:18:53 -07004424 /**
4425 * Returns whether the launcher callbacks overrides search in all apps.
4426 * @return
4427 */
4428 @Thunk boolean isAllAppsSearchOverridden() {
4429 if (mLauncherCallbacks != null) {
4430 return mLauncherCallbacks.overrideAllAppsSearch();
4431 }
4432 return false;
4433 }
4434
Winson Chunga6945242014-01-08 14:04:34 -08004435 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004436 return !ActivityManager.isRunningInTestHarness() &&
4437 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004438 }
4439
Adam Cohen4a9423d2014-03-28 14:22:31 -07004440 protected boolean hasRunFirstRunActivity() {
4441 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4442 }
4443
Adam Cohen432609a2014-03-13 17:03:22 -07004444 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 if (shouldRunFirstRunActivity() &&
4446 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004447 Intent firstRunIntent = getFirstRunActivity();
4448 if (firstRunIntent != null) {
4449 startActivity(firstRunIntent);
4450 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004451 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004452 }
4453 }
Adam Cohen432609a2014-03-13 17:03:22 -07004454 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004455 }
4456
4457 private void markFirstRunActivityShown() {
4458 SharedPreferences.Editor editor = mSharedPrefs.edit();
4459 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4460 editor.apply();
4461 }
4462
Adam Cohen432609a2014-03-13 17:03:22 -07004463 /**
4464 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4465 * screen that must be displayed and dismissed.
4466 */
4467 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.hasDismissableIntroScreen();
4470 }
Adam Cohen432609a2014-03-13 17:03:22 -07004471 return false;
4472 }
4473
4474 /**
4475 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4476 */
4477 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.getIntroScreen();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return null;
4482 }
4483
4484 /**
4485 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4486 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4487 */
4488 private boolean shouldShowIntroScreen() {
4489 return hasDismissableIntroScreen() &&
4490 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4491 }
4492
4493 protected void showIntroScreen() {
4494 View introScreen = getIntroScreen();
4495 changeWallpaperVisiblity(false);
4496 if (introScreen != null) {
4497 mDragLayer.showOverlayView(introScreen);
4498 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004499 if (mLauncherOverlayContainer != null) {
4500 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4501 }
Adam Cohen432609a2014-03-13 17:03:22 -07004502 }
4503
4504 public void dismissIntroScreen() {
4505 markIntroScreenDismissed();
4506 if (showFirstRunActivity()) {
4507 // We delay hiding the intro view until the first run activity is showing. This
4508 // avoids a blip.
4509 mWorkspace.postDelayed(new Runnable() {
4510 @Override
4511 public void run() {
4512 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004513 if (mLauncherOverlayContainer != null) {
4514 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4515 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004516 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004517 }
4518 }, ACTIVITY_START_DELAY);
4519 } else {
4520 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004521 if (mLauncherOverlayContainer != null) {
4522 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4523 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004524 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004525 }
4526 changeWallpaperVisiblity(true);
4527 }
4528
4529 private void markIntroScreenDismissed() {
4530 SharedPreferences.Editor editor = mSharedPrefs.edit();
4531 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4532 editor.apply();
4533 }
4534
Adam Cohen091440a2015-03-18 14:16:05 -07004535 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004536 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4537 // on the device, then we always show the first run cling experience (or if there is no
4538 // launcher2). Otherwise, we prompt the user upon started for migration
4539 LauncherClings launcherClings = new LauncherClings(this);
4540 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4541 if (mModel.canMigrateFromOldLauncherDb(this)) {
4542 launcherClings.showMigrationCling();
4543 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004544 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004545 }
4546 }
4547 }
4548
Winson Chunga6945242014-01-08 14:04:34 -08004549 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004550 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4551 if (mHotseat != null) mHotseat.setAlpha(1f);
4552 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4553 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004554 }
4555
4556 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004557 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4558 if (mHotseat != null) mHotseat.setAlpha(0f);
4559 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4560 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004561 }
4562
Mathew Inwood72fbec12013-11-19 15:45:07 +00004563 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004564 // Called from search suggestion, not supported in other profiles.
4565 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4566 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4567 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4568 myUser);
4569 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004570 return null;
4571 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004572 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004573 }
4574
4575 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4576 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004577 // Called from search suggestion, not supported in other profiles.
4578 return createShortcutDragInfo(shortcutIntent, caption, icon,
4579 UserHandleCompat.myUserHandle());
4580 }
4581
4582 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4583 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004584 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004585 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004586 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004587 }
4588
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004589 protected void moveWorkspaceToDefaultScreen() {
4590 mWorkspace.moveToDefaultScreen(false);
4591 }
4592
Mathew Inwood72fbec12013-11-19 15:45:07 +00004593 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4594 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004595 mWorkspace.onExternalDragStartedWithItem(dragView);
4596 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004597 }
4598
Anjali Koppalf5d29132014-02-28 13:33:27 -08004599 @Override
4600 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004601 if (mLauncherCallbacks != null) {
4602 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4603 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004604 }
4605
Winson Chung80baf5a2010-08-09 16:03:15 -07004606 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004607 * Prints out out state for debugging.
4608 */
4609 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004610 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004611 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004612 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4613 Log.d(TAG, "mRestoring=" + mRestoring);
4614 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4615 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004616 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004617 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004618 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004619
Daniel Sandler325dc232013-06-05 22:57:57 -04004620 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004621 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004622
4623 @Override
4624 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4625 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004626 synchronized (sDumpLogs) {
4627 writer.println(" ");
4628 writer.println("Debug logs: ");
4629 for (int i = 0; i < sDumpLogs.size(); i++) {
4630 writer.println(" " + sDumpLogs.get(i));
4631 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004632 }
Adam Cohen9211d422014-10-07 18:14:53 -07004633 if (mLauncherCallbacks != null) {
4634 mLauncherCallbacks.dump(prefix, fd, writer, args);
4635 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004636 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004637
4638 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004639 if (DEBUG_DUMP_LOG) {
4640 synchronized (sDumpLogs) {
4641 Log.d(TAG, "");
4642 Log.d(TAG, "*********************");
4643 Log.d(TAG, "Launcher debug logs: ");
4644 for (int i = 0; i < sDumpLogs.size(); i++) {
4645 Log.d(TAG, " " + sDumpLogs.get(i));
4646 }
4647 Log.d(TAG, "*********************");
4648 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004649 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004650 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004651 }
4652
4653 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004654 addDumpLog(tag, log, null, debugLog);
4655 }
4656
4657 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004658 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004659 if (e != null) {
4660 Log.d(tag, log, e);
4661 } else {
4662 Log.d(tag, log);
4663 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004664 }
Winson Chungede41292013-09-19 16:27:36 -07004665 if (DEBUG_DUMP_LOG) {
4666 sDateStamp.setTime(System.currentTimeMillis());
4667 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004668 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4669 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004670 }
Winson Chungede41292013-09-19 16:27:36 -07004671 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004672 }
4673
Adam Cohen59400422014-03-05 18:07:04 -08004674 public static CustomAppWidget getCustomAppWidget(String name) {
4675 return sCustomAppWidgets.get(name);
4676 }
4677
4678 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4679 return sCustomAppWidgets;
4680 }
4681
Winson Chungede41292013-09-19 16:27:36 -07004682 public void dumpLogsToLocalData() {
4683 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004684 new AsyncTask<Void, Void, Void>() {
4685 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004686 boolean success = false;
4687 sDateStamp.setTime(sRunStart);
4688 String FILENAME = sDateStamp.getMonth() + "-"
4689 + sDateStamp.getDay() + "_"
4690 + sDateStamp.getHours() + "-"
4691 + sDateStamp.getMinutes() + "_"
4692 + sDateStamp.getSeconds() + ".txt";
4693
4694 FileOutputStream fos = null;
4695 File outFile = null;
4696 try {
4697 outFile = new File(getFilesDir(), FILENAME);
4698 outFile.createNewFile();
4699 fos = new FileOutputStream(outFile);
4700 } catch (Exception e) {
4701 e.printStackTrace();
4702 }
4703 if (fos != null) {
4704 PrintWriter writer = new PrintWriter(fos);
4705
4706 writer.println(" ");
4707 writer.println("Debug logs: ");
4708 synchronized (sDumpLogs) {
4709 for (int i = 0; i < sDumpLogs.size(); i++) {
4710 writer.println(" " + sDumpLogs.get(i));
4711 }
4712 }
4713 writer.close();
4714 }
4715 try {
4716 if (fos != null) {
4717 fos.close();
4718 success = true;
4719 }
4720 } catch (IOException e) {
4721 e.printStackTrace();
4722 }
Michael Jurka43467462013-11-14 12:03:58 +01004723 return null;
Winson Chungede41292013-09-19 16:27:36 -07004724 }
Michael Jurka43467462013-11-14 12:03:58 +01004725 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004726 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004728}
Michael Jurka2763be32011-02-24 11:19:57 -08004729
Michael Jurkaabded662011-03-04 12:06:57 -08004730interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004731 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004732 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004733 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004734 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004735 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004736}
Dan Sandlerd5024042014-01-09 15:01:33 -05004737
4738interface DebugIntents {
4739 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4740 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004741}