blob: b936c1fd5907388116171c0c5ea677888e71d948 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700103import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700105import com.android.launcher3.widget.PendingAddWidgetInfo;
106import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700107
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.DataInputStream;
109import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700111import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800116import java.lang.reflect.InvocationTargetException;
117import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700124import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000125import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
133 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700134 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700135 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Daniel Sandlerf061f822013-06-27 13:59:36 -0400137 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700138 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700139 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700141 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700142
Dan Sandlerd5024042014-01-09 15:01:33 -0500143 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Mathew Inwood876a8462013-06-14 14:12:41 +0100153 /**
154 * IntentStarter uses request codes starting with this. This must be greater than all activity
155 * request codes used internally.
156 */
157 protected static final int REQUEST_LAST = 100;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
160
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800161 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 // To turn on these properties, type
164 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Winson Chung2672ff92012-05-04 16:22:30 -0700167 // The Intent extra that defines whether to ignore the launch animation
168 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400169 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
172 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700173 // Type: int
174 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
177 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: boolean
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
185 // Type: long
186 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
191 // Type: parcelable
192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: int[]
196 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Adam Cohen432609a2014-03-13 17:03:22 -0700198 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800199 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
200
Adam Cohen3ed316a2014-07-23 18:21:20 -0700201 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
202 static final String ACTION_FIRST_LOAD_COMPLETE =
203 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
204
Adam Cohen39a06042013-07-19 14:30:12 -0700205 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700206 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700207
Sunny Goyal594d76d2014-11-06 10:12:54 -0800208 private static final String QSB_WIDGET_ID = "qsb_widget_id";
209 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
210
Winson Chunga6945242014-01-08 14:04:34 -0800211 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
212
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700213 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800214 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700215 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700218 private boolean mIsSafeModeEnabled;
219
Adam Cohenc2d6e892014-10-16 09:49:24 -0700220 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
221 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700222 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700225 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
226 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700227 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000229 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
230 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
231
Winson Chunga2413752012-04-03 14:22:34 -0700232 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700233 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
234 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700235 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700236
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800237 private final BroadcastReceiver mCloseSystemDialogsReceiver
238 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800239 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private LayoutInflater mInflater;
242
Adam Cohen091440a2015-03-18 14:16:05 -0700243 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700244 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800245 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800247 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700248 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700249
Sunny Goyalffe83f12014-08-14 17:39:34 -0700250 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700251 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800254 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800255 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700256
Michael Jurka0280c3b2010-09-17 15:00:07 -0700257 private int[] mTmpAddItemCellCoordinates = new int[2];
258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 private FolderInfo mFolderInfo;
260
Winson Chung3d503fb2011-07-13 17:25:49 -0700261 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800262 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700263
Michael Jurka838a4ca2011-02-07 13:33:06 -0800264 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700265
Winson Chungc51db6a2011-10-05 11:44:49 -0700266 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
268 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700269 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
271 // Main container view for the widget tray screen.
272 private WidgetsContainerView mWidgetsView;
273
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800275 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700278 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
279 // scroll issues (because the workspace may not have been measured yet) and extra work.
280 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
281 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
283 private SpannableStringBuilder mDefaultKeySsb = null;
284
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 private boolean mRestoring;
289 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700290 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700293 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private Bundle mSavedInstanceState;
296
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800298 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700301 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305
Sunny Goyale2df0622015-04-24 11:27:00 -0700306 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700307
Adam Cohen61f560d2013-09-30 15:58:20 -0700308 private View.OnTouchListener mHapticFeedbackTouchListener;
309
Adam Cohended9f8d2010-11-03 13:25:16 -0700310 // Related to the auto-advancing of widgets
311 private final int ADVANCE_MSG = 1;
312 private final int mAdvanceInterval = 20000;
313 private final int mAdvanceStagger = 250;
314 private long mAutoAdvanceSentTime;
315 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700317 new HashMap<View, AppWidgetProviderInfo>();
318
Winson Chung400438b2011-01-16 17:53:48 -0800319 // Determines how long to wait after a rotation before restoring the screen orientation to
320 // match the sensor state.
321 private final int mRestoreScreenOrientationDelay = 500;
322
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700324
Adam Cohen1462de32012-07-24 22:34:36 -0700325 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700326 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700327
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700328 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700329 static Date sDateStamp = new Date();
330 static DateFormat sDateFormat =
331 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
332 static long sRunStart = System.currentTimeMillis();
333 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334
Winson Chung46353de2012-02-16 14:05:10 -0800335 // We only want to get the SharedPreferences once since it does an FS stat each time we get
336 // it from the context.
337 private SharedPreferences mSharedPrefs;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700341 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 private BubbleTextView mWaitingForResume;
347
Adam Cohen59400422014-03-05 18:07:04 -0800348 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
349 new HashMap<String, CustomAppWidget>();
350
351 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
352 static {
353 if (ENABLE_CUSTOM_WIDGET_TEST) {
354 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
355 }
356 }
357
Chet Haasea8f996d2015-02-17 12:56:04 -0800358 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
359 private static Method sClipRevealMethod = null;
360 static {
361 Class<?> activityOptionsClass = ActivityOptions.class;
362 try {
363 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
364 View.class, int.class, int.class, int.class, int.class);
365 } catch (Exception e) {
366 // Earlier version
367 }
368 }
369
Adam Cohen091440a2015-03-18 14:16:05 -0700370 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700371 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700372 if (mWorkspace != null) {
373 mWorkspace.buildPageHardwareLayers();
374 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700375 }
376 };
377
Adam Cohendb364c32014-05-20 14:23:40 -0700378 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379
Adam Cohen091440a2015-03-18 14:16:05 -0700380 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381 int requestCode;
382 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700383 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700384 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800385 int cellX;
386 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700387 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 }
389
Daniel Sandlerff02d492013-08-05 02:12:05 -0400390 private Stats mStats;
391
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700392 FocusIndicatorView mFocusHandler;
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 @Override
395 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700396 if (DEBUG_STRICT_MODE) {
397 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
398 .detectDiskReads()
399 .detectDiskWrites()
400 .detectNetwork() // or .detectAll() for all detectable problems
401 .penaltyLog()
402 .build());
403 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
404 .detectLeakedSqlLiteObjects()
405 .detectLeakedClosableObjects()
406 .penaltyLog()
407 .penaltyDeath()
408 .build());
409 }
410
Adam Cohen9211d422014-10-07 18:14:53 -0700411 if (mLauncherCallbacks != null) {
412 mLauncherCallbacks.preOnCreate();
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700419 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700420
Winson Chung5f8afe62013-08-12 16:19:28 -0700421 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700422 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700423
424 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400425 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700426 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700427 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 mModel = app.setLauncher(this);
429 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700430 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400431 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800433 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Daniel Sandlerff02d492013-08-05 02:12:05 -0400435 mStats = new Stats(this);
436
Sunny Goyalffe83f12014-08-14 17:39:34 -0700437 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700438
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
440 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700442 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
443 // this also ensures that any synchronous binding below doesn't re-trigger another
444 // LauncherModel load.
445 mPaused = false;
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200448 android.os.Debug.startMethodTracing(
449 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451
452 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100456 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800458 registerContentObservers();
459
Joe Onorato7c312c12009-08-13 21:36:53 -0700460 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 mSavedState = savedInstanceState;
463 restoreState(mSavedState);
464
465 if (PROFILE_STARTUP) {
466 android.os.Debug.stopMethodTracing();
467 }
468
469 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700470 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 // If the user leaves launcher, then we should just load items asynchronously when
472 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500473 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 } else {
475 // We only load the page synchronously if the user rotates (or triggers a
476 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800477 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
486 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800487
Adam Cohenf9426d52012-06-04 17:26:21 -0700488 // On large interfaces, we want the screen to auto-rotate based on the current orientation
489 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 if (mLauncherCallbacks.hasLauncherOverlay()) {
494 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700495 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
496 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
497 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700498 mWorkspace.setLauncherOverlay(mLauncherOverlay);
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700501
502 if (shouldShowIntroScreen()) {
503 showIntroScreen();
504 } else {
505 showFirstRunActivity();
506 showFirstRunClings();
507 }
Adam Cohen9211d422014-10-07 18:14:53 -0700508 }
509
510 private LauncherCallbacks mLauncherCallbacks;
511
512 public void onPostCreate(Bundle savedInstanceState) {
513 super.onPostCreate(savedInstanceState);
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onPostCreate(savedInstanceState);
516 }
517 }
518
519 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
520 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700521 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
522 @Override
523 public void onAllAppsBoundsChanged(Rect bounds) {
524 mAppsView.setFixedBounds(Launcher.this, bounds);
525 }
526
527 @Override
528 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700529 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
530 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700531 }
532 });
Adam Cohen9211d422014-10-07 18:14:53 -0700533 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700534 }
535
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700536 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700537 public void onLauncherProviderChange() {
538 if (mLauncherCallbacks != null) {
539 mLauncherCallbacks.onLauncherProviderChange();
540 }
541 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700542
Adam Cohen9211d422014-10-07 18:14:53 -0700543 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700544 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700545 if (mLauncherCallbacks != null) {
546 return mLauncherCallbacks.hasCustomContentToLeft();
547 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700548 return false;
549 }
550
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500552 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600553 * {@link #addToCustomContentPage}. This will only be invoked if
554 * {@link #hasCustomContentToLeft()} is {@code true}.
555 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500556 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700557 if (mLauncherCallbacks != null) {
558 mLauncherCallbacks.populateCustomContentContainer();
559 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600560 }
561
562 /**
563 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
564 * ensure the custom content page is added or removed if necessary.
565 */
566 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600567 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600568 // Not bound yet, wait for bindScreens to be called.
569 return;
570 }
571
572 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
573 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500574 mWorkspace.createCustomContentContainer();
575 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600576 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
577 mWorkspace.removeCustomContentPage();
578 }
579 }
580
Adam Cohen091440a2015-03-18 14:16:05 -0700581 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700582 if (sLocaleConfiguration == null) {
583 new AsyncTask<Void, Void, LocaleConfiguration>() {
584 @Override
585 protected LocaleConfiguration doInBackground(Void... unused) {
586 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
587 readConfiguration(Launcher.this, localeConfiguration);
588 return localeConfiguration;
589 }
590
591 @Override
592 protected void onPostExecute(LocaleConfiguration result) {
593 sLocaleConfiguration = result;
594 checkForLocaleChange(); // recursive, but now with a locale configuration
595 }
596 }.execute();
597 return;
598 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final Configuration configuration = getResources().getConfiguration();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final String locale = configuration.locale.toString();
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mcc = configuration.mcc;
607
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 final int mnc = configuration.mnc;
610
Romain Guy84f296c2009-11-04 15:00:44 -0800611 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612
Romain Guy84f296c2009-11-04 15:00:44 -0800613 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 sLocaleConfiguration.locale = locale;
615 sLocaleConfiguration.mcc = mcc;
616 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700617
Joe Onorato0589f0f2010-02-08 13:44:00 -0800618 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700619
620 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100621 new AsyncTask<Void, Void, Void>() {
622 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100624 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700625 }
Michael Jurka43467462013-11-14 12:03:58 +0100626 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700627 }
628 }
629
Adam Cohen091440a2015-03-18 14:16:05 -0700630 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700631 public String locale;
632 public int mcc = -1;
633 public int mnc = -1;
634 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700635
Adam Cohen091440a2015-03-18 14:16:05 -0700636 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700637 DataInputStream in = null;
638 try {
Helena Josol28db2802014-10-09 17:04:09 +0100639 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700640 configuration.locale = in.readUTF();
641 configuration.mcc = in.readInt();
642 configuration.mnc = in.readInt();
643 } catch (FileNotFoundException e) {
644 // Ignore
645 } catch (IOException e) {
646 // Ignore
647 } finally {
648 if (in != null) {
649 try {
650 in.close();
651 } catch (IOException e) {
652 // Ignore
653 }
654 }
655 }
656 }
657
Adam Cohen091440a2015-03-18 14:16:05 -0700658 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700659 DataOutputStream out = null;
660 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100661 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100662 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700663 out.writeUTF(configuration.locale);
664 out.writeInt(configuration.mcc);
665 out.writeInt(configuration.mnc);
666 out.flush();
667 } catch (FileNotFoundException e) {
668 // Ignore
669 } catch (IOException e) {
670 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100671 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700672 } finally {
673 if (out != null) {
674 try {
675 out.close();
676 } catch (IOException e) {
677 // Ignore
678 }
679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681 }
682
Anton Hanssona2f665f2013-09-26 12:18:32 +0100683 public Stats getStats() {
684 return mStats;
685 }
686
Bjorn Bringertc459e522013-06-07 19:36:01 +0100687 public LayoutInflater getInflater() {
688 return mInflater;
689 }
690
Hyunyoung Song3f471442015-04-08 19:01:34 -0700691 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700692 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
693 // that is subsequently removed from the workspace in startBinding().
694 return !mModel.isLoadingWorkspace();
695 }
696
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800697 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000698 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100699 if (Build.VERSION.SDK_INT >= 17) {
700 return View.generateViewId();
701 } else {
702 // View.generateViewId() is not available. The following fallback logic is a copy
703 // of its implementation.
704 for (;;) {
705 final int result = sNextGeneratedId.get();
706 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
707 int newValue = result + 1;
708 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
709 if (sNextGeneratedId.compareAndSet(result, newValue)) {
710 return result;
711 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000712 }
713 }
714 }
715
716 public int getViewIdForItem(ItemInfo info) {
717 // This cast is safe given the > 2B range for int.
718 int itemId = (int) info.id;
719 if (mItemIdToViewId.containsKey(itemId)) {
720 return mItemIdToViewId.get(itemId);
721 }
722 int viewId = generateViewId();
723 mItemIdToViewId.put(itemId, viewId);
724 return viewId;
725 }
726
727 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700728 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
729 * a configuration step, this allows the proper animations to run after other transitions.
730 */
Adam Cohendb364c32014-05-20 14:23:40 -0700731 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700732 long screenId = args.screenId;
733 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
734 // When the screen id represents an actual screen (as opposed to a rank) we make sure
735 // that the drop page actually exists.
736 screenId = ensurePendingDropLayoutExists(args.screenId);
737 }
Adam Cohendb364c32014-05-20 14:23:40 -0700738
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700741 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700745 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700747 case REQUEST_RECONFIGURE_APPWIDGET:
748 completeRestoreAppWidget(args.appWidgetId);
749 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800750 }
Michael Jurka27614d22012-04-02 06:40:22 -0700751 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
752 // if you turned the screen off and then back while in All Apps, Launcher would not
753 // return to the workspace. Clearing mAddInfo.container here fixes this issue
754 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700755 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800756 }
757
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800758 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700760 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700761 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700762 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800763 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700764
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 Runnable exitSpringLoaded = new Runnable() {
766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
769 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
770 }
771 };
772
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
776 if (resultCode == RESULT_CANCELED) {
777 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700778 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800781 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 }
784 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200785 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
786 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700787 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200788 }
789 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200791
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
793 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700794
Adam Cohendb364c32014-05-20 14:23:40 -0700795 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800796 // We have special handling for widgets
797 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800798 final int appWidgetId;
799 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
800 : -1;
801 if (widgetId < 0) {
802 appWidgetId = pendingAddWidgetId;
803 } else {
804 appWidgetId = widgetId;
805 }
806
Adam Cohenad4e15c2013-10-17 16:21:35 -0700807 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800808 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700809 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
810 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700811 result = RESULT_CANCELED;
812 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700813 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 @Override
815 public void run() {
816 exitSpringLoadedDragModeDelayed(false, 0, null);
817 }
818 };
Adam Cohendb364c32014-05-20 14:23:40 -0700819 if (workspaceLocked) {
820 // No need to remove the empty screen if we're mid-binding, as the
821 // the bind will not add the empty screen.
822 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
823 } else {
824 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
825 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
826 }
Winson Chung5aaab772012-04-27 15:24:02 -0700827 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700828 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700829 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
830 // When the screen id represents an actual screen (as opposed to a rank)
831 // we make sure that the drop page actually exists.
832 mPendingAddInfo.screenId =
833 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
834 }
Adam Cohendb364c32014-05-20 14:23:40 -0700835 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
836
837 dropLayout.setDropPending(true);
838 final Runnable onComplete = new Runnable() {
839 @Override
840 public void run() {
841 completeTwoStageWidgetDrop(resultCode, appWidgetId);
842 dropLayout.setDropPending(false);
843 }
844 };
845 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
846 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
847 } else {
848 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
849 mPendingAddInfo);
850 sPendingAddItem = args;
851 }
Winson Chung5aaab772012-04-27 15:24:02 -0700852 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 return;
854 }
855
Sunny Goyalff572272014-07-23 13:58:07 -0700856 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
857 if (resultCode == RESULT_OK) {
858 // Update the widget view.
859 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
860 pendingAddWidgetId, mPendingAddInfo);
861 if (workspaceLocked) {
862 sPendingAddItem = args;
863 } else {
864 completeAdd(args);
865 }
866 }
867 // Leave the widget in the pending state if the user canceled the configure.
868 return;
869 }
870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 // The pattern used here is that a user PICKs a specific application,
872 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
875 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700876 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700877 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
878 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800879 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700880 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800881 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700882 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700883 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
884 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
Adam Cohen00074722013-10-11 17:46:09 -0700886 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700887 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700888 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800890 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800891
892 }
893
894 @Override
895 protected void onActivityResult(
896 final int requestCode, final int resultCode, final Intent data) {
897 handleActivityResult(requestCode, resultCode, data);
898 if (mLauncherCallbacks != null) {
899 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
900 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800901 }
902
Adam Cohendb364c32014-05-20 14:23:40 -0700903 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
904 appWidgetId, ItemInfo info) {
905 PendingAddArguments args = new PendingAddArguments();
906 args.requestCode = requestCode;
907 args.intent = data;
908 args.container = info.container;
909 args.screenId = info.screenId;
910 args.cellX = info.cellX;
911 args.cellY = info.cellY;
912 args.appWidgetId = appWidgetId;
913 return args;
914 }
915
916 /**
917 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
918 *
919 * @param screenId the screen id to check
920 * @return the new screen, or screenId if it exists
921 */
922 private long ensurePendingDropLayoutExists(long screenId) {
923 CellLayout dropLayout =
924 (CellLayout) mWorkspace.getScreenWithId(screenId);
925 if (dropLayout == null) {
926 // it's possible that the add screen was removed because it was
927 // empty and a re-bind occurred
928 mWorkspace.addExtraEmptyScreen();
929 return mWorkspace.commitExtraEmptyScreen();
930 } else {
931 return screenId;
932 }
933 }
934
Adam Cohen091440a2015-03-18 14:16:05 -0700935 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700936 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 Runnable onCompleteRunnable = null;
939 int animationType = 0;
940
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 if (resultCode == RESULT_OK) {
943 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
944 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 mPendingAddWidgetInfo);
946 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 onCompleteRunnable = new Runnable() {
948 @Override
949 public void run() {
950 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700952 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
953 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 }
955 };
956 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800957 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700960 if (mDragLayer.getAnimatedView() != null) {
961 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
962 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
963 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700964 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700965 // The animated view may be null in the case of a rotation during widget configuration
966 onCompleteRunnable.run();
967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 protected void onStop() {
972 super.onStop();
973 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700974
975 if (mLauncherCallbacks != null) {
976 mLauncherCallbacks.onStop();
977 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
981 protected void onStart() {
982 super.onStart();
983 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700984
985 if (mLauncherCallbacks != null) {
986 mLauncherCallbacks.onStart();
987 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700988 }
989
990 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200992 long startTime = 0;
993 if (DEBUG_RESUME_TIME) {
994 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400995 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200996 }
Adam Cohen9211d422014-10-07 18:14:53 -0700997
998 if (mLauncherCallbacks != null) {
999 mLauncherCallbacks.preOnResume();
1000 }
1001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001003
Winson Chung4a2afa32012-07-19 14:53:05 -07001004 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001005 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001006 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001007 } else if (mOnResumeState == State.APPS) {
1008 showAppsView(false /* animated */, false /* resetListToTop */);
1009 } else if (mOnResumeState == State.WIDGETS) {
1010 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001011 }
1012 mOnResumeState = State.NONE;
1013
Craig Mautner360310b2012-10-26 15:13:08 -07001014 // Background was set to gradient in onPause(), restore to black if in all apps.
1015 setWorkspaceBackground(mState == State.WORKSPACE);
1016
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001017 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001019 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001020 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1026 // execute them here
1027 long startTimeCallbacks = 0;
1028 if (DEBUG_RESUME_TIME) {
1029 startTimeCallbacks = System.currentTimeMillis();
1030 }
1031
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1033 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 if (DEBUG_RESUME_TIME) {
1037 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1038 (System.currentTimeMillis() - startTimeCallbacks));
1039 }
Michael Jurka447bf842013-05-15 14:52:15 +02001040 }
Michael Jurka54554252013-08-01 12:52:23 +02001041 if (mOnResumeCallbacks.size() > 0) {
1042 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1043 mOnResumeCallbacks.get(i).run();
1044 }
1045 mOnResumeCallbacks.clear();
1046 }
Winson Chunge4e50662012-01-23 14:45:13 -08001047
1048 // Reset the pressed state of icons that were locked in the press state while activities
1049 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001051 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 mWaitingForResume.setStayPressed(false);
1053 }
Winson Chung82963d52013-10-09 11:20:57 -07001054
Adam Cohen06dff352012-06-01 17:17:08 -07001055 // It is possible that widgets can receive updates while launcher is not in the foreground.
1056 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1057 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1058 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001059 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001060 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001061
Michael Jurka447bf842013-05-15 14:52:15 +02001062 if (DEBUG_RESUME_TIME) {
1063 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1064 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001065
1066 if (mWorkspace.getCustomContentCallbacks() != null) {
1067 // If we are resuming and the custom content is the current page, we call onShow().
1068 // It is also poassible that onShow will instead be called slightly after first layout
1069 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1070 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001071 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 }
1073 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001074 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001075 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001076
Sunny Goyal756adbc2015-04-16 15:20:51 -07001077 if (!isWorkspaceLoading()) {
1078 // Process any items that were added while Launcher was away.
1079 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1080 }
Adam Cohen9211d422014-10-07 18:14:53 -07001081
1082 if (mLauncherCallbacks != null) {
1083 mLauncherCallbacks.onResume();
1084 }
Adam Cohen06dff352012-06-01 17:17:08 -07001085 }
1086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001089 // Ensure that items added to Launcher are queued until Launcher returns
1090 InstallShortcutReceiver.enableInstallQueue();
1091
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001093 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001094 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001095 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001096
1097 // We call onHide() aggressively. The custom content callbacks should be able to
1098 // debounce excess onHide calls.
1099 if (mWorkspace.getCustomContentCallbacks() != null) {
1100 mWorkspace.getCustomContentCallbacks().onHide();
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Adam Cohen9211d422014-10-07 18:14:53 -07001103 if (mLauncherCallbacks != null) {
1104 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
1107
1108 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001109 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1110 // by a onResume or by scrolling otherwise.
1111 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001112
1113 // Custom content is completely hidden
1114 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001115
1116 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1117 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001118
1119 // Indicates whether the user is allowed to scroll away from the custom content.
1120 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001121 }
1122
Adam Cohenc2d6e892014-10-16 09:49:24 -07001123 public interface LauncherOverlay {
1124
1125 /**
1126 * Touch interaction leading to overscroll has begun
1127 */
1128 public void onScrollInteractionBegin();
1129
1130 /**
1131 * Touch interaction related to overscroll has ended
1132 */
1133 public void onScrollInteractionEnd();
1134
1135 /**
1136 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1137 * screen (or in the case of RTL, the rightmost screen).
1138 */
1139 public void onScrollChange(int progress, boolean rtl);
1140
1141 /**
1142 * Screen has stopped scrolling
1143 */
1144 public void onScrollSettled();
1145
1146 /**
1147 * This method can be called by the Launcher in order to force the LauncherOverlay
1148 * to exit fully immersive mode.
1149 */
1150 public void forceExitFullImmersion();
1151 }
1152
Winson Chung0f785722015-04-08 10:27:49 -07001153 public interface LauncherAppsCallbacks {
1154 /**
1155 * Updates launcher to the available space that AllApps can take so as not to overlap with
1156 * any other views.
1157 */
1158 public void onAllAppsBoundsChanged(Rect bounds);
1159
1160 /**
1161 * Called to dismiss all apps if it is showing.
1162 */
1163 public void dismissAllApps();
1164 }
1165
Adam Cohenc2d6e892014-10-16 09:49:24 -07001166 public interface LauncherOverlayCallbacks {
1167 /**
1168 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1169 * however it doesn't modify any state within the launcher.
1170 */
1171 public boolean canEnterFullImmersion();
1172
1173 /**
1174 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1175 * eg. by occupying the full screen and handling all touch events.
1176 *
1177 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1178 * case, Launcher will modify any necessary state and assumes the overlay is
1179 * handling all interaction. If false, the LauncherOverlay should cancel any
1180 *
1181 */
1182 public boolean enterFullImmersion();
1183
1184 /**
1185 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1186 * full control over UI and state.
1187 */
1188 public void exitFullImmersion();
1189 }
1190
1191 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1192
1193 @Override
1194 public boolean canEnterFullImmersion() {
1195 return mState == State.WORKSPACE;
1196 }
1197
1198 @Override
1199 public boolean enterFullImmersion() {
1200 if (mState == State.WORKSPACE) {
1201 // When fully immersed, disregard any touches which fall through.
1202 mDragLayer.setBlockTouch(true);
1203 return true;
1204 }
1205 return false;
1206 }
1207
1208 @Override
1209 public void exitFullImmersion() {
1210 mDragLayer.setBlockTouch(false);
1211 }
1212 }
1213
Jorim Jaggid017f882014-01-14 17:08:48 -08001214 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001215 if (mLauncherCallbacks != null) {
1216 return mLauncherCallbacks.hasSettings();
1217 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001218 return false;
1219 }
1220
Adam Cohen9211d422014-10-07 18:14:53 -07001221 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001222 CustomContentCallbacks callbacks, String description) {
1223 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001224 }
1225
Adam Cohenedb40762013-07-18 16:45:45 -07001226 // The custom content needs to offset its content to account for the QSB
1227 public int getTopOffsetForCustomContent() {
1228 return mWorkspace.getPaddingTop();
1229 }
1230
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001231 @Override
1232 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001234 if (mModel.isCurrentCallbacks(this)) {
1235 mModel.stopLoader();
1236 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001237 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1238
Romain Guy13c2e7b2010-03-10 19:45:00 -08001239 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001240 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001241
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001242 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001243 @Override
1244 public void onWindowFocusChanged(boolean hasFocus) {
1245 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001246 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001247
1248 if (mLauncherCallbacks != null) {
1249 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1250 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001251 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 private boolean acceptFilter() {
1254 final InputMethodManager inputManager = (InputMethodManager)
1255 getSystemService(Context.INPUT_METHOD_SERVICE);
1256 return !inputManager.isFullscreenMode();
1257 }
1258
1259 @Override
1260 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001261 final int uniChar = event.getUnicodeChar();
1262 final boolean handled = super.onKeyDown(keyCode, event);
1263 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1264 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1266 keyCode, event);
1267 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001268 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001269 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001270 // showSearchDialog()
1271 // If there are multiple keystrokes before the search dialog takes focus,
1272 // onSearchRequested() will be called for every keystroke,
1273 // but it is idempotent, so it's fine.
1274 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276 }
1277
Joe Onorato8a9625e2010-01-28 15:55:35 -08001278 // Eat the long press event so the keyboard doesn't come up.
1279 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1280 return true;
1281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 return handled;
1284 }
1285
Karl Rosaen138a0412009-04-23 19:00:21 -07001286 private String getTypedText() {
1287 return mDefaultKeySsb.toString();
1288 }
1289
1290 private void clearTypedText() {
1291 mDefaultKeySsb.clear();
1292 mDefaultKeySsb.clearSpans();
1293 Selection.setSelection(mDefaultKeySsb, 0);
1294 }
1295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001297 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1298 * State
1299 */
1300 private static State intToState(int stateOrdinal) {
1301 State state = State.WORKSPACE;
1302 final State[] stateValues = State.values();
1303 for (int i = 0; i < stateValues.length; i++) {
1304 if (stateValues[i].ordinal() == stateOrdinal) {
1305 state = stateValues[i];
1306 break;
1307 }
1308 }
1309 return state;
1310 }
1311
1312 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 * Restores the previous state, if it exists.
1314 *
1315 * @param savedState The previous state.
1316 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001317 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 private void restoreState(Bundle savedState) {
1319 if (savedState == null) {
1320 return;
1321 }
1322
Michael Jurka883f55b2010-10-21 15:47:14 -07001323 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001324 if (state == State.APPS || state == State.WIDGETS) {
1325 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001326 }
1327
Adam Cohen21cd0022013-10-09 18:57:02 -07001328 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1329 PagedView.INVALID_RESTORE_PAGE);
1330 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001331 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333
Winson Chung3d503fb2011-07-13 17:25:49 -07001334 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001335 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1338 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001339 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1341 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001342 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1343 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001344 AppWidgetProviderInfo info = savedState.getParcelable(
1345 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1346 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001347 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001348 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 mRestoring = true;
1350 }
1351
1352 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1353 if (renameFolder) {
1354 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001355 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 mRestoring = true;
1357 }
Winson Chunga12a2502010-12-20 14:41:35 -08001358
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001359 mItemIdToViewId = (HashMap<Integer, Integer>)
1360 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
1363 /**
1364 * Finds all the views we need and configure them properly.
1365 */
1366 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001367 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001368
Craig Mautner360310b2012-10-26 15:13:08 -07001369 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001370 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001371 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1372 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001373 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001374 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001375
John Spurlock77e1f472013-09-11 10:09:51 -04001376 mLauncherView.setSystemUiVisibility(
1377 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001378 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
Winson Chung4c98d922011-05-31 16:50:48 -07001380 // Setup the drag layer
1381 mDragLayer.setup(this, dragController);
1382
Winson Chung3d503fb2011-07-13 17:25:49 -07001383 // Setup the hotseat
1384 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1385 if (mHotseat != null) {
1386 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001387 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 }
1389
Jorim Jaggid017f882014-01-14 17:08:48 -08001390 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001391 View widgetButton = findViewById(R.id.widget_button);
1392 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 @Override
1394 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001395 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001396 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001397 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 }
1399 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001400 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1401
1402 View wallpaperButton = findViewById(R.id.wallpaper_button);
1403 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 @Override
1405 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001406 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001407 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001408 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409 }
1410 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001411 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412
1413 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 if (hasSettings()) {
1415 settingsButton.setOnClickListener(new OnClickListener() {
1416 @Override
1417 public void onClick(View arg0) {
1418 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001419 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001420 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001421 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001422 });
1423 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1424 } else {
1425 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001426 }
1427
Adam Cohendbdff6b2013-09-18 19:09:15 -07001428 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001429
Winson Chung4c98d922011-05-31 16:50:48 -07001430 // Setup the workspace
1431 mWorkspace.setHapticFeedbackEnabled(false);
1432 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001433 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001434 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001435
Winson Chungf0ea4d32011-06-06 14:27:16 -07001436 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001437 mSearchDropTargetBar = (SearchDropTargetBar)
1438 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001439
Winson Chungb745afb2015-03-02 11:51:23 -08001440 // Setup Apps
1441 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001442 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1443 mAppsView.hideSearchBar();
1444 }
Winson Chungb745afb2015-03-02 11:51:23 -08001445
Winson Chungf0ea4d32011-06-06 14:27:16 -07001446 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001447 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001448
Winson Chung3d503fb2011-07-13 17:25:49 -07001449 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001450 dragController.setDragScoller(mWorkspace);
1451 dragController.setScrollView(mDragLayer);
1452 dragController.setMoveTarget(mWorkspace);
1453 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001454 if (mSearchDropTargetBar != null) {
1455 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001456 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001457 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001458
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001459 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001460 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001461 mWeightWatcher = new WeightWatcher(this);
1462 mWeightWatcher.setAlpha(0.5f);
1463 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001464 new FrameLayout.LayoutParams(
1465 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001466 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001467 Gravity.BOTTOM)
1468 );
Adam Cohen39a06042013-07-19 14:30:12 -07001469
1470 boolean show = shouldShowWeightWatcher();
1471 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474
1475 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001476 * Sets the all apps button. This method is called from {@link Hotseat}.
1477 */
1478 public void setAllAppsButton(View allAppsButton) {
1479 mAllAppsButton = allAppsButton;
1480 }
1481
1482 public View getAllAppsButton() {
1483 return mAllAppsButton;
1484 }
1485
1486 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 * Creates a view representing a shortcut.
1488 *
1489 * @param info The data structure describing the shortcut.
1490 *
1491 * @return A View inflated from R.layout.application.
1492 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001493 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001495 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 /**
1499 * Creates a view representing a shortcut inflated from the specified resource.
1500 *
1501 * @param layoutResId The id of the XML layout used to create the shortcut.
1502 * @param parent The group the shortcut belongs to.
1503 * @param info The data structure describing the shortcut.
1504 *
1505 * @return A View inflated from layoutResId.
1506 */
Adam Cohen59400422014-03-05 18:07:04 -08001507 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001508 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001509 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001511 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 return favorite;
1513 }
1514
1515 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 * Add a shortcut to the workspace.
1517 *
1518 * @param data The intent describing the shortcut.
1519 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001522 int cellY) {
1523 int[] cellXY = mTmpAddItemCellCoordinates;
1524 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001525 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Michael Jurkad3ef3062010-11-23 16:23:58 -08001527 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528
Sunny Goyal2350bc92014-10-14 16:42:54 -07001529 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001530 if (info == null) {
1531 return;
1532 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 final View view = createShortcut(info);
1534
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 // First we check if we already know the exact location where we want to add this item.
1536 if (cellX >= 0 && cellY >= 0) {
1537 cellXY[0] = cellX;
1538 cellXY[1] = cellY;
1539 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001540
1541 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001542 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001543 true, null,null)) {
1544 return;
1545 }
1546 DragObject dragObject = new DragObject();
1547 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001548 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1549 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001550 return;
1551 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001552 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001553 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001555 foundCellSpan = (result != null);
1556 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001557 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001558 }
1559
1560 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001561 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 return;
1563 }
1564
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001565 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
1567 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001569 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571 }
1572
Adam Cohen2f093b62012-04-30 18:59:53 -07001573 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1574 int minHeight) {
1575 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001576 // We want to account for the extra amount of padding that we are adding to the widget
1577 // to ensure that it gets the full amount of space that it has requested
1578 int requiredWidth = minWidth + padding.left + padding.right;
1579 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001580 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001581 }
1582
Adam Cohen2f093b62012-04-30 18:59:53 -07001583 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1584 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001585 }
1586
Adam Cohen2f093b62012-04-30 18:59:53 -07001587 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1588 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001589 }
1590
Adam Cohen2f093b62012-04-30 18:59:53 -07001591 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1592 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001593 }
1594
Adam Cohen2f093b62012-04-30 18:59:53 -07001595 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1596 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001597 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001598 }
1599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001601 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001603 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 */
Adam Cohen091440a2015-03-18 14:16:05 -07001605 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001606 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1607
1608 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001609 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001610 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1611 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001612 }
Romain Guycbb89e42009-06-08 15:52:54 -07001613
Adam Cohen59400422014-03-05 18:07:04 -08001614 if (appWidgetInfo.isCustomWidget) {
1615 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001616 }
1617
Adam Cohen59400422014-03-05 18:07:04 -08001618 LauncherAppWidgetInfo launcherInfo;
1619 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1620 launcherInfo.spanX = info.spanX;
1621 launcherInfo.spanY = info.spanY;
1622 launcherInfo.minSpanX = info.minSpanX;
1623 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001624 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001627 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628
1629 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001630 if (hostView == null) {
1631 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001632 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1633 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 } else {
1635 // The AppWidgetHostView has already been inflated and instantiated
1636 launcherInfo.hostView = hostView;
1637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001639 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001640 launcherInfo.notifyWidgetSizeChanged(this);
1641
Adam Cohen59400422014-03-05 18:07:04 -08001642 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1643 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001644
1645 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001647 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1651 @Override
1652 public void onReceive(Context context, Intent intent) {
1653 final String action = intent.getAction();
1654 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1655 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001656 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001657 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001658
Winson Chungc100e8e2011-08-09 16:02:43 -07001659 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001660 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001661 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001662 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001663 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1666 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1669 mModel.resetLoadedState(false, true);
1670 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1671 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1672 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1673 mModel.resetLoadedState(false, true);
1674 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1675 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1676 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001677 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1678 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1679 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001680 }
1681 }
1682 };
1683
1684 @Override
1685 public void onAttachedToWindow() {
1686 super.onAttachedToWindow();
1687
1688 // Listen for broadcasts related to user-presence
1689 final IntentFilter filter = new IntentFilter();
1690 filter.addAction(Intent.ACTION_SCREEN_OFF);
1691 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001692 // For handling managed profiles
1693 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1694 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001695 if (ENABLE_DEBUG_INTENTS) {
1696 filter.addAction(DebugIntents.DELETE_DATABASE);
1697 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1698 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001700 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001701 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001702 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001703 mVisible = true;
1704 }
1705
Adam Cohen0b395352014-06-09 22:54:36 +00001706 /**
1707 * Sets up transparent navigation and status bars in LMP.
1708 * This method is a no-op for other platform versions.
1709 */
Sunny Goyald0091012015-01-20 15:55:34 -08001710 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001711 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001712 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001713 Window window = getWindow();
1714 window.getAttributes().systemUiVisibility |=
1715 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1716 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1717 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1718 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1719 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1720 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1721 window.setStatusBarColor(Color.TRANSPARENT);
1722 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001723 }
1724 }
1725
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 @Override
1727 public void onDetachedFromWindow() {
1728 super.onDetachedFromWindow();
1729 mVisible = false;
1730
Adam Cohend113e0c2010-11-11 10:48:05 -08001731 if (mAttached) {
1732 unregisterReceiver(mReceiver);
1733 mAttached = false;
1734 }
Winson Chungb745afb2015-03-02 11:51:23 -08001735 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737
1738 public void onWindowVisibilityChanged(int visibility) {
1739 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001740 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001741 // The following code used to be in onResume, but it turns out onResume is called when
1742 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1743 // is a more appropriate event to handle
1744 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001745 if (!mWorkspaceLoading) {
1746 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001747 // We want to let Launcher draw itself at least once before we force it to build
1748 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001749 // apps is nice and speedy.
1750 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001751 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001752 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001753 if (mStarted) return;
1754 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001755 // We delay the layer building a bit in order to give
1756 // other message processing a time to run. In particular
1757 // this avoids a delay in hiding the IME if it was
1758 // currently shown, because doing that may involve
1759 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001760 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 final ViewTreeObserver.OnDrawListener listener = this;
1762 mWorkspace.post(new Runnable() {
1763 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001764 if (mWorkspace != null &&
1765 mWorkspace.getViewTreeObserver() != null) {
1766 mWorkspace.getViewTreeObserver().
1767 removeOnDrawListener(listener);
1768 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001769 }
1770 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001771 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001772 }
1773 });
1774 }
1775 clearTypedText();
1776 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
1778
Adam Cohen091440a2015-03-18 14:16:05 -07001779 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 mHandler.removeMessages(ADVANCE_MSG);
1781 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1782 mHandler.sendMessageDelayed(msg, delay);
1783 mAutoAdvanceSentTime = System.currentTimeMillis();
1784 }
1785
Adam Cohen091440a2015-03-18 14:16:05 -07001786 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1788 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1789 mAutoAdvanceRunning = autoAdvanceRunning;
1790 if (autoAdvanceRunning) {
1791 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1792 sendAdvanceMessage(delay);
1793 } else {
1794 if (!mWidgetsToAdvance.isEmpty()) {
1795 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1796 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1797 }
1798 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001799 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 }
1801 }
1802 }
1803
1804 private final Handler mHandler = new Handler() {
1805 @Override
1806 public void handleMessage(Message msg) {
1807 if (msg.what == ADVANCE_MSG) {
1808 int i = 0;
1809 for (View key: mWidgetsToAdvance.keySet()) {
1810 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1811 final int delay = mAdvanceStagger * i;
1812 if (v instanceof Advanceable) {
1813 postDelayed(new Runnable() {
1814 public void run() {
1815 ((Advanceable) v).advance();
1816 }
1817 }, delay);
1818 }
1819 i++;
1820 }
1821 sendAdvanceMessage(mAdvanceInterval);
1822 }
1823 }
1824 };
1825
1826 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001827 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001828 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1829 if (v instanceof Advanceable) {
1830 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001831 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001832 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001833 }
1834 }
1835
1836 void removeWidgetToAutoAdvance(View hostView) {
1837 if (mWidgetsToAdvance.containsKey(hostView)) {
1838 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001839 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001840 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001841 }
1842
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001844 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 launcherInfo.hostView = null;
1846 }
1847
Hyunyoung Song3f471442015-04-08 19:01:34 -07001848 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001849 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1850 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001851 }
1852
Winson Chunga6945242014-01-08 14:04:34 -08001853 public DragLayer getDragLayer() {
1854 return mDragLayer;
1855 }
1856
Winson Chungb745afb2015-03-02 11:51:23 -08001857 public AppsContainerView getAppsView() {
1858 return mAppsView;
1859 }
1860
Hyunyoung Song3f471442015-04-08 19:01:34 -07001861 public WidgetsContainerView getWidgetsView() {
1862 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001863 }
1864
Winson Chunga6945242014-01-08 14:04:34 -08001865 public Workspace getWorkspace() {
1866 return mWorkspace;
1867 }
1868
1869 public Hotseat getHotseat() {
1870 return mHotseat;
1871 }
1872
Jorim Jaggid017f882014-01-14 17:08:48 -08001873 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001874 return mOverviewPanel;
1875 }
1876
1877 public SearchDropTargetBar getSearchBar() {
1878 return mSearchDropTargetBar;
1879 }
1880
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001881 public LauncherAppWidgetHost getAppWidgetHost() {
1882 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Romain Guycbb89e42009-06-08 15:52:54 -07001884
Winson Chunga9abd0e2010-10-27 17:18:37 -07001885 public LauncherModel getModel() {
1886 return mModel;
1887 }
1888
Winson Chunga6945242014-01-08 14:04:34 -08001889 protected SharedPreferences getSharedPrefs() {
1890 return mSharedPrefs;
1891 }
1892
Bjorn Bringertc459e522013-06-07 19:36:01 +01001893 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001894 getWindow().closeAllPanels();
1895
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001896 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001897 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 @Override
1901 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001902 long startTime = 0;
1903 if (DEBUG_RESUME_TIME) {
1904 startTime = System.currentTimeMillis();
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 super.onNewIntent(intent);
1907
1908 // Close the menu
1909 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001910 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001912
Adam Cohened307df2013-10-02 09:37:31 -07001913 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1914 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1915 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001916
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (mWorkspace == null) {
1918 // Can be cases where mWorkspace is null, this prevents a NPE
1919 return;
1920 }
1921 Folder openFolder = mWorkspace.getOpenFolder();
1922 // In all these cases, only animate if we're already on home
1923 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001924
1925 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1926 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001928 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001930 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001931
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 closeFolder();
1933 exitSpringLoadedDragMode();
1934
1935 // If we are already on home, then just animate back to the workspace,
1936 // otherwise, just wait until onResume to set the state back to Workspace
1937 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001938 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 } else {
1940 mOnResumeState = State.WORKSPACE;
1941 }
1942
1943 final View v = getWindow().peekDecorView();
1944 if (v != null && v.getWindowToken() != null) {
1945 InputMethodManager imm = (InputMethodManager)getSystemService(
1946 INPUT_METHOD_SERVICE);
1947 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1948 }
1949
Winson Chungb745afb2015-03-02 11:51:23 -08001950 // Reset the apps view
1951 if (!alreadyOnHome && mAppsView != null) {
1952 mAppsView.scrollToTop();
1953 }
1954
Hyunyoung Song3f471442015-04-08 19:01:34 -07001955 // Reset the widgets view
1956 if (!alreadyOnHome && mWidgetsView != null) {
1957 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001958 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001959
Adam Cohen9211d422014-10-07 18:14:53 -07001960 if (mLauncherCallbacks != null) {
1961 mLauncherCallbacks.onHomeIntent();
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
Adam Cohened307df2013-10-02 09:37:31 -07001964
Michael Jurka447bf842013-05-15 14:52:15 +02001965 if (DEBUG_RESUME_TIME) {
1966 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968
Adam Cohen9211d422014-10-07 18:14:53 -07001969 if (mLauncherCallbacks != null) {
1970 mLauncherCallbacks.onNewIntent(intent);
1971 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001972 }
1973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001975 public void onRestoreInstanceState(Bundle state) {
1976 super.onRestoreInstanceState(state);
1977 for (int page: mSynchronouslyBoundPages) {
1978 mWorkspace.restoreInstanceStateForChild(page);
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981
1982 @Override
1983 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001984 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001985 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1986 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001987 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001988 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Michael Jurka883f55b2010-10-21 15:47:14 -07001990 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001991 // We close any open folder since it will not be re-opened, and we need to make sure
1992 // this state is reflected.
1993 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 mWaitingForResult) {
1997 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2000 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002001 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2002 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2003 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002004 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
2007 if (mFolderInfo != null && mWaitingForResult) {
2008 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2009 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2010 }
Michael Jurka946ad472010-07-09 18:05:18 -07002011
Hyunyoung Song3f471442015-04-08 19:01:34 -07002012 // Save the current widgets tray?
2013 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002014 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002015
2016 if (mLauncherCallbacks != null) {
2017 mLauncherCallbacks.onSaveInstanceState(outState);
2018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 @Override
2022 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Winson Chunge7a03942011-08-05 15:05:12 -07002025 // Remove all pending runnables
2026 mHandler.removeMessages(ADVANCE_MSG);
2027 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002028 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002029
Winson Chungcd2b0142011-06-08 16:02:26 -07002030 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002031 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002032
2033 // It's possible to receive onDestroy after a new Launcher activity has
2034 // been created. In this case, don't interfere with the new Launcher.
2035 if (mModel.isCurrentCallbacks(this)) {
2036 mModel.stopLoader();
2037 app.setLauncher(null);
2038 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002041 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002043 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002045 mAppWidgetHost = null;
2046
2047 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048
2049 TextKeyListener.getInstance().release();
2050
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002051 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002052 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002053
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002054 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002055 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002056 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002057 mWorkspace = null;
2058 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002059
Michael Jurka2ecf9952012-06-18 12:52:28 -07002060 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002061
2062 if (mLauncherCallbacks != null) {
2063 mLauncherCallbacks.onDestroy();
2064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
Adam Cohena9cf38f2011-05-02 15:36:58 -07002067 public DragController getDragController() {
2068 return mDragController;
2069 }
2070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 @Override
2072 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002073 onStartForResult(requestCode);
2074 super.startActivityForResult(intent, requestCode);
2075 }
2076
2077 @Override
2078 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2079 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2080 onStartForResult(requestCode);
2081 try {
2082 super.startIntentSenderForResult(intent, requestCode,
2083 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2084 } catch (IntentSender.SendIntentException e) {
2085 throw new ActivityNotFoundException();
2086 }
2087 }
2088
2089 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002090 if (requestCode >= 0) {
2091 setWaitingForResult(true);
2092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
2094
Winson Chung70d72102011-08-12 11:24:35 -07002095 /**
2096 * Indicates that we want global search for this activity by setting the globalSearch
2097 * argument for {@link #startSearch} to true.
2098 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002100 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002103 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002104
Karl Rosaen138a0412009-04-23 19:00:21 -07002105 if (initialQuery == null) {
2106 // Use any text typed in the launcher as the initial query
2107 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 if (appSearchData == null) {
2110 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002111 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
Winson Chungadf0c182012-08-23 14:59:07 -07002113 Rect sourceBounds = new Rect();
2114 if (mSearchDropTargetBar != null) {
2115 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2116 }
Romain Guycbb89e42009-06-08 15:52:54 -07002117
Ian Parkinson047036e2014-09-01 15:40:53 +01002118 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002119 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002120 if (clearTextImmediately) {
2121 clearTypedText();
2122 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002123 }
2124
Ian Parkinson047036e2014-09-01 15:40:53 +01002125 /**
2126 * Start a text search.
2127 *
2128 * @return {@code true} if the search will start immediately, so any further keypresses
2129 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2130 * to buffer keypresses.
2131 */
2132 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002133 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002134 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2135 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2136 sourceBounds);
2137 }
2138
Michael Jurkaa33411c2012-06-14 16:18:21 -07002139 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002140 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002141 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002142 }
2143
2144 /**
2145 * Starts the global search activity. This code is a copied from SearchManager
2146 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002147 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002149 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002150 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2151 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2152 if (globalSearchActivity == null) {
2153 Log.w(TAG, "No global search activity found.");
2154 return;
2155 }
2156 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2157 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2158 intent.setComponent(globalSearchActivity);
2159 // Make sure that we have a Bundle to put source in
2160 if (appSearchData == null) {
2161 appSearchData = new Bundle();
2162 } else {
2163 appSearchData = new Bundle(appSearchData);
2164 }
Adam Cohen9211d422014-10-07 18:14:53 -07002165 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002166 if (!appSearchData.containsKey("source")) {
2167 appSearchData.putString("source", getPackageName());
2168 }
2169 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2170 if (!TextUtils.isEmpty(initialQuery)) {
2171 intent.putExtra(SearchManager.QUERY, initialQuery);
2172 }
2173 if (selectInitialQuery) {
2174 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2175 }
2176 intent.setSourceBounds(sourceBounds);
2177 try {
2178 startActivity(intent);
2179 } catch (ActivityNotFoundException ex) {
2180 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 }
2183
Yura4f93ec62014-02-04 14:15:21 +00002184 public boolean isOnCustomContent() {
2185 return mWorkspace.isOnOrMovingToCustomContent();
2186 }
2187
Winson Chung70d72102011-08-12 11:24:35 -07002188 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002189 public boolean onPrepareOptionsMenu(Menu menu) {
2190 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002191 if (!isOnCustomContent()) {
2192 // Close any open folders
2193 closeFolder();
2194 // Stop resizing any widgets
2195 mWorkspace.exitWidgetResizeMode();
2196 if (!mWorkspace.isInOverviewMode()) {
2197 // Show the overview mode
2198 showOverviewMode(true);
2199 } else {
2200 showWorkspace(true);
2201 }
Winson Chunge0298742014-01-17 12:03:00 -08002202 }
Adam Cohen9211d422014-10-07 18:14:53 -07002203 if (mLauncherCallbacks != null) {
2204 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2205 }
2206
Michael Jurkab94f3f82013-09-11 18:08:54 +02002207 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002210 @Override
2211 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002212 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002213 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002214 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002215 }
2216
Joe Onorato9c1289c2009-08-17 11:03:03 -04002217 public boolean isWorkspaceLocked() {
2218 return mWorkspaceLoading || mWaitingForResult;
2219 }
2220
Adam Cohen517a7f52014-03-01 12:12:59 -08002221 public boolean isWorkspaceLoading() {
2222 return mWorkspaceLoading;
2223 }
2224
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002225 private void setWorkspaceLoading(boolean value) {
2226 boolean isLocked = isWorkspaceLocked();
2227 mWorkspaceLoading = value;
2228 if (isLocked != isWorkspaceLocked()) {
2229 onWorkspaceLockedChanged();
2230 }
2231 }
2232
2233 private void setWaitingForResult(boolean value) {
2234 boolean isLocked = isWorkspaceLocked();
2235 mWaitingForResult = value;
2236 if (isLocked != isWorkspaceLocked()) {
2237 onWorkspaceLockedChanged();
2238 }
2239 }
2240
Adam Cohen9211d422014-10-07 18:14:53 -07002241 protected void onWorkspaceLockedChanged() {
2242 if (mLauncherCallbacks != null) {
2243 mLauncherCallbacks.onWorkspaceLockedChanged();
2244 }
2245 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002246
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2251 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002252 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002255
Sunny Goyale6b63a32015-01-16 16:14:29 -08002256 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002257 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002258 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2259 }
2260
Sunny Goyale6b63a32015-01-16 16:14:29 -08002261 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002262 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2263 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002264 if (appWidgetInfo.configure != null) {
2265 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002266 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002267
Bjorn Bringert7984c942009-12-09 15:38:25 +00002268 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002269 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2270 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002273 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002274 Runnable onComplete = new Runnable() {
2275 @Override
2276 public void run() {
2277 // Exit spring loaded mode if necessary after adding the widget
2278 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2279 null);
2280 }
2281 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002283 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002284 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 }
2286 }
Romain Guycbb89e42009-06-08 15:52:54 -07002287
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002288 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002289 // Close any folders that may be open.
2290 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002291 mWorkspace.moveToCustomContentScreen(animate);
2292 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002293
2294 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2295 int[] cell, int spanX, int spanY) {
2296 switch (info.itemType) {
2297 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2298 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2299 int span[] = new int[2];
2300 span[0] = spanX;
2301 span[1] = spanY;
2302 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2303 container, screenId, cell, span);
2304 break;
2305 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2306 processShortcutFromDrop(info.componentName, container, screenId, cell);
2307 break;
2308 default:
2309 throw new IllegalStateException("Unknown item type: " + info.itemType);
2310 }
2311 }
2312
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313 /**
2314 * Process a shortcut drop.
2315 *
2316 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002320 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2321 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002322 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002325 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326
2327 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 mPendingAddInfo.cellX = cell[0];
2329 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002331
2332 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2333 createShortcutIntent.setComponent(componentName);
2334 processShortcut(createShortcutIntent);
2335 }
2336
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 /**
2338 * Process a widget drop.
2339 *
2340 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002344 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2345 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002349 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002350 mPendingAddInfo.minSpanX = info.minSpanX;
2351 mPendingAddInfo.minSpanY = info.minSpanY;
2352
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 mPendingAddInfo.cellX = cell[0];
2355 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002357 if (span != null) {
2358 mPendingAddInfo.spanX = span[0];
2359 mPendingAddInfo.spanY = span[1];
2360 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361
Adam Cohened66b2b2012-01-23 17:28:51 -08002362 AppWidgetHostView hostView = info.boundWidget;
2363 int appWidgetId;
2364 if (hostView != null) {
2365 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002366 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002367 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002368 // In this case, we either need to start an activity to get permission to bind
2369 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002370 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002371 Bundle options = info.bindOptions;
2372
Sunny Goyalffe83f12014-08-14 17:39:34 -07002373 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2374 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002375 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002376 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002377 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002378 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002379 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2380 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002382 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2383 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002384 // TODO: we need to make sure that this accounts for the options bundle.
2385 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2387 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002388 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002389 }
2390
Joe Onoratodeb98af2010-02-19 14:59:39 -08002391 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002392 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 }
2394
Winson Chung24ab2f12010-09-16 14:10:47 -07002395 void processWallpaper(Intent intent) {
2396 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2397 }
2398
Adam Cohendcd297f2013-06-18 13:13:40 -07002399 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002401 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 folderInfo.title = getText(R.string.folder_name);
2403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002405 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2406 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002407 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408
2409 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 FolderIcon newFolder =
2411 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002412 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002413 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002414 // Force measure the new folder icon
2415 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2416 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002417 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 }
Romain Guycbb89e42009-06-08 15:52:54 -07002419
Joe Onorato9c1289c2009-08-17 11:03:03 -04002420 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002421 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 }
2423
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002424 /**
2425 * Registers various content observers. The current implementation registers
2426 * only a favorites observer to keep track of the favorites applications.
2427 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002428 private void registerContentObservers() {
2429 ContentResolver resolver = getContentResolver();
2430 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2431 true, mWidgetObserver);
2432 }
2433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 @Override
2435 public boolean dispatchKeyEvent(KeyEvent event) {
2436 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2437 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002440 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002441 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002442 dumpState();
2443 return true;
2444 }
2445 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 }
2447 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2448 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002449 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 return true;
2451 }
2452 }
2453
2454 return super.dispatchKeyEvent(event);
2455 }
2456
Joe Onorato88ec0992009-11-19 13:16:06 -08002457 @Override
2458 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002459 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2460 return;
2461 }
2462
Winson Chung3d9490a2015-03-24 17:38:42 -07002463 LauncherAccessibilityDelegate delegate =
2464 LauncherAppState.getInstance().getAccessibilityDelegate();
2465 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002466 return;
2467 }
2468
Winson Chungb745afb2015-03-02 11:51:23 -08002469 if (isAppsViewVisible()) {
2470 showWorkspace(true);
2471 } else if (isWidgetsViewVisible()) {
2472 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002473 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002474 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002475 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002476 Folder openFolder = mWorkspace.getOpenFolder();
2477 if (openFolder.isEditingName()) {
2478 openFolder.dismissEditingName();
2479 } else {
2480 closeFolder();
2481 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002482 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002483 mWorkspace.exitWidgetResizeMode();
2484
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002485 // Back button is a no-op here, but give at least some feedback for the button press
2486 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002487 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002488 }
2489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002491 * Re-listen when widgets are reset.
2492 */
Adam Cohen091440a2015-03-18 14:16:05 -07002493 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002494 if (mAppWidgetHost != null) {
2495 mAppWidgetHost.startListening();
2496 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002497 }
2498
2499 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 * Launches the intent referred by the clicked shortcut.
2501 *
2502 * @param v The view representing the clicked shortcut.
2503 */
2504 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002505 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2506 // view has detached (it's possible for this to happen if the view is removed mid touch).
2507 if (v.getWindowToken() == null) {
2508 return;
2509 }
2510
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002511 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002512 return;
2513 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002514
Adam Cohen1697b792013-09-17 19:08:21 -07002515 if (v instanceof Workspace) {
2516 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002517 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002518 }
2519 return;
2520 }
2521
2522 if (v instanceof CellLayout) {
2523 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002524 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002525 }
2526 }
2527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002529 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002532 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002534 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002535 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002536 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002537 } else if (tag instanceof AppInfo) {
2538 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002539 } else if (tag instanceof LauncherAppWidgetInfo) {
2540 if (v instanceof PendingAppWidgetHostView) {
2541 onClickPendingWidget((PendingAppWidgetHostView) v);
2542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 }
2544 }
2545
Sunny Goyal508da152014-08-14 10:53:27 -07002546 public void onClickPagedViewIcon(View v) {
2547 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002548 if (mLauncherCallbacks != null) {
2549 mLauncherCallbacks.onClickPagedViewIcon(v);
2550 }
Sunny Goyal508da152014-08-14 10:53:27 -07002551 }
2552
Michael Jurka0e260592010-06-30 17:07:39 -07002553 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002554 return false;
2555 }
2556
Michael Jurkaaf442092010-06-10 17:01:57 -07002557 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002558 * Event handler for the app widget view which has not fully restored.
2559 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002561 if (mIsSafeModeEnabled) {
2562 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2563 return;
2564 }
2565
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002566 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002567 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002568 int widgetId = info.appWidgetId;
2569 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2570 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002571 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2572 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002573 mPendingAddInfo.copyFrom(info);
2574 mPendingAddWidgetId = widgetId;
2575
Sunny Goyalffe83f12014-08-14 17:39:34 -07002576 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2577 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002578 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002579 } else if (info.installProgress < 0) {
2580 // The install has not been queued
2581 final String packageName = info.providerName.getPackageName();
2582 showBrokenAppInstallDialog(packageName,
2583 new DialogInterface.OnClickListener() {
2584 public void onClick(DialogInterface dialog, int id) {
2585 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2586 }
2587 });
2588 } else {
2589 // Download has started.
2590 final String packageName = info.providerName.getPackageName();
2591 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002592 }
2593 }
2594
2595 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002596 * Event handler for the "grid" button that appears on the home screen, which
2597 * enters all apps mode.
2598 *
2599 * @param v The view that was clicked.
2600 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 protected void onClickAllAppsButton(View v) {
2602 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002603 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 showWorkspace(true);
2605 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002606 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002607 }
2608 }
2609
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002610 private void showBrokenAppInstallDialog(final String packageName,
2611 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002612 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002613 .setTitle(R.string.abandoned_promises_title)
2614 .setMessage(R.string.abandoned_promise_explanation)
2615 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2616 .setNeutralButton(R.string.abandoned_clean_this,
2617 new DialogInterface.OnClickListener() {
2618 public void onClick(DialogInterface dialog, int id) {
2619 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2620 mWorkspace.removeAbandonedPromise(packageName, user);
2621 }
2622 })
2623 .create().show();
2624 return;
2625 }
2626
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 /**
2628 * Event handler for an app shortcut click.
2629 *
2630 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2631 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002633 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2634 Object tag = v.getTag();
2635 if (!(tag instanceof ShortcutInfo)) {
2636 throw new IllegalArgumentException("Input must be a Shortcut");
2637 }
2638
2639 // Open shortcut
2640 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002641
2642 if (shortcut.isDisabled != 0) {
2643 int error = R.string.activity_not_available;
2644 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2645 error = R.string.safemode_shortcut_error;
2646 }
2647 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2648 return;
2649 }
2650
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 final Intent intent = shortcut.intent;
2652
2653 // Check for special shortcuts
2654 if (intent.getComponent() != null) {
2655 final String shortcutClass = intent.getComponent().getClassName();
2656
2657 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2658 MemoryDumpActivity.startDump(this);
2659 return;
2660 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2661 toggleShowWeightWatcher();
2662 return;
2663 }
2664 }
2665
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002667 if ((v instanceof BubbleTextView)
2668 && shortcut.isPromise()
2669 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002670 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002671 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 new DialogInterface.OnClickListener() {
2673 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002674 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 return;
2678 }
2679
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002681 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002682
2683 if (mLauncherCallbacks != null) {
2684 mLauncherCallbacks.onClickAppShortcut(v);
2685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 }
2687
Adam Cohen091440a2015-03-18 14:16:05 -07002688 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002690 final ShortcutInfo shortcut;
2691 final Intent intent;
2692 if (tag instanceof ShortcutInfo) {
2693 shortcut = (ShortcutInfo) tag;
2694 intent = shortcut.intent;
2695 int[] pos = new int[2];
2696 v.getLocationOnScreen(pos);
2697 intent.setSourceBounds(new Rect(pos[0], pos[1],
2698 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002699
Sunny Goyal508da152014-08-14 10:53:27 -07002700 } else if (tag instanceof AppInfo) {
2701 shortcut = null;
2702 intent = ((AppInfo) tag).intent;
2703 } else {
2704 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2705 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706
2707 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 mStats.recordLaunch(intent, shortcut);
2709
2710 if (success && v instanceof BubbleTextView) {
2711 mWaitingForResume = (BubbleTextView) v;
2712 mWaitingForResume.setStayPressed(true);
2713 }
2714 }
2715
2716 /**
2717 * Event handler for a folder icon click.
2718 *
2719 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2720 */
2721 protected void onClickFolderIcon(View v) {
2722 if (LOGD) Log.d(TAG, "onClickFolder");
2723 if (!(v instanceof FolderIcon)){
2724 throw new IllegalArgumentException("Input must be a FolderIcon");
2725 }
2726
2727 FolderIcon folderIcon = (FolderIcon) v;
2728 final FolderInfo info = folderIcon.getFolderInfo();
2729 Folder openFolder = mWorkspace.getFolderForTag(info);
2730
2731 // If the folder info reports that the associated folder is open, then verify that
2732 // it is actually opened. There have been a few instances where this gets out of sync.
2733 if (info.opened && openFolder == null) {
2734 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2735 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2736 info.opened = false;
2737 }
2738
2739 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2740 // Close any open folder
2741 closeFolder();
2742 // Open the requested folder
2743 openFolder(folderIcon);
2744 } else {
2745 // Find the open folder...
2746 int folderScreen;
2747 if (openFolder != null) {
2748 folderScreen = mWorkspace.getPageForView(openFolder);
2749 // .. and close it
2750 closeFolder(openFolder);
2751 if (folderScreen != mWorkspace.getCurrentPage()) {
2752 // Close any folder open on the current screen
2753 closeFolder();
2754 // Pull the folder onto this screen
2755 openFolder(folderIcon);
2756 }
2757 }
2758 }
Adam Cohen9211d422014-10-07 18:14:53 -07002759
2760 if (mLauncherCallbacks != null) {
2761 mLauncherCallbacks.onClickFolderIcon(v);
2762 }
Michael Jurka5130e402011-10-13 04:55:35 -07002763 }
2764
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002765 /**
2766 * Event handler for the (Add) Widgets button that appears after a long press
2767 * on the home screen.
2768 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002769 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002771 if (mIsSafeModeEnabled) {
2772 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2773 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002774 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onClickAddWidgetButton(view);
2777 }
Adam Cohen9211d422014-10-07 18:14:53 -07002778 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002779 }
2780
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 /**
2782 * Event handler for the wallpaper picker button that appears after a long press
2783 * on the home screen.
2784 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002785 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002787 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2788 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002789
2790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onClickWallpaperPicker(v);
2792 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 }
2794
2795 /**
2796 * Event handler for a click on the settings button that appears after a long press
2797 * on the home screen.
2798 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002799 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002801 if (mLauncherCallbacks != null) {
2802 mLauncherCallbacks.onClickSettingsButton(v);
2803 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 }
2805
Michael Jurka5130e402011-10-13 04:55:35 -07002806 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002807 // Provide the same haptic feedback that the system offers for virtual keys.
2808 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002809 }
2810
Adam Cohen61f560d2013-09-30 15:58:20 -07002811 public void performHapticFeedbackOnTouchDown(View v) {
2812 // Provide the same haptic feedback that the system offers for virtual keys.
2813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2814 }
2815
2816 public View.OnTouchListener getHapticFeedbackTouchListener() {
2817 if (mHapticFeedbackTouchListener == null) {
2818 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2819 @Override
2820 public boolean onTouch(View v, MotionEvent event) {
2821 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2822 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2823 }
2824 return false;
2825 }
2826 };
2827 }
2828 return mHapticFeedbackTouchListener;
2829 }
2830
Adam Cohen9211d422014-10-07 18:14:53 -07002831 public void onDragStarted(View view) {
2832 if (isOnCustomContent()) {
2833 // Custom content screen doesn't participate in drag and drop. If on custom
2834 // content screen, move to default.
2835 moveWorkspaceToDefaultScreen();
2836 }
2837
2838 if (mLauncherCallbacks != null) {
2839 mLauncherCallbacks.onDragStarted(view);
2840 }
2841 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002842
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002843 /**
2844 * Called when the user stops interacting with the launcher.
2845 * This implies that the user is now on the homescreen and is not doing housekeeping.
2846 */
Adam Cohen9211d422014-10-07 18:14:53 -07002847 protected void onInteractionEnd() {
2848 if (mLauncherCallbacks != null) {
2849 mLauncherCallbacks.onInteractionEnd();
2850 }
2851 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002852
2853 /**
2854 * Called when the user starts interacting with the launcher.
2855 * The possible interactions are:
2856 * - open all apps
2857 * - reorder an app shortcut, or a widget
2858 * - open the overview mode.
2859 * This is a good time to stop doing things that only make sense
2860 * when the user is on the homescreen and not doing housekeeping.
2861 */
Adam Cohen9211d422014-10-07 18:14:53 -07002862 protected void onInteractionBegin() {
2863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onInteractionBegin();
2865 }
2866 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002867
Kenny Guyf07af7b2014-07-31 11:39:16 +01002868 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002869 try {
2870 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002871 launcherApps.showAppDetailsForProfile(componentName, user);
2872 } catch (SecurityException e) {
2873 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2874 Log.e(TAG, "Launcher does not have permission to launch settings");
2875 } catch (ActivityNotFoundException e) {
2876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2877 Log.e(TAG, "Unable to launch settings");
2878 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002879 }
2880
Michael Jurka1e2f4652013-07-08 18:03:46 -07002881 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002882 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2883 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002884 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002885 // System applications cannot be installed. For now, show a toast explaining that.
2886 // We may give them the option of disabling apps this way.
2887 int messageId = R.string.uninstall_system_app_text;
2888 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002889 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002890 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002891 String packageName = componentName.getPackageName();
2892 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002893 Intent intent = new Intent(
2894 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002895 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2896 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002897 if (user != null) {
2898 user.addToIntent(intent, Intent.EXTRA_USER);
2899 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002900 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002901 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002902 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002903 }
2904
Michael Jurka86a720e2012-05-09 11:23:23 -07002905 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002906 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002908 // Only launch using the new animation if the shortcut has not opted out (this is a
2909 // private contract between launcher and may be ignored in the future).
2910 boolean useLaunchAnimation = (v != null) &&
2911 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002912 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2913 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002914
Kenny Guy1317e2d2014-05-08 18:52:50 +01002915 UserHandleCompat user = null;
2916 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2917 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2918 user = userManager.getUserForSerialNumber(serialNumber);
2919 }
2920
2921 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002922 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002923 ActivityOptions opts = null;
2924 if (sClipRevealMethod != null) {
2925 // TODO: call method directly when Launcher3 can depend on M APIs
2926 int left = 0, top = 0;
2927 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2928 if (v instanceof TextView) {
2929 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002930 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2931 if (icon != null) {
2932 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002933 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002934 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002935 width = bounds.width();
2936 height = bounds.height();
2937 }
2938 }
2939 try {
2940 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2941 left, top, width, height);
2942 } catch (IllegalAccessException e) {
2943 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2944 sClipRevealMethod = null;
2945 } catch (InvocationTargetException e) {
2946 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2947 sClipRevealMethod = null;
2948 }
2949 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002950 if (opts == null && !Utilities.isLmpOrAbove()) {
2951 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002952 v.getMeasuredWidth(), v.getMeasuredHeight());
2953 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002954 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002955 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002956
2957 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2958 // Could be launching some bookkeeping activity
2959 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002960 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002961 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002962 launcherApps.startActivityForProfile(intent.getComponent(), user,
2963 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002964 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002965 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 } catch (SecurityException e) {
2967 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002968 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002970 "or use the exported attribute for this activity. "
2971 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002973 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002975
Michael Jurka86a720e2012-05-09 11:23:23 -07002976 boolean startActivitySafely(View v, Intent intent, Object tag) {
2977 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002978 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2979 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2980 return false;
2981 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002982 try {
2983 success = startActivity(v, intent, tag);
2984 } catch (ActivityNotFoundException e) {
2985 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2986 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2987 }
2988 return success;
2989 }
2990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 /**
2992 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2993 * in the DragLayer in the exact absolute location of the original FolderIcon.
2994 */
2995 private void copyFolderIconToImage(FolderIcon fi) {
2996 final int width = fi.getMeasuredWidth();
2997 final int height = fi.getMeasuredHeight();
2998
2999 // Lazy load ImageView, Bitmap and Canvas
3000 if (mFolderIconImageView == null) {
3001 mFolderIconImageView = new ImageView(this);
3002 }
3003 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3004 mFolderIconBitmap.getHeight() != height) {
3005 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3006 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3007 }
3008
3009 DragLayer.LayoutParams lp;
3010 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3011 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3012 } else {
3013 lp = new DragLayer.LayoutParams(width, height);
3014 }
3015
Adam Cohen307fe232012-08-16 17:55:58 -07003016 // The layout from which the folder is being opened may be scaled, adjust the starting
3017 // view size by this scale factor.
3018 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003019 lp.customPosition = true;
3020 lp.x = mRectForFolderAnimation.left;
3021 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003022 lp.width = (int) (scale * width);
3023 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003024
3025 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3026 fi.draw(mFolderIconCanvas);
3027 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003028 if (fi.getFolder() != null) {
3029 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3030 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003031 }
Adam Cohen268c4752012-06-06 17:47:33 -07003032 // Just in case this image view is still in the drag layer from a previous animation,
3033 // we remove it and re-add it.
3034 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3035 mDragLayer.removeView(mFolderIconImageView);
3036 }
3037 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003038 if (fi.getFolder() != null) {
3039 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003040 }
Adam Cohen268c4752012-06-06 17:47:33 -07003041 }
3042
Adam Cohen2801caf2011-05-13 20:57:39 -07003043 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003044 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003045 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3046 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3047 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3048
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 FolderInfo info = (FolderInfo) fi.getTag();
3050 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3051 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003052 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3053 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003054 }
3055
Adam Cohen268c4752012-06-06 17:47:33 -07003056 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3057 copyFolderIconToImage(fi);
3058 fi.setVisibility(View.INVISIBLE);
3059
Michael Jurka2ecf9952012-06-18 12:52:28 -07003060 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003061 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003062 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003063 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3064 }
3065 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003066 oa.start();
3067 }
3068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003070 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003071 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3072 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3073 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3074
Adam Cohen268c4752012-06-06 17:47:33 -07003075 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003076
Adam Cohen268c4752012-06-06 17:47:33 -07003077 // We remove and re-draw the FolderIcon in-case it has changed
3078 mDragLayer.removeView(mFolderIconImageView);
3079 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003080 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003081 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003082 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003083 oa.addListener(new AnimatorListenerAdapter() {
3084 @Override
3085 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003086 if (cl != null) {
3087 cl.clearFolderLeaveBehind();
3088 // Remove the ImageView copy of the FolderIcon and make the original visible.
3089 mDragLayer.removeView(mFolderIconImageView);
3090 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 }
3092 }
3093 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003094 oa.start();
3095 }
3096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003098 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 * is animated relative to the specified View. If the View is null, no animation
3100 * is played.
3101 *
3102 * @param folderInfo The FolderInfo describing the folder to open.
3103 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003104 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003105 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003106 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3107 if (openFolder != null && openFolder != folder) {
3108 // Close any open folder before opening a folder.
3109 closeFolder();
3110 }
3111
Adam Cohena9cf38f2011-05-02 15:36:58 -07003112 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113
Adam Cohena9cf38f2011-05-02 15:36:58 -07003114 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115
Sunny Goyalf4066152015-04-15 09:42:19 -07003116 // While the folder is open, the position of the icon cannot change.
3117 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3118
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 // Just verify that the folder hasn't already been added to the DragLayer.
3120 // There was a one-off crash where the folder had a parent already.
3121 if (folder.getParent() == null) {
3122 mDragLayer.addView(folder);
3123 mDragController.addDropTarget((DropTarget) folder);
3124 } else {
3125 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3126 folder.getParent() + ").");
3127 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003128 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003129 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003130
3131 // Notify the accessibility manager that this folder "window" has appeared and occluded
3132 // the workspace items
3133 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3134 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003135 }
3136
3137 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003138 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003139 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003140 if (folder.isEditingName()) {
3141 folder.dismissEditingName();
3142 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003143 closeFolder(folder);
3144 }
3145 }
3146
3147 void closeFolder(Folder folder) {
3148 folder.getInfo().opened = false;
3149
3150 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3151 if (parent != null) {
3152 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3153 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003154 if (fi != null) {
3155 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3156 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003157 }
3158 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003159
3160 // Notify the accessibility manager that this folder "window" has disappeard and no
3161 // longer occludeds the workspace items
3162 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003163 }
3164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003166 if (!isDraggingEnabled()) return false;
3167 if (isWorkspaceLocked()) return false;
3168 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169
Adam Cohen1697b792013-09-17 19:08:21 -07003170 if (v instanceof Workspace) {
3171 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003172
3173 if (!mWorkspace.isTouchActive()) {
3174 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003175 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3176 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3177 return true;
3178 } else {
3179 return false;
3180 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003181 } else {
3182 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003183 }
Adam Cohen1697b792013-09-17 19:08:21 -07003184 }
3185
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003186 CellLayout.CellInfo longClickCellInfo = null;
3187 View itemUnderLongClick = null;
3188 if (v.getTag() instanceof ItemInfo) {
3189 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003190 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003191 itemUnderLongClick = longClickCellInfo.cell;
3192 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 }
3194
Winson Chung3d503fb2011-07-13 17:25:49 -07003195 // The hotseat touch handling does not go through Workspace, and we always allow long press
3196 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003197 final boolean inHotseat = isHotseatLayout(v);
3198 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003199 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003200 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003201 // User long pressed on empty space
3202 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3203 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003204 if (mWorkspace.isInOverviewMode()) {
3205 mWorkspace.startReordering(v);
3206 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003207 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003210 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3211 mHotseat.getOrderInHotseat(
3212 longClickCellInfo.cellX,
3213 longClickCellInfo.cellY));
3214 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003216 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 }
3218 }
3219 }
3220 return true;
3221 }
3222
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 boolean isHotseatLayout(View layout) {
3224 return mHotseat != null && layout != null &&
3225 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3226 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003227
Winson Chung3d503fb2011-07-13 17:25:49 -07003228 /**
3229 * Returns the CellLayout of the specified container at the specified screen.
3230 */
Sunny Goyal92820592015-03-02 11:31:35 -08003231 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003232 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3233 if (mHotseat != null) {
3234 return mHotseat.getLayout();
3235 } else {
3236 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003237 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003238 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003239 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003240 }
3241 }
3242
Winson Chungb745afb2015-03-02 11:51:23 -08003243 /**
3244 * For overridden classes.
3245 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003246 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003247 return isAppsViewVisible();
3248 }
3249
3250 public boolean isAppsViewVisible() {
3251 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3252 }
3253
3254 public boolean isWidgetsViewVisible() {
3255 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003256 }
3257
Craig Mautner360310b2012-10-26 15:13:08 -07003258 private void setWorkspaceBackground(boolean workspace) {
3259 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003260 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003261 }
3262
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003263 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003264 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3265 int curflags = getWindow().getAttributes().flags
3266 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3267 if (wpflags != curflags) {
3268 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3269 }
Craig Mautner360310b2012-10-26 15:13:08 -07003270 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003271 }
3272
Michael Jurkae326f182011-11-21 14:05:46 -08003273 @Override
3274 public void onTrimMemory(int level) {
3275 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003276 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3277 // The widget preview db can result in holding onto over
3278 // 3MB of memory for caching which isn't necessary.
3279 SQLiteDatabase.releaseMemory();
3280
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003281 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003282 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003283 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003284 if (mLauncherCallbacks != null) {
3285 mLauncherCallbacks.onTrimMemory(level);
3286 }
Michael Jurkae326f182011-11-21 14:05:46 -08003287 }
3288
Winson Chungb745afb2015-03-02 11:51:23 -08003289 @Override
3290 public void onStateTransitionHideSearchBar() {
3291 // Hide the search bar
3292 if (mSearchDropTargetBar != null) {
3293 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3294 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003295 }
3296
Winson Chungb745afb2015-03-02 11:51:23 -08003297 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003298 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3299 true);
Adam Cohened307df2013-10-02 09:37:31 -07003300 }
3301
Winson Chungdc61c4d2015-04-20 18:26:57 -07003302 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3303 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3304 onCompleteRunnable, true);
3305 }
3306
3307 protected void showWorkspace(int snapToPage, boolean animated) {
3308 showWorkspace(snapToPage, animated, null, true);
3309 }
3310
3311 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3312 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003313 boolean changed = mState != State.WORKSPACE ||
3314 mWorkspace.getState() != Workspace.State.NORMAL;
3315 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003316 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003318 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003319 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320
Winson Chungc7d2b602012-05-16 17:02:20 -07003321 // Show the search bar (only animate if we were showing the drop target bar in spring
3322 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003323 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003324 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003325 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003326
Michael Jurkab3e22d92011-10-31 15:58:33 -07003327 // Set focus to the AppsCustomize button
3328 if (mAllAppsButton != null) {
3329 mAllAppsButton.requestFocus();
3330 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003331 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003332
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003333 // Change the state *after* we've called all the transition code
3334 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003335
Winson Chung5fb63472011-02-02 17:03:37 -08003336 // Resume the auto-advance of widgets
3337 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003338 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003339
Winson Chung0f785722015-04-08 10:27:49 -07003340 if (changed) {
3341 // Send an accessibility event to announce the context change
3342 getWindow().getDecorView()
3343 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003344
Winson Chung0f785722015-04-08 10:27:49 -07003345 onWorkspaceShown(animated);
3346 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003347 }
3348
Adam Cohened307df2013-10-02 09:37:31 -07003349 void showOverviewMode(boolean animated) {
3350 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003351 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003352 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3353 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003354 mState = State.WORKSPACE;
3355 onWorkspaceShown(animated);
3356 }
3357
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003358 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003359 }
3360
Winson Chungb745afb2015-03-02 11:51:23 -08003361 /**
3362 * Shows the apps view.
3363 */
3364 void showAppsView(boolean animated, boolean resetListToTop) {
3365 if (resetListToTop) {
3366 mAppsView.scrollToTop();
3367 }
3368 showAppsOrWidgets(animated, State.APPS);
3369 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370
Winson Chungb745afb2015-03-02 11:51:23 -08003371 /**
3372 * Shows the widgets view.
3373 */
3374 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003376 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003377 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003378 }
Winson Chungb745afb2015-03-02 11:51:23 -08003379 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003380
3381 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003382 @Override
3383 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003384 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003385 }
3386 });
Winson Chungb745afb2015-03-02 11:51:23 -08003387 }
3388
3389 /**
3390 * Sets up the transition to show the apps/widgets view.
3391 */
3392 private void showAppsOrWidgets(boolean animated, State toState) {
3393 if (mState != State.WORKSPACE) return;
3394 if (toState != State.APPS && toState != State.WIDGETS) return;
3395
3396 if (toState == State.APPS) {
3397 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003398 if (mLauncherCallbacks != null) {
3399 mLauncherCallbacks.onAllAppsShown();
3400 }
Winson Chungb745afb2015-03-02 11:51:23 -08003401 } else {
3402 mStateTransitionAnimation.startAnimationToWidgets(animated);
3403 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003406 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407
3408 // Pause the auto-advance of widgets until we are out of AllApps
3409 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003410 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411 closeFolder();
3412
3413 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003414 getWindow().getDecorView()
3415 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416 }
3417
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418 public void enterSpringLoadedDragMode() {
3419 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3420 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003421 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3422 mState == State.WIDGETS_SPRING_LOADED) {
3423 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003424 }
Winson Chungb745afb2015-03-02 11:51:23 -08003425
3426 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003427 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3428 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003429 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003430 }
Adam Cohen7777d962011-08-18 18:58:38 -07003431
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003433 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003434 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003435
Winson Chunge7a03942011-08-05 15:05:12 -07003436 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003437 @Override
3438 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003439 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003440 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3441 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003442 // Before we show workspace, hide all apps again because
3443 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3444 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003445 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003446 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003447 } else {
3448 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003449 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003450 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003451 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003452 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003453
Michael Jurkad3ef3062010-11-23 16:23:58 -08003454 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003455 if (mState == State.APPS_SPRING_LOADED) {
3456 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3457 mState = State.APPS;
3458 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3459 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3460 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003461 }
3462 // Otherwise, we are not in spring loaded mode, so don't do anything.
3463 }
3464
Michael Jurkab3e22d92011-10-31 15:58:33 -07003465 void lockAllApps() {
3466 // TODO
3467 }
3468
3469 void unlockAllApps() {
3470 // TODO
3471 }
3472
Sunny Goyal594d76d2014-11-06 10:12:54 -08003473 protected void disableVoiceButtonProxy(boolean disable) {
3474 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003475 }
3476
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003477 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003478 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3479 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003480 }
3481
Adam Cohen24ce0b32014-01-14 16:18:14 -08003482 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003483 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3484 if (searchProvider == null) {
3485 return null;
3486 }
3487
3488 Bundle opts = new Bundle();
3489 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003490 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003491
3492 SharedPreferences sp = getSharedPreferences(
3493 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3494 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003495 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003496 if (!searchProvider.provider.flattenToString().equals(
3497 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003498 || (widgetInfo == null)
3499 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003500 // A valid widget is not already bound.
3501 if (widgetId > -1) {
3502 mAppWidgetHost.deleteAppWidgetId(widgetId);
3503 widgetId = -1;
3504 }
3505
3506 // Try to bind a new widget
3507 widgetId = mAppWidgetHost.allocateAppWidgetId();
3508
3509 if (!AppWidgetManagerCompat.getInstance(this)
3510 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3511 mAppWidgetHost.deleteAppWidgetId(widgetId);
3512 widgetId = -1;
3513 }
3514
3515 sp.edit()
3516 .putInt(QSB_WIDGET_ID, widgetId)
3517 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3518 .commit();
3519 }
3520
3521 if (widgetId != -1) {
3522 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3523 mQsb.updateAppWidgetOptions(opts);
3524 mQsb.setPadding(0, 0, 0, 0);
3525 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003526 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003527 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003528 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003529 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003530 }
3531
Sunny Goyal22235bc2015-04-03 09:23:43 -07003532 private void reinflateQSBIfNecessary() {
3533 if (mQsb instanceof LauncherAppWidgetHostView &&
3534 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3535 mSearchDropTargetBar.removeView(mQsb);
3536 mQsb = null;
3537 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3538 }
3539 }
3540
Michael Jurkad7c28052012-04-27 15:43:36 -07003541 @Override
3542 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003543 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003544 final List<CharSequence> text = event.getText();
3545 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003546 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003547 if (mState == State.APPS) {
3548 text.add("Apps");
3549 } else if (mState == State.WIDGETS) {
3550 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003551 } else {
3552 text.add(getString(R.string.all_apps_home_button_label));
3553 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003554 return result;
3555 }
3556
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003557 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003558 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003559 */
Adam Cohen091440a2015-03-18 14:16:05 -07003560 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003561 @Override
3562 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003563 closeSystemDialogs();
3564 }
3565 }
3566
3567 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003568 * Receives notifications whenever the appwidgets are reset.
3569 */
3570 private class AppWidgetResetObserver extends ContentObserver {
3571 public AppWidgetResetObserver() {
3572 super(new Handler());
3573 }
3574
3575 @Override
3576 public void onChange(boolean selfChange) {
3577 onAppWidgetReset();
3578 }
3579 }
3580
3581 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003582 * If the activity is currently paused, signal that we need to run the passed Runnable
3583 * in onResume.
3584 *
3585 * This needs to be called from incoming places where resources might have been loaded
3586 * while we are paused. That is becaues the Configuration might be wrong
3587 * when we're not running, and if it comes back to what it was when we
3588 * were paused, we are not restarted.
3589 *
3590 * Implementation of the method from LauncherModel.Callbacks.
3591 *
3592 * @return true if we are currently paused. The caller might be able to
3593 * skip some work in that case since we will come back again.
3594 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003595 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 if (mPaused) {
3597 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003598 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003599 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003600 }
3601 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003602 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003603 return true;
3604 } else {
3605 return false;
3606 }
3607 }
3608
Michael Jurkac402cd92013-05-20 15:49:32 +02003609 private boolean waitUntilResume(Runnable run) {
3610 return waitUntilResume(run, false);
3611 }
3612
Michael Jurka1e2f4652013-07-08 18:03:46 -07003613 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003614 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003615 }
3616
Michael Jurka7607c2f2013-04-03 14:33:19 -07003617 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003618 * If the activity is currently paused, signal that we need to re-run the loader
3619 * in onResume.
3620 *
3621 * This needs to be called from incoming places where resources might have been loaded
3622 * while we are paused. That is becaues the Configuration might be wrong
3623 * when we're not running, and if it comes back to what it was when we
3624 * were paused, we are not restarted.
3625 *
3626 * Implementation of the method from LauncherModel.Callbacks.
3627 *
3628 * @return true if we are currently paused. The caller might be able to
3629 * skip some work in that case since we will come back again.
3630 */
3631 public boolean setLoadOnResume() {
3632 if (mPaused) {
3633 Log.i(TAG, "setLoadOnResume");
3634 mOnResumeNeedsLoad = true;
3635 return true;
3636 } else {
3637 return false;
3638 }
3639 }
3640
3641 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003642 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003643 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003645 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003646 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003647 } else {
3648 return SCREEN_COUNT / 2;
3649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003651
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 /**
3653 * Refreshes the shortcuts shown on the workspace.
3654 *
3655 * Implementation of the method from LauncherModel.Callbacks.
3656 */
3657 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003658 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003659
Michael Jurka7607c2f2013-04-03 14:33:19 -07003660 // If we're starting binding all over again, clear any bind calls we'd postponed in
3661 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3662 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003663 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003664
Winson Chungd64d1762013-08-20 14:37:16 -07003665 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003666 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003667 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003668
Michael Jurka05bf6442011-11-30 20:28:53 -08003669 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003670 if (mHotseat != null) {
3671 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003672 }
3673 }
3674
Adam Cohendcd297f2013-06-18 13:13:40 -07003675 @Override
3676 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003677 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003678
Adam Cohen5084cba2013-09-03 12:01:16 -07003679 // If there are no screens, we need to have an empty screen
3680 if (orderedScreenIds.size() == 0) {
3681 mWorkspace.addExtraEmptyScreen();
3682 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003683
3684 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003685 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003686 if (hasCustomContentToLeft()) {
3687 mWorkspace.createCustomContentContainer();
3688 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003689 }
Winson Chung64359a52013-07-08 17:17:08 -07003690 }
3691
3692 @Override
3693 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003694 // Log to disk
3695 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3696 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3697 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003698 int count = orderedScreenIds.size();
3699 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003700 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003701 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003702 }
3703
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003704 @Override
3705 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3706 final long screenId, final int[] cell, final int spanX, final int spanY) {
3707 showWorkspace(true, new Runnable() {
3708
3709 @Override
3710 public void run() {
3711 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3712 addPendingItem(info, container, screenId, cell, spanX, spanY);
3713 }
3714 });
3715 }
3716
Adam Cohen39a06042013-07-19 14:30:12 -07003717 private boolean shouldShowWeightWatcher() {
3718 String spKey = LauncherAppState.getSharedPreferencesKey();
3719 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003720 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003721
3722 return show;
3723 }
3724
3725 private void toggleShowWeightWatcher() {
3726 String spKey = LauncherAppState.getSharedPreferencesKey();
3727 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3728 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3729
3730 show = !show;
3731
3732 SharedPreferences.Editor editor = sp.edit();
3733 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3734 editor.commit();
3735
3736 if (mWeightWatcher != null) {
3737 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3738 }
3739 }
3740
Winson Chungd64d1762013-08-20 14:37:16 -07003741 public void bindAppsAdded(final ArrayList<Long> newScreens,
3742 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003743 final ArrayList<ItemInfo> addAnimated,
3744 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003745 Runnable r = new Runnable() {
3746 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003747 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003748 }
3749 };
3750 if (waitUntilResume(r)) {
3751 return;
3752 }
3753
Winson Chungd64d1762013-08-20 14:37:16 -07003754 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003755 if (newScreens != null) {
3756 bindAddScreens(newScreens);
3757 }
Winson Chungd64d1762013-08-20 14:37:16 -07003758
3759 // We add the items without animation on non-visible pages, and with
3760 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003761 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003762 bindItems(addNotAnimated, 0,
3763 addNotAnimated.size(), false);
3764 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003765 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003766 bindItems(addAnimated, 0,
3767 addAnimated.size(), true);
3768 }
Winson Chungc58497e2013-09-03 17:48:37 -07003769
Winson Chung87412982013-10-03 18:34:14 -07003770 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003771 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003772
Winson Chungb745afb2015-03-02 11:51:23 -08003773 if (addedApps != null && mAppsView != null) {
3774 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003775 }
Winson Chungd64d1762013-08-20 14:37:16 -07003776 }
3777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003778 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 * Bind the items start-end from the list.
3780 *
3781 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003782 */
Winson Chung64359a52013-07-08 17:17:08 -07003783 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3784 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003785 Runnable r = new Runnable() {
3786 public void run() {
3787 bindItems(shortcuts, start, end, forceAnimateIcons);
3788 }
3789 };
3790 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003791 return;
3792 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003793
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003795 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3796 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003797 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003798 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003799 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003800 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003802
3803 // Short circuit if we are loading dock items for a configuration which has no dock
3804 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3805 mHotseat == null) {
3806 continue;
3807 }
3808
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 switch (item.itemType) {
3810 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3811 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003812 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003813 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003814
Winson Chung64359a52013-07-08 17:17:08 -07003815 /*
3816 * TODO: FIX collision case
3817 */
Winson Chungce376632013-07-11 16:12:41 -07003818 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3819 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3820 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003821 View v = cl.getChildAt(item.cellX, item.cellY);
3822 Object tag = v.getTag();
3823 String desc = "Collision while binding workspace item: " + item
3824 + ". Collides with " + tag;
3825 if (LauncherAppState.isDogfoodBuild()) {
3826 throw (new RuntimeException(desc));
3827 } else {
3828 Log.d(TAG, desc);
3829 }
Winson Chungce376632013-07-11 16:12:41 -07003830 }
Winson Chung64359a52013-07-08 17:17:08 -07003831 }
3832
Adam Cohendcd297f2013-06-18 13:13:40 -07003833 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3834 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003835 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003836 // Animate all the applications up now
3837 shortcut.setAlpha(0f);
3838 shortcut.setScaleX(0f);
3839 shortcut.setScaleY(0f);
3840 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003841 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003844 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003845 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003846 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003847 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003848 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3849 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003851 default:
3852 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855
Winson Chung997a9232013-07-24 15:33:46 -07003856 if (animateIcons) {
3857 // Animate to the correct page
3858 if (newShortcutsScreenId > -1) {
3859 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003860 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003861 final Runnable startBounceAnimRunnable = new Runnable() {
3862 public void run() {
3863 anim.playTogether(bounceAnims);
3864 anim.start();
3865 }
3866 };
Winson Chung997a9232013-07-24 15:33:46 -07003867 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003868 // We post the animation slightly delayed to prevent slowdowns
3869 // when we are loading right after we return to launcher.
3870 mWorkspace.postDelayed(new Runnable() {
3871 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003872 if (mWorkspace != null) {
3873 mWorkspace.snapToPage(newScreenIndex);
3874 mWorkspace.postDelayed(startBounceAnimRunnable,
3875 NEW_APPS_ANIMATION_DELAY);
3876 }
Winson Chung94d67682013-09-25 16:29:40 -07003877 }
3878 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003879 } else {
3880 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003881 }
3882 }
Winson Chung64359a52013-07-08 17:17:08 -07003883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003885 }
3886
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 /**
3888 * Implementation of the method from LauncherModel.Callbacks.
3889 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003890 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003891 Runnable r = new Runnable() {
3892 public void run() {
3893 bindFolders(folders);
3894 }
3895 };
3896 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003897 return;
3898 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003899 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901
3902 /**
3903 * Add the views for a widget to the workspace.
3904 *
3905 * Implementation of the method from LauncherModel.Callbacks.
3906 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003907 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003908 Runnable r = new Runnable() {
3909 public void run() {
3910 bindAppWidget(item);
3911 }
3912 };
3913 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003914 return;
3915 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003916
Daniel Sandler843e8602010-06-07 14:59:01 -04003917 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3918 if (DEBUG_WIDGETS) {
3919 Log.d(TAG, "bindAppWidget: " + item);
3920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 final Workspace workspace = mWorkspace;
3922
Adam Cohen59400422014-03-05 18:07:04 -08003923 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003924 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003925
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003926 if (!mIsSafeModeEnabled
3927 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3928 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003929 if (appWidgetInfo == null) {
3930 if (DEBUG_WIDGETS) {
3931 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3932 + " belongs to component " + item.providerName
3933 + ", as the povider is null");
3934 }
3935 LauncherModel.deleteItemFromDatabase(this, item);
3936 return;
3937 }
3938 // Note: This assumes that the id remap broadcast is received before this step.
3939 // If that is not the case, the id remap will be ignored and user may see the
3940 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003941 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003942 pendingInfo.spanX = item.spanX;
3943 pendingInfo.spanY = item.spanY;
3944 pendingInfo.minSpanX = item.minSpanX;
3945 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003946 Bundle options = null;
3947 // AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003948
Sunny Goyalff572272014-07-23 13:58:07 -07003949 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003950 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3951 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003952
3953 // TODO consider showing a permission dialog when the widget is clicked.
3954 if (!success) {
3955 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3958 + " belongs to component " + item.providerName
3959 + ", as the launcher is unable to bing a new widget id");
3960 }
3961 LauncherModel.deleteItemFromDatabase(this, item);
3962 return;
3963 }
3964
3965 item.appWidgetId = newWidgetId;
3966
3967 // If the widget has a configure activity, it is still needs to set it up, otherwise
3968 // the widget is ready to go.
3969 item.restoreStatus = (appWidgetInfo.configure == null)
3970 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3971 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3972
3973 LauncherModel.updateItemInDatabase(this, item);
3974 }
3975
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003976 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003977 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003978 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003979 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3980 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003981 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003982
Sunny Goyal651077b2014-06-30 14:15:31 -07003983 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3984 } else {
3985 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003986 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3987 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003988 view.updateIcon(mIconCache);
3989 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003990 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003991 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003995 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Adam Cohendcd297f2013-06-18 13:13:40 -07003997 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003999 if (!item.isCustomWidget()) {
4000 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4001 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004002
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 workspace.requestLayout();
4004
Daniel Sandler843e8602010-06-07 14:59:01 -04004005 if (DEBUG_WIDGETS) {
4006 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4007 + (SystemClock.uptimeMillis()-start) + "ms");
4008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 }
4010
Sunny Goyalff572272014-07-23 13:58:07 -07004011 /**
4012 * Restores a pending widget.
4013 *
4014 * @param appWidgetId The app widget id
4015 * @param cellInfo The position on screen where to create the widget.
4016 */
4017 private void completeRestoreAppWidget(final int appWidgetId) {
4018 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4019 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4020 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4021 return;
4022 }
4023
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004024 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004025 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4026
4027 mWorkspace.reinflateWidgetsIfNecessary();
4028 LauncherModel.updateItemInDatabase(this, info);
4029 }
4030
Adam Cohen1462de32012-07-24 22:34:36 -07004031 public void onPageBoundSynchronously(int page) {
4032 mSynchronouslyBoundPages.add(page);
4033 }
4034
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 /**
4036 * Callback saying that there aren't any more items to bind.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004040 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004041 Runnable r = new Runnable() {
4042 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004043 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004044 }
4045 };
4046 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004047 return;
4048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 if (mSavedState != null) {
4050 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004051 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 mSavedState = null;
4054 }
4055
Adam Cohen1462de32012-07-24 22:34:36 -07004056 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004058 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004059 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004060
4061 // If we received the result of any pending adds while the loader was running (e.g. the
4062 // widget configuration forced an orientation change), process them now.
4063 if (sPendingAddItem != null) {
4064 final long screenId = completeAdd(sPendingAddItem);
4065
4066 // TODO: this moves the user to the page where the pending item was added. Ideally,
4067 // the screen would be guaranteed to exist after bind, and the page would be set through
4068 // the workspace restore process.
4069 mWorkspace.post(new Runnable() {
4070 @Override
4071 public void run() {
4072 mWorkspace.snapToScreenId(screenId);
4073 }
4074 });
4075 sPendingAddItem = null;
4076 }
4077
Sunny Goyal756adbc2015-04-16 15:20:51 -07004078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004079
4080 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004081 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004082 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004083 }
4084
Adam Cohen3ed316a2014-07-23 18:21:20 -07004085 private void sendLoadingCompleteBroadcastIfNecessary() {
4086 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4087 String permission =
4088 getResources().getString(R.string.receive_first_load_broadcast_permission);
4089 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4090 sendBroadcast(intent, permission);
4091 SharedPreferences.Editor editor = mSharedPrefs.edit();
4092 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4093 editor.apply();
4094 }
4095 }
4096
Winson Chunga0b7e862013-09-05 16:03:15 -07004097 public boolean isAllAppsButtonRank(int rank) {
4098 if (mHotseat != null) {
4099 return mHotseat.isAllAppsButtonRank(rank);
4100 }
4101 return false;
4102 }
4103
Winson Chunga2413752012-04-03 14:22:34 -07004104 private boolean canRunNewAppsAnimation() {
4105 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4106 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4107 }
4108
Winson Chungc9168342013-06-26 14:54:55 -07004109 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4110 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4111 PropertyValuesHolder.ofFloat("alpha", 1f),
4112 PropertyValuesHolder.ofFloat("scaleX", 1f),
4113 PropertyValuesHolder.ofFloat("scaleY", 1f));
4114 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4115 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4116 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4117 return bounceAnim;
4118 }
4119
Adam Cohen27772732013-11-11 14:00:56 +00004120 public boolean useVerticalBarLayout() {
4121 return LauncherAppState.getInstance().getDynamicGrid().
4122 getDeviceProfile().isVerticalBarLayout();
4123 }
4124
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004125 protected Rect getSearchBarBounds() {
4126 return LauncherAppState.getInstance().getDynamicGrid().
4127 getDeviceProfile().getSearchBarBounds();
4128 }
4129
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004130 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004131 if (mSearchDropTargetBar == null) {
4132 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004133 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004134 if (mQsb != null) {
4135 mSearchDropTargetBar.removeView(mQsb);
4136 mQsb = null;
4137 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004138 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004139 }
4140
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004142 * Add the icons for all apps.
4143 *
4144 * Implementation of the method from LauncherModel.Callbacks.
4145 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004146 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004147 if (mAppsView != null) {
4148 mAppsView.setApps(apps);
4149 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004150 if (mWidgetsView != null) {
4151 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4152 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004153 }
Adam Cohen9211d422014-10-07 18:14:53 -07004154 if (mLauncherCallbacks != null) {
4155 mLauncherCallbacks.bindAllApplications(apps);
4156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 }
4158
4159 /**
4160 * A package was updated.
4161 *
4162 * Implementation of the method from LauncherModel.Callbacks.
4163 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004164 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004165 Runnable r = new Runnable() {
4166 public void run() {
4167 bindAppsUpdated(apps);
4168 }
4169 };
4170 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004171 return;
4172 }
4173
Winson Chungb745afb2015-03-02 11:51:23 -08004174 if (mAppsView != null) {
4175 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004176 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004177 }
4178
Sunny Goyal4390ace2014-10-13 11:33:11 -07004179 @Override
4180 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4181 Runnable r = new Runnable() {
4182 public void run() {
4183 bindWidgetsRestored(widgets);
4184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
4188 }
4189 mWorkspace.widgetsRestored(widgets);
4190 }
4191
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004193 * Some shortcuts were updated in the background.
4194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004197 @Override
4198 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4199 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004200 Runnable r = new Runnable() {
4201 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004202 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004203 }
4204 };
4205 if (waitUntilResume(r)) {
4206 return;
4207 }
4208
Sunny Goyal4390ace2014-10-13 11:33:11 -07004209 if (!updated.isEmpty()) {
4210 mWorkspace.updateShortcuts(updated);
4211 }
4212
4213 if (!removed.isEmpty()) {
4214 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4215 for (ShortcutInfo si : removed) {
4216 removedComponents.add(si.getTargetComponent());
4217 }
4218 mWorkspace.removeItemsByComponentName(removedComponents, user);
4219 // Notify the drag controller
4220 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004221 }
4222 }
4223
4224 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004225 * Update the state of a package, typically related to install state.
4226 *
4227 * Implementation of the method from LauncherModel.Callbacks.
4228 */
Sunny Goyale755d462014-07-22 13:48:29 -07004229 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004230 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4231 Runnable r = new Runnable() {
4232 public void run() {
4233 bindRestoreItemsChange(updates);
4234 }
4235 };
4236 if (waitUntilResume(r)) {
4237 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004238 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004239
Sunny Goyal756adbc2015-04-16 15:20:51 -07004240 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004241 }
4242
4243 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004244 * A package was uninstalled. We take both the super set of packageNames
4245 * in addition to specific applications to remove, the reason being that
4246 * this can be called when a package is updated as well. In that scenario,
4247 * we only remove specific components from the workspace, where as
4248 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004250 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004251 * Implementation of the method from LauncherModel.Callbacks.
4252 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004253 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004254 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004256 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004257 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004258 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004259 }
Winson Chungd64d1762013-08-20 14:37:16 -07004260 };
4261 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004262 return;
4263 }
4264
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4267 for (AppInfo info : appInfos) {
4268 removedComponents.add(info.componentName);
4269 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004270 if (!packageNames.isEmpty()) {
4271 mWorkspace.removeItemsByPackageName(packageNames, user);
4272 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004273 if (!removedComponents.isEmpty()) {
4274 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004275 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 // Notify the drag controller
4277 mDragController.onAppsRemoved(packageNames, removedComponents);
4278
Sunny Goyal1a745e82014-10-02 15:58:31 -07004279 } else {
4280 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004281 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004282
Winson Chungdf95eb12013-10-16 14:57:07 -07004283 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004284 if (mAppsView != null) {
4285 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 }
Joe Onoratobe386092009-11-17 17:32:16 -08004288
4289 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004290 * A number of packages were updated.
4291 */
Adam Cohen091440a2015-03-18 14:16:05 -07004292 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004293 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004294 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004295 bindPackagesUpdated(mWidgetsAndShortcuts);
4296 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004297 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004298 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004299
Michael Jurkac402cd92013-05-20 15:49:32 +02004300 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4301 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4302 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 return;
4304 }
4305
Hyunyoung Song3f471442015-04-08 19:01:34 -07004306 if (mWidgetsView != null) {
4307 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4308 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004309 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004310 }
4311
Winson Chung400438b2011-01-16 17:53:48 -08004312 private int mapConfigurationOriActivityInfoOri(int configOri) {
4313 final Display d = getWindowManager().getDefaultDisplay();
4314 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4315 switch (d.getRotation()) {
4316 case Surface.ROTATION_0:
4317 case Surface.ROTATION_180:
4318 // We are currently in the same basic orientation as the natural orientation
4319 naturalOri = configOri;
4320 break;
4321 case Surface.ROTATION_90:
4322 case Surface.ROTATION_270:
4323 // We are currently in the other basic orientation to the natural orientation
4324 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4325 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4326 break;
4327 }
4328
4329 int[] oriMap = {
4330 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4331 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4332 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4333 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4334 };
4335 // Since the map starts at portrait, we need to offset if this device's natural orientation
4336 // is landscape.
4337 int indexOffset = 0;
4338 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4339 indexOffset = 1;
4340 }
4341 return oriMap[(d.getRotation() + indexOffset) % 4];
4342 }
Adam Cohen446e9402011-09-15 18:21:21 -07004343
Winson Chung4b919f82012-05-01 10:44:08 -07004344 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004345 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004346 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4347 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4348 .getConfiguration().orientation));
4349 } else {
4350 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4351 }
Winson Chung4b919f82012-05-01 10:44:08 -07004352 }
4353 }
4354 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004355 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004356 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004357 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004358 } else {
4359 mHandler.postDelayed(new Runnable() {
4360 public void run() {
4361 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4362 }
4363 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004364 }
Winson Chung4b919f82012-05-01 10:44:08 -07004365 }
Winson Chung400438b2011-01-16 17:53:48 -08004366 }
4367
Winson Chunge43a1e72014-01-15 10:33:02 -08004368 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004369 if (mLauncherCallbacks != null) {
4370 return mLauncherCallbacks.isLauncherPreinstalled();
4371 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004372 PackageManager pm = getPackageManager();
4373 try {
4374 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4375 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4376 return true;
4377 } else {
4378 return false;
4379 }
4380 } catch (NameNotFoundException e) {
4381 e.printStackTrace();
4382 return false;
4383 }
4384 }
4385
Adam Cohenea90f832014-05-21 15:03:34 -07004386 /**
4387 * This method indicates whether or not we should suggest default wallpaper dimensions
4388 * when our wallpaper cropper was not yet used to set a wallpaper.
4389 */
4390 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004391 if (mLauncherCallbacks != null) {
4392 return mLauncherCallbacks.overrideWallpaperDimensions();
4393 }
Adam Cohenea90f832014-05-21 15:03:34 -07004394 return true;
4395 }
4396
Adam Cohen432609a2014-03-13 17:03:22 -07004397 /**
4398 * To be overridden by subclasses to indicate that there is an activity to launch
4399 * before showing the standard launcher experience.
4400 */
4401 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004402 if (mLauncherCallbacks != null) {
4403 return mLauncherCallbacks.hasFirstRunActivity();
4404 }
Adam Cohen432609a2014-03-13 17:03:22 -07004405 return false;
4406 }
4407
4408 /**
4409 * To be overridden by subclasses to launch any first run activity
4410 */
4411 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004412 if (mLauncherCallbacks != null) {
4413 return mLauncherCallbacks.getFirstRunActivity();
4414 }
Adam Cohen432609a2014-03-13 17:03:22 -07004415 return null;
4416 }
4417
Winson Chung2826a402015-04-17 16:18:53 -07004418 /**
4419 * Returns whether the launcher callbacks overrides search in all apps.
4420 * @return
4421 */
4422 @Thunk boolean isAllAppsSearchOverridden() {
4423 if (mLauncherCallbacks != null) {
4424 return mLauncherCallbacks.overrideAllAppsSearch();
4425 }
4426 return false;
4427 }
4428
Winson Chunga6945242014-01-08 14:04:34 -08004429 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004430 return !ActivityManager.isRunningInTestHarness() &&
4431 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004432 }
4433
Adam Cohen4a9423d2014-03-28 14:22:31 -07004434 protected boolean hasRunFirstRunActivity() {
4435 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4436 }
4437
Adam Cohen432609a2014-03-13 17:03:22 -07004438 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004439 if (shouldRunFirstRunActivity() &&
4440 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004441 Intent firstRunIntent = getFirstRunActivity();
4442 if (firstRunIntent != null) {
4443 startActivity(firstRunIntent);
4444 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004445 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004446 }
4447 }
Adam Cohen432609a2014-03-13 17:03:22 -07004448 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004449 }
4450
4451 private void markFirstRunActivityShown() {
4452 SharedPreferences.Editor editor = mSharedPrefs.edit();
4453 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4454 editor.apply();
4455 }
4456
Adam Cohen432609a2014-03-13 17:03:22 -07004457 /**
4458 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4459 * screen that must be displayed and dismissed.
4460 */
4461 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004462 if (mLauncherCallbacks != null) {
4463 return mLauncherCallbacks.hasDismissableIntroScreen();
4464 }
Adam Cohen432609a2014-03-13 17:03:22 -07004465 return false;
4466 }
4467
4468 /**
4469 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4470 */
4471 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004472 if (mLauncherCallbacks != null) {
4473 return mLauncherCallbacks.getIntroScreen();
4474 }
Adam Cohen432609a2014-03-13 17:03:22 -07004475 return null;
4476 }
4477
4478 /**
4479 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4480 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4481 */
4482 private boolean shouldShowIntroScreen() {
4483 return hasDismissableIntroScreen() &&
4484 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4485 }
4486
4487 protected void showIntroScreen() {
4488 View introScreen = getIntroScreen();
4489 changeWallpaperVisiblity(false);
4490 if (introScreen != null) {
4491 mDragLayer.showOverlayView(introScreen);
4492 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004493 if (mLauncherOverlayContainer != null) {
4494 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4495 }
Adam Cohen432609a2014-03-13 17:03:22 -07004496 }
4497
4498 public void dismissIntroScreen() {
4499 markIntroScreenDismissed();
4500 if (showFirstRunActivity()) {
4501 // We delay hiding the intro view until the first run activity is showing. This
4502 // avoids a blip.
4503 mWorkspace.postDelayed(new Runnable() {
4504 @Override
4505 public void run() {
4506 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004507 if (mLauncherOverlayContainer != null) {
4508 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4509 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004510 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512 }, ACTIVITY_START_DELAY);
4513 } else {
4514 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004515 if (mLauncherOverlayContainer != null) {
4516 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4517 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004518 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004519 }
4520 changeWallpaperVisiblity(true);
4521 }
4522
4523 private void markIntroScreenDismissed() {
4524 SharedPreferences.Editor editor = mSharedPrefs.edit();
4525 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4526 editor.apply();
4527 }
4528
Adam Cohen091440a2015-03-18 14:16:05 -07004529 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004530 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4531 // on the device, then we always show the first run cling experience (or if there is no
4532 // launcher2). Otherwise, we prompt the user upon started for migration
4533 LauncherClings launcherClings = new LauncherClings(this);
4534 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4535 if (mModel.canMigrateFromOldLauncherDb(this)) {
4536 launcherClings.showMigrationCling();
4537 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004538 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004539 }
4540 }
4541 }
4542
Winson Chunga6945242014-01-08 14:04:34 -08004543 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004544 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4545 if (mHotseat != null) mHotseat.setAlpha(1f);
4546 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4547 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004548 }
4549
4550 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004551 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4552 if (mHotseat != null) mHotseat.setAlpha(0f);
4553 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4554 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004555 }
4556
Mathew Inwood72fbec12013-11-19 15:45:07 +00004557 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004558 // Called from search suggestion, not supported in other profiles.
4559 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4560 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4561 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4562 myUser);
4563 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004564 return null;
4565 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004566 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004567 }
4568
4569 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4570 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004571 // Called from search suggestion, not supported in other profiles.
4572 return createShortcutDragInfo(shortcutIntent, caption, icon,
4573 UserHandleCompat.myUserHandle());
4574 }
4575
4576 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4577 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004578 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004579 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004580 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004581 }
4582
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004583 protected void moveWorkspaceToDefaultScreen() {
4584 mWorkspace.moveToDefaultScreen(false);
4585 }
4586
Mathew Inwood72fbec12013-11-19 15:45:07 +00004587 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4588 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004589 mWorkspace.onExternalDragStartedWithItem(dragView);
4590 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004591 }
4592
Anjali Koppalf5d29132014-02-28 13:33:27 -08004593 @Override
4594 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004595 if (mLauncherCallbacks != null) {
4596 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4597 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004598 }
4599
Winson Chung80baf5a2010-08-09 16:03:15 -07004600 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004601 * Prints out out state for debugging.
4602 */
4603 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004604 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004605 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004606 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4607 Log.d(TAG, "mRestoring=" + mRestoring);
4608 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4609 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004610 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004611 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004612 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004613
Daniel Sandler325dc232013-06-05 22:57:57 -04004614 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004615 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004616
4617 @Override
4618 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4619 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004620 synchronized (sDumpLogs) {
4621 writer.println(" ");
4622 writer.println("Debug logs: ");
4623 for (int i = 0; i < sDumpLogs.size(); i++) {
4624 writer.println(" " + sDumpLogs.get(i));
4625 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004626 }
Adam Cohen9211d422014-10-07 18:14:53 -07004627 if (mLauncherCallbacks != null) {
4628 mLauncherCallbacks.dump(prefix, fd, writer, args);
4629 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004630 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004631
4632 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004633 if (DEBUG_DUMP_LOG) {
4634 synchronized (sDumpLogs) {
4635 Log.d(TAG, "");
4636 Log.d(TAG, "*********************");
4637 Log.d(TAG, "Launcher debug logs: ");
4638 for (int i = 0; i < sDumpLogs.size(); i++) {
4639 Log.d(TAG, " " + sDumpLogs.get(i));
4640 }
4641 Log.d(TAG, "*********************");
4642 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004643 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004644 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004645 }
4646
4647 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004648 addDumpLog(tag, log, null, debugLog);
4649 }
4650
4651 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004652 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004653 if (e != null) {
4654 Log.d(tag, log, e);
4655 } else {
4656 Log.d(tag, log);
4657 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004658 }
Winson Chungede41292013-09-19 16:27:36 -07004659 if (DEBUG_DUMP_LOG) {
4660 sDateStamp.setTime(System.currentTimeMillis());
4661 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004662 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4663 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004664 }
Winson Chungede41292013-09-19 16:27:36 -07004665 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 }
4667
Adam Cohen59400422014-03-05 18:07:04 -08004668 public static CustomAppWidget getCustomAppWidget(String name) {
4669 return sCustomAppWidgets.get(name);
4670 }
4671
4672 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4673 return sCustomAppWidgets;
4674 }
4675
Winson Chungede41292013-09-19 16:27:36 -07004676 public void dumpLogsToLocalData() {
4677 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004678 new AsyncTask<Void, Void, Void>() {
4679 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004680 boolean success = false;
4681 sDateStamp.setTime(sRunStart);
4682 String FILENAME = sDateStamp.getMonth() + "-"
4683 + sDateStamp.getDay() + "_"
4684 + sDateStamp.getHours() + "-"
4685 + sDateStamp.getMinutes() + "_"
4686 + sDateStamp.getSeconds() + ".txt";
4687
4688 FileOutputStream fos = null;
4689 File outFile = null;
4690 try {
4691 outFile = new File(getFilesDir(), FILENAME);
4692 outFile.createNewFile();
4693 fos = new FileOutputStream(outFile);
4694 } catch (Exception e) {
4695 e.printStackTrace();
4696 }
4697 if (fos != null) {
4698 PrintWriter writer = new PrintWriter(fos);
4699
4700 writer.println(" ");
4701 writer.println("Debug logs: ");
4702 synchronized (sDumpLogs) {
4703 for (int i = 0; i < sDumpLogs.size(); i++) {
4704 writer.println(" " + sDumpLogs.get(i));
4705 }
4706 }
4707 writer.close();
4708 }
4709 try {
4710 if (fos != null) {
4711 fos.close();
4712 success = true;
4713 }
4714 } catch (IOException e) {
4715 e.printStackTrace();
4716 }
Michael Jurka43467462013-11-14 12:03:58 +01004717 return null;
Winson Chungede41292013-09-19 16:27:36 -07004718 }
Michael Jurka43467462013-11-14 12:03:58 +01004719 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004720 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004722}
Michael Jurka2763be32011-02-24 11:19:57 -08004723
Michael Jurkaabded662011-03-04 12:06:57 -08004724interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004725 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004726 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004727 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004728 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004729 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004730}
Dan Sandlerd5024042014-01-09 15:01:33 -05004731
4732interface DebugIntents {
4733 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4734 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004735}