blob: 2fa2f4ad70dfeb66234bb1325bc543d9dcf58067 [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;
Sunny Goyale755d462014-07-22 13:48:29 -0700101import com.android.launcher3.compat.PackageInstallerCompat;
102import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -0700105import com.android.launcher3.util.Thunk;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700106
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.DataInputStream;
108import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700110import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800115import java.lang.reflect.InvocationTargetException;
116import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700119import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700122import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700123import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000124import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126/**
127 * Default launcher application.
128 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100129public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700130 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800131 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
132 LauncherStateTransitionAnimation.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800133 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400137 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700150 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700151
Mathew Inwood876a8462013-06-14 14:12:41 +0100152 /**
153 * IntentStarter uses request codes starting with this. This must be greater than all activity
154 * request codes used internally.
155 */
156 protected static final int REQUEST_LAST = 100;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
159
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Winson Chung2672ff92012-05-04 16:22:30 -0700166 // The Intent extra that defines whether to ignore the launch animation
167 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400168 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
171 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700172 // Type: int
173 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
176 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
178 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: boolean
183 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
184 // Type: long
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700186 // Type: int
187 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
190 // Type: parcelable
191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: int[]
195 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Adam Cohen3ed316a2014-07-23 18:21:20 -0700200 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
201 static final String ACTION_FIRST_LOAD_COMPLETE =
202 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
203
Adam Cohen39a06042013-07-19 14:30:12 -0700204 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700205 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700206
Sunny Goyal594d76d2014-11-06 10:12:54 -0800207 private static final String QSB_WIDGET_ID = "qsb_widget_id";
208 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
209
Winson Chunga6945242014-01-08 14:04:34 -0800210 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
211
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700212 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800213 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700214 @Thunk State mState = State.WORKSPACE;
215 @Thunk AnimatorSet mStateAnimation;
216 @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;
Adam Cohen091440a2015-03-18 14:16:05 -0700267 @Thunk AppsContainerView mAppsView;
268 @Thunk AppsCustomizeTabHost mAppsCustomizeTabHost;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 private AppsCustomizePagedView mAppsCustomizeContent;
270 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800271 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700274 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
275 // scroll issues (because the workspace may not have been measured yet) and extra work.
276 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
277 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
279 private SpannableStringBuilder mDefaultKeySsb = null;
280
Adam Cohen091440a2015-03-18 14:16:05 -0700281 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800283 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private boolean mRestoring;
285 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700286 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
Michael Jurka1e2f4652013-07-08 18:03:46 -0700288 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700289 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 private Bundle mSavedInstanceState;
292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700295 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700297 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
Adam Cohen091440a2015-03-18 14:16:05 -0700300 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700302 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
Adam Cohen61f560d2013-09-30 15:58:20 -0700304 private View.OnTouchListener mHapticFeedbackTouchListener;
305
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 // Related to the auto-advancing of widgets
307 private final int ADVANCE_MSG = 1;
308 private final int mAdvanceInterval = 20000;
309 private final int mAdvanceStagger = 250;
310 private long mAutoAdvanceSentTime;
311 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700313 new HashMap<View, AppWidgetProviderInfo>();
314
Winson Chung400438b2011-01-16 17:53:48 -0800315 // Determines how long to wait after a rotation before restoring the screen orientation to
316 // match the sensor state.
317 private final int mRestoreScreenOrientationDelay = 500;
318
Adam Cohen091440a2015-03-18 14:16:05 -0700319 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700320
Adam Cohen1462de32012-07-24 22:34:36 -0700321 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700323
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700324 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700325 static Date sDateStamp = new Date();
326 static DateFormat sDateFormat =
327 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
328 static long sRunStart = System.currentTimeMillis();
329 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330
Winson Chung46353de2012-02-16 14:05:10 -0800331 // We only want to get the SharedPreferences once since it does an FS stat each time we get
332 // it from the context.
333 private SharedPreferences mSharedPrefs;
334
Winson Chungf0c6ae02012-03-21 16:10:31 -0700335 // Holds the page that we need to animate to, and the icon views that we need to animate up
336 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700337 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700338 private Bitmap mFolderIconBitmap;
339 private Canvas mFolderIconCanvas;
340 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700341
Michael Jurkaddd62e92011-02-16 17:49:14 -0800342 private BubbleTextView mWaitingForResume;
343
Adam Cohen59400422014-03-05 18:07:04 -0800344 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
345 new HashMap<String, CustomAppWidget>();
346
347 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
348 static {
349 if (ENABLE_CUSTOM_WIDGET_TEST) {
350 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
351 }
352 }
353
Chet Haasea8f996d2015-02-17 12:56:04 -0800354 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
355 private static Method sClipRevealMethod = null;
356 static {
357 Class<?> activityOptionsClass = ActivityOptions.class;
358 try {
359 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
360 View.class, int.class, int.class, int.class, int.class);
361 } catch (Exception e) {
362 // Earlier version
363 }
364 }
365
Adam Cohen091440a2015-03-18 14:16:05 -0700366 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700367 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700368 if (mWorkspace != null) {
369 mWorkspace.buildPageHardwareLayers();
370 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700371 }
372 };
373
Adam Cohendb364c32014-05-20 14:23:40 -0700374 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375
Adam Cohen091440a2015-03-18 14:16:05 -0700376 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377 int requestCode;
378 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700379 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700380 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381 int cellX;
382 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700383 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 }
385
Daniel Sandlerff02d492013-08-05 02:12:05 -0400386 private Stats mStats;
387
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700388 FocusIndicatorView mFocusHandler;
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 @Override
391 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700392 if (DEBUG_STRICT_MODE) {
393 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
394 .detectDiskReads()
395 .detectDiskWrites()
396 .detectNetwork() // or .detectAll() for all detectable problems
397 .penaltyLog()
398 .build());
399 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
400 .detectLeakedSqlLiteObjects()
401 .detectLeakedClosableObjects()
402 .penaltyLog()
403 .penaltyDeath()
404 .build());
405 }
406
Adam Cohen9211d422014-10-07 18:14:53 -0700407 if (mLauncherCallbacks != null) {
408 mLauncherCallbacks.preOnCreate();
409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400413 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700415 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700416
Winson Chung5f8afe62013-08-12 16:19:28 -0700417 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700418 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700419
420 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700422 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700423 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800424 mModel = app.setLauncher(this);
425 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700426 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800429 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Daniel Sandlerff02d492013-08-05 02:12:05 -0400431 mStats = new Stats(this);
432
Sunny Goyalffe83f12014-08-14 17:39:34 -0700433 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
436 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700438 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
439 // this also ensures that any synchronous binding below doesn't re-trigger another
440 // LauncherModel load.
441 mPaused = false;
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200444 android.os.Debug.startMethodTracing(
445 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
448 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100452 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800454 registerContentObservers();
455
Joe Onorato7c312c12009-08-13 21:36:53 -0700456 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 mSavedState = savedInstanceState;
459 restoreState(mSavedState);
460
461 if (PROFILE_STARTUP) {
462 android.os.Debug.stopMethodTracing();
463 }
464
465 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700466 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 // If the user leaves launcher, then we should just load items asynchronously when
468 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500469 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 } else {
471 // We only load the page synchronously if the user rotates (or triggers a
472 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800473 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476
477 // For handling default keys
478 mDefaultKeySsb = new SpannableStringBuilder();
479 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800480
481 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
482 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800483
Adam Cohenf9426d52012-06-04 17:26:21 -0700484 // On large interfaces, we want the screen to auto-rotate based on the current orientation
485 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700486
Adam Cohen9211d422014-10-07 18:14:53 -0700487 if (mLauncherCallbacks != null) {
488 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700489 if (mLauncherCallbacks.hasLauncherOverlay()) {
490 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700491 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
492 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
493 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700494 mWorkspace.setLauncherOverlay(mLauncherOverlay);
495 }
Adam Cohen9211d422014-10-07 18:14:53 -0700496 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700497
498 if (shouldShowIntroScreen()) {
499 showIntroScreen();
500 } else {
501 showFirstRunActivity();
502 showFirstRunClings();
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
505
506 private LauncherCallbacks mLauncherCallbacks;
507
508 public void onPostCreate(Bundle savedInstanceState) {
509 super.onPostCreate(savedInstanceState);
510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onPostCreate(savedInstanceState);
512 }
513 }
514
515 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
516 mLauncherCallbacks = callbacks;
517 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700518 }
519
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700520 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700521 public void onLauncherProviderChange() {
522 if (mLauncherCallbacks != null) {
523 mLauncherCallbacks.onLauncherProviderChange();
524 }
525 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700526
Adam Cohen9211d422014-10-07 18:14:53 -0700527 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700528 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700529 if (mLauncherCallbacks != null) {
530 return mLauncherCallbacks.hasCustomContentToLeft();
531 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700532 return false;
533 }
534
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500536 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600537 * {@link #addToCustomContentPage}. This will only be invoked if
538 * {@link #hasCustomContentToLeft()} is {@code true}.
539 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500540 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700541 if (mLauncherCallbacks != null) {
542 mLauncherCallbacks.populateCustomContentContainer();
543 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 }
545
546 /**
547 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
548 * ensure the custom content page is added or removed if necessary.
549 */
550 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600551 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 // Not bound yet, wait for bindScreens to be called.
553 return;
554 }
555
556 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
557 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500558 mWorkspace.createCustomContentContainer();
559 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600560 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
561 mWorkspace.removeCustomContentPage();
562 }
563 }
564
Adam Cohen091440a2015-03-18 14:16:05 -0700565 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 if (sLocaleConfiguration == null) {
567 new AsyncTask<Void, Void, LocaleConfiguration>() {
568 @Override
569 protected LocaleConfiguration doInBackground(Void... unused) {
570 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
571 readConfiguration(Launcher.this, localeConfiguration);
572 return localeConfiguration;
573 }
574
575 @Override
576 protected void onPostExecute(LocaleConfiguration result) {
577 sLocaleConfiguration = result;
578 checkForLocaleChange(); // recursive, but now with a locale configuration
579 }
580 }.execute();
581 return;
582 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700583
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final Configuration configuration = getResources().getConfiguration();
585
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 final String locale = configuration.locale.toString();
588
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final int mcc = configuration.mcc;
591
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 final int mnc = configuration.mnc;
594
Romain Guy84f296c2009-11-04 15:00:44 -0800595 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596
Romain Guy84f296c2009-11-04 15:00:44 -0800597 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 sLocaleConfiguration.locale = locale;
599 sLocaleConfiguration.mcc = mcc;
600 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700601
Joe Onorato0589f0f2010-02-08 13:44:00 -0800602 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603
604 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100605 new AsyncTask<Void, Void, Void>() {
606 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100608 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 }
Michael Jurka43467462013-11-14 12:03:58 +0100610 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700611 }
612 }
613
Adam Cohen091440a2015-03-18 14:16:05 -0700614 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700615 public String locale;
616 public int mcc = -1;
617 public int mnc = -1;
618 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700619
Adam Cohen091440a2015-03-18 14:16:05 -0700620 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700621 DataInputStream in = null;
622 try {
Helena Josol28db2802014-10-09 17:04:09 +0100623 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700624 configuration.locale = in.readUTF();
625 configuration.mcc = in.readInt();
626 configuration.mnc = in.readInt();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 // Ignore
631 } finally {
632 if (in != null) {
633 try {
634 in.close();
635 } catch (IOException e) {
636 // Ignore
637 }
638 }
639 }
640 }
641
Adam Cohen091440a2015-03-18 14:16:05 -0700642 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700643 DataOutputStream out = null;
644 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100645 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100646 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700647 out.writeUTF(configuration.locale);
648 out.writeInt(configuration.mcc);
649 out.writeInt(configuration.mnc);
650 out.flush();
651 } catch (FileNotFoundException e) {
652 // Ignore
653 } catch (IOException e) {
654 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100655 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700656 } finally {
657 if (out != null) {
658 try {
659 out.close();
660 } catch (IOException e) {
661 // Ignore
662 }
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665 }
666
Anton Hanssona2f665f2013-09-26 12:18:32 +0100667 public Stats getStats() {
668 return mStats;
669 }
670
Bjorn Bringertc459e522013-06-07 19:36:01 +0100671 public LayoutInflater getInflater() {
672 return mInflater;
673 }
674
Winson Chung36a62fe2012-05-06 18:04:42 -0700675 boolean isDraggingEnabled() {
676 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
677 // that is subsequently removed from the workspace in startBinding().
678 return !mModel.isLoadingWorkspace();
679 }
680
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800681 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000682 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100683 if (Build.VERSION.SDK_INT >= 17) {
684 return View.generateViewId();
685 } else {
686 // View.generateViewId() is not available. The following fallback logic is a copy
687 // of its implementation.
688 for (;;) {
689 final int result = sNextGeneratedId.get();
690 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
691 int newValue = result + 1;
692 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
693 if (sNextGeneratedId.compareAndSet(result, newValue)) {
694 return result;
695 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000696 }
697 }
698 }
699
700 public int getViewIdForItem(ItemInfo info) {
701 // This cast is safe given the > 2B range for int.
702 int itemId = (int) info.id;
703 if (mItemIdToViewId.containsKey(itemId)) {
704 return mItemIdToViewId.get(itemId);
705 }
706 int viewId = generateViewId();
707 mItemIdToViewId.put(itemId, viewId);
708 return viewId;
709 }
710
711 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700712 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
713 * a configuration step, this allows the proper animations to run after other transitions.
714 */
Adam Cohendb364c32014-05-20 14:23:40 -0700715 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700716 long screenId = args.screenId;
717 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
718 // When the screen id represents an actual screen (as opposed to a rank) we make sure
719 // that the drop page actually exists.
720 screenId = ensurePendingDropLayoutExists(args.screenId);
721 }
Adam Cohendb364c32014-05-20 14:23:40 -0700722
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800723 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700725 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700727 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700729 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700730 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700731 case REQUEST_RECONFIGURE_APPWIDGET:
732 completeRestoreAppWidget(args.appWidgetId);
733 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 }
Michael Jurka27614d22012-04-02 06:40:22 -0700735 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
736 // if you turned the screen off and then back while in All Apps, Launcher would not
737 // return to the workspace. Clearing mAddInfo.container here fixes this issue
738 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700739 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 }
741
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800742 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700744 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700745 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700748
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 Runnable exitSpringLoaded = new Runnable() {
750 @Override
751 public void run() {
752 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
753 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
754 }
755 };
756
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
760 if (resultCode == RESULT_CANCELED) {
761 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700762 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800765 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 }
768 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200769 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
770 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
771 mWorkspace.exitOverviewMode(false);
772 }
773 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
777 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700778
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 // We have special handling for widgets
781 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800782 final int appWidgetId;
783 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
784 : -1;
785 if (widgetId < 0) {
786 appWidgetId = pendingAddWidgetId;
787 } else {
788 appWidgetId = widgetId;
789 }
790
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800792 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700793 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
794 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 result = RESULT_CANCELED;
796 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 @Override
799 public void run() {
800 exitSpringLoadedDragModeDelayed(false, 0, null);
801 }
802 };
Adam Cohendb364c32014-05-20 14:23:40 -0700803 if (workspaceLocked) {
804 // No need to remove the empty screen if we're mid-binding, as the
805 // the bind will not add the empty screen.
806 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
807 } else {
808 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
809 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
810 }
Winson Chung5aaab772012-04-27 15:24:02 -0700811 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700812 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700813 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
814 // When the screen id represents an actual screen (as opposed to a rank)
815 // we make sure that the drop page actually exists.
816 mPendingAddInfo.screenId =
817 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
818 }
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
820
821 dropLayout.setDropPending(true);
822 final Runnable onComplete = new Runnable() {
823 @Override
824 public void run() {
825 completeTwoStageWidgetDrop(resultCode, appWidgetId);
826 dropLayout.setDropPending(false);
827 }
828 };
829 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 } else {
832 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
833 mPendingAddInfo);
834 sPendingAddItem = args;
835 }
Winson Chung5aaab772012-04-27 15:24:02 -0700836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 return;
838 }
839
Sunny Goyalff572272014-07-23 13:58:07 -0700840 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
841 if (resultCode == RESULT_OK) {
842 // Update the widget view.
843 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
844 pendingAddWidgetId, mPendingAddInfo);
845 if (workspaceLocked) {
846 sPendingAddItem = args;
847 } else {
848 completeAdd(args);
849 }
850 }
851 // Leave the widget in the pending state if the user canceled the configure.
852 return;
853 }
854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 // The pattern used here is that a user PICKs a specific application,
856 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
859 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700860 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700861 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
862 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700864 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800865 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700867 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Adam Cohen00074722013-10-11 17:46:09 -0700870 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700871 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800875
876 }
877
878 @Override
879 protected void onActivityResult(
880 final int requestCode, final int resultCode, final Intent data) {
881 handleActivityResult(requestCode, resultCode, data);
882 if (mLauncherCallbacks != null) {
883 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
884 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
886
Adam Cohendb364c32014-05-20 14:23:40 -0700887 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
888 appWidgetId, ItemInfo info) {
889 PendingAddArguments args = new PendingAddArguments();
890 args.requestCode = requestCode;
891 args.intent = data;
892 args.container = info.container;
893 args.screenId = info.screenId;
894 args.cellX = info.cellX;
895 args.cellY = info.cellY;
896 args.appWidgetId = appWidgetId;
897 return args;
898 }
899
900 /**
901 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
902 *
903 * @param screenId the screen id to check
904 * @return the new screen, or screenId if it exists
905 */
906 private long ensurePendingDropLayoutExists(long screenId) {
907 CellLayout dropLayout =
908 (CellLayout) mWorkspace.getScreenWithId(screenId);
909 if (dropLayout == null) {
910 // it's possible that the add screen was removed because it was
911 // empty and a re-bind occurred
912 mWorkspace.addExtraEmptyScreen();
913 return mWorkspace.commitExtraEmptyScreen();
914 } else {
915 return screenId;
916 }
917 }
918
Adam Cohen091440a2015-03-18 14:16:05 -0700919 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700920 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 Runnable onCompleteRunnable = null;
923 int animationType = 0;
924
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 if (resultCode == RESULT_OK) {
927 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
928 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 mPendingAddWidgetInfo);
930 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 onCompleteRunnable = new Runnable() {
932 @Override
933 public void run() {
934 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
937 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 }
939 };
940 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800941 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 if (mDragLayer.getAnimatedView() != null) {
945 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
946 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
947 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700948 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700949 // The animated view may be null in the case of a rotation during widget configuration
950 onCompleteRunnable.run();
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953
954 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700955 protected void onStop() {
956 super.onStop();
957 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStop();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
965 protected void onStart() {
966 super.onStart();
967 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700968
969 if (mLauncherCallbacks != null) {
970 mLauncherCallbacks.onStart();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200976 long startTime = 0;
977 if (DEBUG_RESUME_TIME) {
978 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400979 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200980 }
Adam Cohen9211d422014-10-07 18:14:53 -0700981
982 if (mLauncherCallbacks != null) {
983 mLauncherCallbacks.preOnResume();
984 }
985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700987
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 // Restore the previous launcher state
Winson Chungb745afb2015-03-02 11:51:23 -0800989 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800991 } else if (mOnResumeState == State.APPS) {
992 showAppsView(false /* animated */, false /* resetListToTop */);
993 } else if (mOnResumeState == State.WIDGETS) {
994 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700995 }
996 mOnResumeState = State.NONE;
997
Craig Mautner360310b2012-10-26 15:13:08 -0700998 // Background was set to gradient in onPause(), restore to black if in all apps.
999 setWorkspaceBackground(mState == State.WORKSPACE);
1000
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001001 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001002 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001003 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001004 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001006 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001009 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1010 // execute them here
1011 long startTimeCallbacks = 0;
1012 if (DEBUG_RESUME_TIME) {
1013 startTimeCallbacks = System.currentTimeMillis();
1014 }
1015
1016 if (mAppsCustomizeContent != null) {
1017 mAppsCustomizeContent.setBulkBind(true);
1018 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001019 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1020 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001021 }
1022 if (mAppsCustomizeContent != null) {
1023 mAppsCustomizeContent.setBulkBind(false);
1024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 if (DEBUG_RESUME_TIME) {
1027 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1028 (System.currentTimeMillis() - startTimeCallbacks));
1029 }
Michael Jurka447bf842013-05-15 14:52:15 +02001030 }
Michael Jurka54554252013-08-01 12:52:23 +02001031 if (mOnResumeCallbacks.size() > 0) {
1032 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1033 mOnResumeCallbacks.get(i).run();
1034 }
1035 mOnResumeCallbacks.clear();
1036 }
Winson Chunge4e50662012-01-23 14:45:13 -08001037
1038 // Reset the pressed state of icons that were locked in the press state while activities
1039 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001040 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001041 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 mWaitingForResume.setStayPressed(false);
1043 }
Winson Chung82963d52013-10-09 11:20:57 -07001044
Adam Cohen06dff352012-06-01 17:17:08 -07001045 // It is possible that widgets can receive updates while launcher is not in the foreground.
1046 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1047 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1048 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001049 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001050 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001051
Winson Chung780fe592013-09-26 14:48:44 -07001052 // Process any items that were added while Launcher was away.
1053 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1054
Michael Jurka447bf842013-05-15 14:52:15 +02001055 if (DEBUG_RESUME_TIME) {
1056 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1057 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001058
1059 if (mWorkspace.getCustomContentCallbacks() != null) {
1060 // If we are resuming and the custom content is the current page, we call onShow().
1061 // It is also poassible that onShow will instead be called slightly after first layout
1062 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1063 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001064 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001065 }
1066 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001067 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001068 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001069
1070 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001071
1072 if (mLauncherCallbacks != null) {
1073 mLauncherCallbacks.onResume();
1074 }
Adam Cohen06dff352012-06-01 17:17:08 -07001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001079 // Ensure that items added to Launcher are queued until Launcher returns
1080 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001081 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001082
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001084 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001085 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001086 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001087
1088 // We call onHide() aggressively. The custom content callbacks should be able to
1089 // debounce excess onHide calls.
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 mWorkspace.getCustomContentCallbacks().onHide();
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Adam Cohen9211d422014-10-07 18:14:53 -07001094 if (mLauncherCallbacks != null) {
1095 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Adam Cohenc2d6e892014-10-16 09:49:24 -07001114 public interface LauncherOverlay {
1115
1116 /**
1117 * Touch interaction leading to overscroll has begun
1118 */
1119 public void onScrollInteractionBegin();
1120
1121 /**
1122 * Touch interaction related to overscroll has ended
1123 */
1124 public void onScrollInteractionEnd();
1125
1126 /**
1127 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1128 * screen (or in the case of RTL, the rightmost screen).
1129 */
1130 public void onScrollChange(int progress, boolean rtl);
1131
1132 /**
1133 * Screen has stopped scrolling
1134 */
1135 public void onScrollSettled();
1136
1137 /**
1138 * This method can be called by the Launcher in order to force the LauncherOverlay
1139 * to exit fully immersive mode.
1140 */
1141 public void forceExitFullImmersion();
1142 }
1143
1144 public interface LauncherOverlayCallbacks {
1145 /**
1146 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1147 * however it doesn't modify any state within the launcher.
1148 */
1149 public boolean canEnterFullImmersion();
1150
1151 /**
1152 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1153 * eg. by occupying the full screen and handling all touch events.
1154 *
1155 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1156 * case, Launcher will modify any necessary state and assumes the overlay is
1157 * handling all interaction. If false, the LauncherOverlay should cancel any
1158 *
1159 */
1160 public boolean enterFullImmersion();
1161
1162 /**
1163 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1164 * full control over UI and state.
1165 */
1166 public void exitFullImmersion();
1167 }
1168
1169 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1170
1171 @Override
1172 public boolean canEnterFullImmersion() {
1173 return mState == State.WORKSPACE;
1174 }
1175
1176 @Override
1177 public boolean enterFullImmersion() {
1178 if (mState == State.WORKSPACE) {
1179 // When fully immersed, disregard any touches which fall through.
1180 mDragLayer.setBlockTouch(true);
1181 return true;
1182 }
1183 return false;
1184 }
1185
1186 @Override
1187 public void exitFullImmersion() {
1188 mDragLayer.setBlockTouch(false);
1189 }
1190 }
1191
Jorim Jaggid017f882014-01-14 17:08:48 -08001192 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001193 if (mLauncherCallbacks != null) {
1194 return mLauncherCallbacks.hasSettings();
1195 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 return false;
1197 }
1198
Adam Cohenbffe7452013-07-22 18:21:45 -07001199
Adam Cohen9211d422014-10-07 18:14:53 -07001200 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001201 CustomContentCallbacks callbacks, String description) {
1202 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001203 }
1204
Adam Cohenedb40762013-07-18 16:45:45 -07001205 // The custom content needs to offset its content to account for the QSB
1206 public int getTopOffsetForCustomContent() {
1207 return mWorkspace.getPaddingTop();
1208 }
1209
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001210 @Override
1211 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001213 if (mModel.isCurrentCallbacks(this)) {
1214 mModel.stopLoader();
1215 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001216 if (mAppsCustomizeContent != null) {
1217 mAppsCustomizeContent.surrender();
1218 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001219 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001220 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001221
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223 @Override
1224 public void onWindowFocusChanged(boolean hasFocus) {
1225 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001226 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001227
1228 if (mLauncherCallbacks != null) {
1229 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1230 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001231 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 private boolean acceptFilter() {
1234 final InputMethodManager inputManager = (InputMethodManager)
1235 getSystemService(Context.INPUT_METHOD_SERVICE);
1236 return !inputManager.isFullscreenMode();
1237 }
1238
1239 @Override
1240 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001241 final int uniChar = event.getUnicodeChar();
1242 final boolean handled = super.onKeyDown(keyCode, event);
1243 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1244 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1246 keyCode, event);
1247 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001249 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001250 // showSearchDialog()
1251 // If there are multiple keystrokes before the search dialog takes focus,
1252 // onSearchRequested() will be called for every keystroke,
1253 // but it is idempotent, so it's fine.
1254 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256 }
1257
Joe Onorato8a9625e2010-01-28 15:55:35 -08001258 // Eat the long press event so the keyboard doesn't come up.
1259 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1260 return true;
1261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 return handled;
1264 }
1265
Karl Rosaen138a0412009-04-23 19:00:21 -07001266 private String getTypedText() {
1267 return mDefaultKeySsb.toString();
1268 }
1269
1270 private void clearTypedText() {
1271 mDefaultKeySsb.clear();
1272 mDefaultKeySsb.clearSpans();
1273 Selection.setSelection(mDefaultKeySsb, 0);
1274 }
1275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001277 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1278 * State
1279 */
1280 private static State intToState(int stateOrdinal) {
1281 State state = State.WORKSPACE;
1282 final State[] stateValues = State.values();
1283 for (int i = 0; i < stateValues.length; i++) {
1284 if (stateValues[i].ordinal() == stateOrdinal) {
1285 state = stateValues[i];
1286 break;
1287 }
1288 }
1289 return state;
1290 }
1291
1292 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 * Restores the previous state, if it exists.
1294 *
1295 * @param savedState The previous state.
1296 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001297 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 private void restoreState(Bundle savedState) {
1299 if (savedState == null) {
1300 return;
1301 }
1302
Michael Jurka883f55b2010-10-21 15:47:14 -07001303 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001304 if (state == State.APPS || state == State.WIDGETS) {
1305 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001306 }
1307
Adam Cohen21cd0022013-10-09 18:57:02 -07001308 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1309 PagedView.INVALID_RESTORE_PAGE);
1310 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001311 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001315 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001316
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1318 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001319 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1321 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001322 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1323 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001324 AppWidgetProviderInfo info = savedState.getParcelable(
1325 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1326 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001327 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001328 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
1331
1332 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1333 if (renameFolder) {
1334 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001335 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 mRestoring = true;
1337 }
Winson Chunga12a2502010-12-20 14:41:35 -08001338
Winson Chung785d2eb2011-04-14 16:08:02 -07001339 // Restore the AppsCustomize tab
1340 if (mAppsCustomizeTabHost != null) {
1341 String curTab = savedState.getString("apps_customize_currentTab");
1342 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001343 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001345 mAppsCustomizeContent.loadAssociatedPages(
1346 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 }
1348
Winson Chung5afbf7b2011-07-25 11:53:08 -07001349 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1350 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001351 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001352 mItemIdToViewId = (HashMap<Integer, Integer>)
1353 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
1356 /**
1357 * Finds all the views we need and configure them properly.
1358 */
1359 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001360 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001361
Craig Mautner360310b2012-10-26 15:13:08 -07001362 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001363 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001364 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1365 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001366 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001367 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001368
John Spurlock77e1f472013-09-11 10:09:51 -04001369 mLauncherView.setSystemUiVisibility(
1370 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001371 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372
Winson Chung4c98d922011-05-31 16:50:48 -07001373 // Setup the drag layer
1374 mDragLayer.setup(this, dragController);
1375
Winson Chung3d503fb2011-07-13 17:25:49 -07001376 // Setup the hotseat
1377 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1378 if (mHotseat != null) {
1379 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001380 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001381 }
1382
Jorim Jaggid017f882014-01-14 17:08:48 -08001383 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001384 View widgetButton = findViewById(R.id.widget_button);
1385 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001386 @Override
1387 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001388 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001389 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001390 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001391 }
1392 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001393 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1394
1395 View wallpaperButton = findViewById(R.id.wallpaper_button);
1396 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 @Override
1398 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001399 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001400 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001401 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001402 }
1403 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001404 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1405
1406 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001407 if (hasSettings()) {
1408 settingsButton.setOnClickListener(new OnClickListener() {
1409 @Override
1410 public void onClick(View arg0) {
1411 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001412 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001414 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001415 });
1416 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1417 } else {
1418 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001419 }
1420
Adam Cohendbdff6b2013-09-18 19:09:15 -07001421 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001422
Winson Chung4c98d922011-05-31 16:50:48 -07001423 // Setup the workspace
1424 mWorkspace.setHapticFeedbackEnabled(false);
1425 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001426 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001427 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001428
Winson Chungf0ea4d32011-06-06 14:27:16 -07001429 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001430 mSearchDropTargetBar = (SearchDropTargetBar)
1431 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001432
Winson Chungb745afb2015-03-02 11:51:23 -08001433 // Setup Apps
1434 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1435
Winson Chungf0ea4d32011-06-06 14:27:16 -07001436 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001437 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001438 mAppsCustomizeContent = (AppsCustomizePagedView)
1439 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1440 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001441
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001443 dragController.setDragScoller(mWorkspace);
1444 dragController.setScrollView(mDragLayer);
1445 dragController.setMoveTarget(mWorkspace);
1446 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001447 if (mSearchDropTargetBar != null) {
1448 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001449 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001450 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001451
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001452 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001454 mWeightWatcher = new WeightWatcher(this);
1455 mWeightWatcher.setAlpha(0.5f);
1456 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001457 new FrameLayout.LayoutParams(
1458 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001459 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001460 Gravity.BOTTOM)
1461 );
Adam Cohen39a06042013-07-19 14:30:12 -07001462
1463 boolean show = shouldShowWeightWatcher();
1464 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001469 * Sets the all apps button. This method is called from {@link Hotseat}.
1470 */
1471 public void setAllAppsButton(View allAppsButton) {
1472 mAllAppsButton = allAppsButton;
1473 }
1474
1475 public View getAllAppsButton() {
1476 return mAllAppsButton;
1477 }
1478
1479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 * Creates a view representing a shortcut.
1481 *
1482 * @param info The data structure describing the shortcut.
1483 *
1484 * @return A View inflated from R.layout.application.
1485 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001488 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 /**
1492 * Creates a view representing a shortcut inflated from the specified resource.
1493 *
1494 * @param layoutResId The id of the XML layout used to create the shortcut.
1495 * @param parent The group the shortcut belongs to.
1496 * @param info The data structure describing the shortcut.
1497 *
1498 * @return A View inflated from layoutResId.
1499 */
Adam Cohen59400422014-03-05 18:07:04 -08001500 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001501 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001502 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001504 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return favorite;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Add a shortcut to the workspace.
1510 *
1511 * @param data The intent describing the shortcut.
1512 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001515 int cellY) {
1516 int[] cellXY = mTmpAddItemCellCoordinates;
1517 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001521
Sunny Goyal2350bc92014-10-14 16:42:54 -07001522 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001523 if (info == null) {
1524 return;
1525 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 final View view = createShortcut(info);
1527
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 // First we check if we already know the exact location where we want to add this item.
1529 if (cellX >= 0 && cellY >= 0) {
1530 cellXY[0] = cellX;
1531 cellXY[1] = cellY;
1532 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001533
1534 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001535 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 true, null,null)) {
1537 return;
1538 }
1539 DragObject dragObject = new DragObject();
1540 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001541 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1542 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001543 return;
1544 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001548 foundCellSpan = (result != null);
1549 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001551 }
1552
1553 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001554 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001555 return;
1556 }
1557
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001558 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001562 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564 }
1565
Adam Cohen2f093b62012-04-30 18:59:53 -07001566 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1567 int minHeight) {
1568 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 // We want to account for the extra amount of padding that we are adding to the widget
1570 // to ensure that it gets the full amount of space that it has requested
1571 int requiredWidth = minWidth + padding.left + padding.right;
1572 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001573 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1577 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001578 }
1579
Adam Cohen2f093b62012-04-30 18:59:53 -07001580 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1581 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001582 }
1583
Adam Cohen2f093b62012-04-30 18:59:53 -07001584 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1585 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001586 }
1587
Adam Cohen2f093b62012-04-30 18:59:53 -07001588 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1589 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001590 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001594 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001596 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 */
Adam Cohen091440a2015-03-18 14:16:05 -07001598 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001599 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1600
1601 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001602 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001603 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1604 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001605 }
Romain Guycbb89e42009-06-08 15:52:54 -07001606
Adam Cohen59400422014-03-05 18:07:04 -08001607 if (appWidgetInfo.isCustomWidget) {
1608 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001609 }
1610
Adam Cohen59400422014-03-05 18:07:04 -08001611 LauncherAppWidgetInfo launcherInfo;
1612 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1613 launcherInfo.spanX = info.spanX;
1614 launcherInfo.spanY = info.spanY;
1615 launcherInfo.minSpanX = info.minSpanX;
1616 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001617 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001620 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
1622 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 if (hostView == null) {
1624 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001625 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1626 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001627 } else {
1628 // The AppWidgetHostView has already been inflated and instantiated
1629 launcherInfo.hostView = hostView;
1630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001632 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001633 launcherInfo.notifyWidgetSizeChanged(this);
1634
Adam Cohen59400422014-03-05 18:07:04 -08001635 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1636 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001637
1638 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001640 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1644 @Override
1645 public void onReceive(Context context, Intent intent) {
1646 final String action = intent.getAction();
1647 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1648 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001649 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001650 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001651
Winson Chungc100e8e2011-08-09 16:02:43 -07001652 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001653 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001654 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1655 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001656 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001657 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1659 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001661 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1662 mModel.resetLoadedState(false, true);
1663 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1664 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1665 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1666 mModel.resetLoadedState(false, true);
1667 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1668 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1669 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001670 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1671 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1672 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674 }
1675 };
1676
1677 @Override
1678 public void onAttachedToWindow() {
1679 super.onAttachedToWindow();
1680
1681 // Listen for broadcasts related to user-presence
1682 final IntentFilter filter = new IntentFilter();
1683 filter.addAction(Intent.ACTION_SCREEN_OFF);
1684 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001685 // For handling managed profiles
1686 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1687 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 if (ENABLE_DEBUG_INTENTS) {
1689 filter.addAction(DebugIntents.DELETE_DATABASE);
1690 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1691 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001693 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001694 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001695 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 mVisible = true;
1697 }
1698
Adam Cohen0b395352014-06-09 22:54:36 +00001699 /**
1700 * Sets up transparent navigation and status bars in LMP.
1701 * This method is a no-op for other platform versions.
1702 */
Sunny Goyald0091012015-01-20 15:55:34 -08001703 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001704 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001705 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001706 Window window = getWindow();
1707 window.getAttributes().systemUiVisibility |=
1708 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1709 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1710 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1711 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1712 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1713 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1714 window.setStatusBarColor(Color.TRANSPARENT);
1715 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001716 }
1717 }
1718
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 @Override
1720 public void onDetachedFromWindow() {
1721 super.onDetachedFromWindow();
1722 mVisible = false;
1723
Adam Cohend113e0c2010-11-11 10:48:05 -08001724 if (mAttached) {
1725 unregisterReceiver(mReceiver);
1726 mAttached = false;
1727 }
Winson Chungb745afb2015-03-02 11:51:23 -08001728 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730
1731 public void onWindowVisibilityChanged(int visibility) {
1732 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001733 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001734 // The following code used to be in onResume, but it turns out onResume is called when
1735 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1736 // is a more appropriate event to handle
1737 if (mVisible) {
1738 mAppsCustomizeTabHost.onWindowVisible();
1739 if (!mWorkspaceLoading) {
1740 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001741 // We want to let Launcher draw itself at least once before we force it to build
1742 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001743 // apps is nice and speedy.
1744 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001746 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001747 if (mStarted) return;
1748 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001749 // We delay the layer building a bit in order to give
1750 // other message processing a time to run. In particular
1751 // this avoids a delay in hiding the IME if it was
1752 // currently shown, because doing that may involve
1753 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001754 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001755 final ViewTreeObserver.OnDrawListener listener = this;
1756 mWorkspace.post(new Runnable() {
1757 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001758 if (mWorkspace != null &&
1759 mWorkspace.getViewTreeObserver() != null) {
1760 mWorkspace.getViewTreeObserver().
1761 removeOnDrawListener(listener);
1762 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001763 }
1764 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001765 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001766 }
1767 });
1768 }
1769 clearTypedText();
1770 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772
Adam Cohen091440a2015-03-18 14:16:05 -07001773 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 mHandler.removeMessages(ADVANCE_MSG);
1775 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1776 mHandler.sendMessageDelayed(msg, delay);
1777 mAutoAdvanceSentTime = System.currentTimeMillis();
1778 }
1779
Adam Cohen091440a2015-03-18 14:16:05 -07001780 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1782 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1783 mAutoAdvanceRunning = autoAdvanceRunning;
1784 if (autoAdvanceRunning) {
1785 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1786 sendAdvanceMessage(delay);
1787 } else {
1788 if (!mWidgetsToAdvance.isEmpty()) {
1789 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1790 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1791 }
1792 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001793 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 }
1795 }
1796 }
1797
1798 private final Handler mHandler = new Handler() {
1799 @Override
1800 public void handleMessage(Message msg) {
1801 if (msg.what == ADVANCE_MSG) {
1802 int i = 0;
1803 for (View key: mWidgetsToAdvance.keySet()) {
1804 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1805 final int delay = mAdvanceStagger * i;
1806 if (v instanceof Advanceable) {
1807 postDelayed(new Runnable() {
1808 public void run() {
1809 ((Advanceable) v).advance();
1810 }
1811 }, delay);
1812 }
1813 i++;
1814 }
1815 sendAdvanceMessage(mAdvanceInterval);
1816 }
1817 }
1818 };
1819
1820 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001821 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1823 if (v instanceof Advanceable) {
1824 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001825 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001826 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 }
1828 }
1829
1830 void removeWidgetToAutoAdvance(View hostView) {
1831 if (mWidgetsToAdvance.containsKey(hostView)) {
1832 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001833 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001834 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 }
1836
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001838 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 launcherInfo.hostView = null;
1840 }
1841
Winson Chung93eef082012-03-23 15:59:27 -07001842 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1843 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1844 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001845 }
1846
Winson Chunga6945242014-01-08 14:04:34 -08001847 public DragLayer getDragLayer() {
1848 return mDragLayer;
1849 }
1850
Winson Chungb745afb2015-03-02 11:51:23 -08001851 public AppsContainerView getAppsView() {
1852 return mAppsView;
1853 }
1854
1855 public AppsCustomizeTabHost getWidgetsView() {
1856 return mAppsCustomizeTabHost;
1857 }
1858
Winson Chunga6945242014-01-08 14:04:34 -08001859 public Workspace getWorkspace() {
1860 return mWorkspace;
1861 }
1862
1863 public Hotseat getHotseat() {
1864 return mHotseat;
1865 }
1866
Jorim Jaggid017f882014-01-14 17:08:48 -08001867 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001868 return mOverviewPanel;
1869 }
1870
1871 public SearchDropTargetBar getSearchBar() {
1872 return mSearchDropTargetBar;
1873 }
1874
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001875 public LauncherAppWidgetHost getAppWidgetHost() {
1876 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
Romain Guycbb89e42009-06-08 15:52:54 -07001878
Winson Chunga9abd0e2010-10-27 17:18:37 -07001879 public LauncherModel getModel() {
1880 return mModel;
1881 }
1882
Winson Chunga6945242014-01-08 14:04:34 -08001883 protected SharedPreferences getSharedPrefs() {
1884 return mSharedPrefs;
1885 }
1886
Bjorn Bringertc459e522013-06-07 19:36:01 +01001887 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001888 getWindow().closeAllPanels();
1889
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001890 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001891 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001892 }
1893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 @Override
1895 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001896 long startTime = 0;
1897 if (DEBUG_RESUME_TIME) {
1898 startTime = System.currentTimeMillis();
1899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 super.onNewIntent(intent);
1901
1902 // Close the menu
1903 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001904 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001905 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001906
Adam Cohened307df2013-10-02 09:37:31 -07001907 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1908 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1909 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001910
Adam Cohen6fecd412013-10-02 17:41:50 -07001911 if (mWorkspace == null) {
1912 // Can be cases where mWorkspace is null, this prevents a NPE
1913 return;
1914 }
1915 Folder openFolder = mWorkspace.getOpenFolder();
1916 // In all these cases, only animate if we're already on home
1917 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001918
1919 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1920 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001921 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001922 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001923 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001924 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001925
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 closeFolder();
1927 exitSpringLoadedDragMode();
1928
1929 // If we are already on home, then just animate back to the workspace,
1930 // otherwise, just wait until onResume to set the state back to Workspace
1931 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001932 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001933 } else {
1934 mOnResumeState = State.WORKSPACE;
1935 }
1936
1937 final View v = getWindow().peekDecorView();
1938 if (v != null && v.getWindowToken() != null) {
1939 InputMethodManager imm = (InputMethodManager)getSystemService(
1940 INPUT_METHOD_SERVICE);
1941 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1942 }
1943
Winson Chungb745afb2015-03-02 11:51:23 -08001944 // Reset the apps view
1945 if (!alreadyOnHome && mAppsView != null) {
1946 mAppsView.scrollToTop();
1947 }
1948
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001950 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001951 mAppsCustomizeTabHost.reset();
1952 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001953
Adam Cohen9211d422014-10-07 18:14:53 -07001954 if (mLauncherCallbacks != null) {
1955 mLauncherCallbacks.onHomeIntent();
1956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
Adam Cohened307df2013-10-02 09:37:31 -07001958
Michael Jurka447bf842013-05-15 14:52:15 +02001959 if (DEBUG_RESUME_TIME) {
1960 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohen9211d422014-10-07 18:14:53 -07001963 if (mLauncherCallbacks != null) {
1964 mLauncherCallbacks.onNewIntent(intent);
1965 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001969 public void onRestoreInstanceState(Bundle state) {
1970 super.onRestoreInstanceState(state);
1971 for (int page: mSynchronouslyBoundPages) {
1972 mWorkspace.restoreInstanceStateForChild(page);
1973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
1976 @Override
1977 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001978 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001979 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1980 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001981 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001982 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
Michael Jurka883f55b2010-10-21 15:47:14 -07001984 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001985 // We close any open folder since it will not be re-opened, and we need to make sure
1986 // this state is reflected.
1987 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
Adam Cohendcd297f2013-06-18 13:13:40 -07001989 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001990 mWaitingForResult) {
1991 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1994 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001995 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1996 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1997 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001998 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000
2001 if (mFolderInfo != null && mWaitingForResult) {
2002 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2003 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2004 }
Michael Jurka946ad472010-07-09 18:05:18 -07002005
Winson Chung785d2eb2011-04-14 16:08:02 -07002006 // Save the current AppsCustomize tab
2007 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002008 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2009 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002010 if (currentTabTag != null) {
2011 outState.putString("apps_customize_currentTab", currentTabTag);
2012 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002013 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2014 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002015 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002016 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002017
2018 if (mLauncherCallbacks != null) {
2019 mLauncherCallbacks.onSaveInstanceState(outState);
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
2023 @Override
2024 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002026
Winson Chunge7a03942011-08-05 15:05:12 -07002027 // Remove all pending runnables
2028 mHandler.removeMessages(ADVANCE_MSG);
2029 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002030 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002031
Winson Chungcd2b0142011-06-08 16:02:26 -07002032 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002033 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002034
2035 // It's possible to receive onDestroy after a new Launcher activity has
2036 // been created. In this case, don't interfere with the new Launcher.
2037 if (mModel.isCurrentCallbacks(this)) {
2038 mModel.stopLoader();
2039 app.setLauncher(null);
2040 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002043 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002045 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002047 mAppWidgetHost = null;
2048
2049 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050
2051 TextKeyListener.getInstance().release();
2052
Winson Chung81b52252012-08-27 15:34:29 -07002053 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2054 // to prevent leaking Launcher activities on orientation change.
2055 if (mModel != null) {
2056 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2057 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002058
2059 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002060 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002062 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002063 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002064 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002065 mWorkspace = null;
2066 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002067
Michael Jurka2ecf9952012-06-18 12:52:28 -07002068 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002069
2070 if (mLauncherCallbacks != null) {
2071 mLauncherCallbacks.onDestroy();
2072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
Adam Cohena9cf38f2011-05-02 15:36:58 -07002075 public DragController getDragController() {
2076 return mDragController;
2077 }
2078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 @Override
2080 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002081 onStartForResult(requestCode);
2082 super.startActivityForResult(intent, requestCode);
2083 }
2084
2085 @Override
2086 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2087 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2088 onStartForResult(requestCode);
2089 try {
2090 super.startIntentSenderForResult(intent, requestCode,
2091 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2092 } catch (IntentSender.SendIntentException e) {
2093 throw new ActivityNotFoundException();
2094 }
2095 }
2096
2097 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002098 if (requestCode >= 0) {
2099 setWaitingForResult(true);
2100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102
Winson Chung70d72102011-08-12 11:24:35 -07002103 /**
2104 * Indicates that we want global search for this activity by setting the globalSearch
2105 * argument for {@link #startSearch} to true.
2106 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002108 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002110
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002111 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002112
Karl Rosaen138a0412009-04-23 19:00:21 -07002113 if (initialQuery == null) {
2114 // Use any text typed in the launcher as the initial query
2115 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 if (appSearchData == null) {
2118 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002119 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 }
Winson Chungadf0c182012-08-23 14:59:07 -07002121 Rect sourceBounds = new Rect();
2122 if (mSearchDropTargetBar != null) {
2123 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2124 }
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Ian Parkinson047036e2014-09-01 15:40:53 +01002126 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002128 if (clearTextImmediately) {
2129 clearTypedText();
2130 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 }
2132
Ian Parkinson047036e2014-09-01 15:40:53 +01002133 /**
2134 * Start a text search.
2135 *
2136 * @return {@code true} if the search will start immediately, so any further keypresses
2137 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2138 * to buffer keypresses.
2139 */
2140 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002142 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2143 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2144 sourceBounds);
2145 }
2146
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002149 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002150 }
2151
2152 /**
2153 * Starts the global search activity. This code is a copied from SearchManager
2154 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002157 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002158 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2159 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2160 if (globalSearchActivity == null) {
2161 Log.w(TAG, "No global search activity found.");
2162 return;
2163 }
2164 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2165 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2166 intent.setComponent(globalSearchActivity);
2167 // Make sure that we have a Bundle to put source in
2168 if (appSearchData == null) {
2169 appSearchData = new Bundle();
2170 } else {
2171 appSearchData = new Bundle(appSearchData);
2172 }
Adam Cohen9211d422014-10-07 18:14:53 -07002173 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002174 if (!appSearchData.containsKey("source")) {
2175 appSearchData.putString("source", getPackageName());
2176 }
2177 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2178 if (!TextUtils.isEmpty(initialQuery)) {
2179 intent.putExtra(SearchManager.QUERY, initialQuery);
2180 }
2181 if (selectInitialQuery) {
2182 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2183 }
2184 intent.setSourceBounds(sourceBounds);
2185 try {
2186 startActivity(intent);
2187 } catch (ActivityNotFoundException ex) {
2188 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191
Yura4f93ec62014-02-04 14:15:21 +00002192 public boolean isOnCustomContent() {
2193 return mWorkspace.isOnOrMovingToCustomContent();
2194 }
2195
Winson Chung70d72102011-08-12 11:24:35 -07002196 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002197 public boolean onPrepareOptionsMenu(Menu menu) {
2198 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002199 if (!isOnCustomContent()) {
2200 // Close any open folders
2201 closeFolder();
2202 // Stop resizing any widgets
2203 mWorkspace.exitWidgetResizeMode();
2204 if (!mWorkspace.isInOverviewMode()) {
2205 // Show the overview mode
2206 showOverviewMode(true);
2207 } else {
2208 showWorkspace(true);
2209 }
Winson Chunge0298742014-01-17 12:03:00 -08002210 }
Adam Cohen9211d422014-10-07 18:14:53 -07002211 if (mLauncherCallbacks != null) {
2212 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2213 }
2214
Michael Jurkab94f3f82013-09-11 18:08:54 +02002215 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002218 @Override
2219 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002220 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002221 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002222 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002223 }
2224
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 public boolean isWorkspaceLocked() {
2226 return mWorkspaceLoading || mWaitingForResult;
2227 }
2228
Adam Cohen517a7f52014-03-01 12:12:59 -08002229 public boolean isWorkspaceLoading() {
2230 return mWorkspaceLoading;
2231 }
2232
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002233 private void setWorkspaceLoading(boolean value) {
2234 boolean isLocked = isWorkspaceLocked();
2235 mWorkspaceLoading = value;
2236 if (isLocked != isWorkspaceLocked()) {
2237 onWorkspaceLockedChanged();
2238 }
2239 }
2240
2241 private void setWaitingForResult(boolean value) {
2242 boolean isLocked = isWorkspaceLocked();
2243 mWaitingForResult = value;
2244 if (isLocked != isWorkspaceLocked()) {
2245 onWorkspaceLockedChanged();
2246 }
2247 }
2248
Adam Cohen9211d422014-10-07 18:14:53 -07002249 protected void onWorkspaceLockedChanged() {
2250 if (mLauncherCallbacks != null) {
2251 mLauncherCallbacks.onWorkspaceLockedChanged();
2252 }
2253 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002254
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2259 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002261 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002263
Sunny Goyale6b63a32015-01-16 16:14:29 -08002264 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002265 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002266 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2267 }
2268
Sunny Goyale6b63a32015-01-16 16:14:29 -08002269 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002270 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2271 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002272 if (appWidgetInfo.configure != null) {
2273 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002274 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002275
Bjorn Bringert7984c942009-12-09 15:38:25 +00002276 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002277 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2278 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002281 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002282 Runnable onComplete = new Runnable() {
2283 @Override
2284 public void run() {
2285 // Exit spring loaded mode if necessary after adding the widget
2286 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2287 null);
2288 }
2289 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002291 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002292 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 }
Romain Guycbb89e42009-06-08 15:52:54 -07002295
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002296 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002297 // Close any folders that may be open.
2298 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002299 mWorkspace.moveToCustomContentScreen(animate);
2300 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002301
2302 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2303 int[] cell, int spanX, int spanY) {
2304 switch (info.itemType) {
2305 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2306 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2307 int span[] = new int[2];
2308 span[0] = spanX;
2309 span[1] = spanY;
2310 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2311 container, screenId, cell, span);
2312 break;
2313 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2314 processShortcutFromDrop(info.componentName, container, screenId, cell);
2315 break;
2316 default:
2317 throw new IllegalStateException("Unknown item type: " + info.itemType);
2318 }
2319 }
2320
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321 /**
2322 * Process a shortcut drop.
2323 *
2324 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002328 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2329 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002330 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002333 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334
2335 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 mPendingAddInfo.cellX = cell[0];
2337 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002339
2340 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2341 createShortcutIntent.setComponent(componentName);
2342 processShortcut(createShortcutIntent);
2343 }
2344
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 /**
2346 * Process a widget drop.
2347 *
2348 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002352 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2353 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002357 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002358 mPendingAddInfo.minSpanX = info.minSpanX;
2359 mPendingAddInfo.minSpanY = info.minSpanY;
2360
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mPendingAddInfo.cellX = cell[0];
2363 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002364 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002365 if (span != null) {
2366 mPendingAddInfo.spanX = span[0];
2367 mPendingAddInfo.spanY = span[1];
2368 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369
Adam Cohened66b2b2012-01-23 17:28:51 -08002370 AppWidgetHostView hostView = info.boundWidget;
2371 int appWidgetId;
2372 if (hostView != null) {
2373 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002374 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002375 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002376 // In this case, we either need to start an activity to get permission to bind
2377 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002379 Bundle options = info.bindOptions;
2380
Sunny Goyalffe83f12014-08-14 17:39:34 -07002381 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2382 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002383 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002384 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002385 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002387 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2388 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002390 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2391 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002392 // TODO: we need to make sure that this accounts for the options bundle.
2393 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2395 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002396 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002397 }
2398
Joe Onoratodeb98af2010-02-19 14:59:39 -08002399 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002400 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 }
2402
Winson Chung24ab2f12010-09-16 14:10:47 -07002403 void processWallpaper(Intent intent) {
2404 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2405 }
2406
Adam Cohendcd297f2013-06-18 13:13:40 -07002407 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002409 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 folderInfo.title = getText(R.string.folder_name);
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002413 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2414 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002415 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416
2417 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 FolderIcon newFolder =
2419 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002420 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002422 // Force measure the new folder icon
2423 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2424 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002425 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 }
Romain Guycbb89e42009-06-08 15:52:54 -07002427
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002429 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002430 }
2431
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002432 /**
2433 * Registers various content observers. The current implementation registers
2434 * only a favorites observer to keep track of the favorites applications.
2435 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002436 private void registerContentObservers() {
2437 ContentResolver resolver = getContentResolver();
2438 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2439 true, mWidgetObserver);
2440 }
2441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 @Override
2443 public boolean dispatchKeyEvent(KeyEvent event) {
2444 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2445 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002447 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002448 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002449 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002450 dumpState();
2451 return true;
2452 }
2453 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 }
2455 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2456 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002457 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 return true;
2459 }
2460 }
2461
2462 return super.dispatchKeyEvent(event);
2463 }
2464
Joe Onorato88ec0992009-11-19 13:16:06 -08002465 @Override
2466 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002467 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2468 return;
2469 }
2470
Winson Chung3d9490a2015-03-24 17:38:42 -07002471 LauncherAccessibilityDelegate delegate =
2472 LauncherAppState.getInstance().getAccessibilityDelegate();
2473 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002474 return;
2475 }
2476
Winson Chungb745afb2015-03-02 11:51:23 -08002477 if (isAppsViewVisible()) {
2478 showWorkspace(true);
2479 } else if (isWidgetsViewVisible()) {
2480 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002481 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002482 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002484 Folder openFolder = mWorkspace.getOpenFolder();
2485 if (openFolder.isEditingName()) {
2486 openFolder.dismissEditingName();
2487 } else {
2488 closeFolder();
2489 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002491 mWorkspace.exitWidgetResizeMode();
2492
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002493 // Back button is a no-op here, but give at least some feedback for the button press
2494 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002495 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002499 * Re-listen when widgets are reset.
2500 */
Adam Cohen091440a2015-03-18 14:16:05 -07002501 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002502 if (mAppWidgetHost != null) {
2503 mAppWidgetHost.startListening();
2504 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 }
2506
2507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * Launches the intent referred by the clicked shortcut.
2509 *
2510 * @param v The view representing the clicked shortcut.
2511 */
2512 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2514 // view has detached (it's possible for this to happen if the view is removed mid touch).
2515 if (v.getWindowToken() == null) {
2516 return;
2517 }
2518
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002519 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 return;
2521 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002522
Adam Cohen1697b792013-09-17 19:08:21 -07002523 if (v instanceof Workspace) {
2524 if (mWorkspace.isInOverviewMode()) {
2525 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 return;
2528 }
2529
2530 if (v instanceof CellLayout) {
2531 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002532 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 }
2535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002537 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002540 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002543 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002545 } else if (tag instanceof AppInfo) {
2546 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 } else if (tag instanceof LauncherAppWidgetInfo) {
2548 if (v instanceof PendingAppWidgetHostView) {
2549 onClickPendingWidget((PendingAppWidgetHostView) v);
2550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553
Sunny Goyal508da152014-08-14 10:53:27 -07002554 public void onClickPagedViewIcon(View v) {
2555 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002556 if (mLauncherCallbacks != null) {
2557 mLauncherCallbacks.onClickPagedViewIcon(v);
2558 }
Sunny Goyal508da152014-08-14 10:53:27 -07002559 }
2560
Michael Jurka0e260592010-06-30 17:07:39 -07002561 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002562 return false;
2563 }
2564
Michael Jurkaaf442092010-06-10 17:01:57 -07002565 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002566 * Event handler for the app widget view which has not fully restored.
2567 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002569 if (mIsSafeModeEnabled) {
2570 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2571 return;
2572 }
2573
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002575 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002576 int widgetId = info.appWidgetId;
2577 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2578 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002579 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2580 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002581 mPendingAddInfo.copyFrom(info);
2582 mPendingAddWidgetId = widgetId;
2583
Sunny Goyalffe83f12014-08-14 17:39:34 -07002584 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2585 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002586 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002587 } else if (info.installProgress < 0) {
2588 // The install has not been queued
2589 final String packageName = info.providerName.getPackageName();
2590 showBrokenAppInstallDialog(packageName,
2591 new DialogInterface.OnClickListener() {
2592 public void onClick(DialogInterface dialog, int id) {
2593 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2594 }
2595 });
2596 } else {
2597 // Download has started.
2598 final String packageName = info.providerName.getPackageName();
2599 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002600 }
2601 }
2602
2603 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002604 * Event handler for the "grid" button that appears on the home screen, which
2605 * enters all apps mode.
2606 *
2607 * @param v The view that was clicked.
2608 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002609 protected void onClickAllAppsButton(View v) {
2610 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002611 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002612 showWorkspace(true);
2613 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002614 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 }
Adam Cohen9211d422014-10-07 18:14:53 -07002616 if (mLauncherCallbacks != null) {
2617 mLauncherCallbacks.onClickAllAppsButton(v);
2618 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 }
2620
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002621 private void showBrokenAppInstallDialog(final String packageName,
2622 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002623 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002624 .setTitle(R.string.abandoned_promises_title)
2625 .setMessage(R.string.abandoned_promise_explanation)
2626 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2627 .setNeutralButton(R.string.abandoned_clean_this,
2628 new DialogInterface.OnClickListener() {
2629 public void onClick(DialogInterface dialog, int id) {
2630 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2631 mWorkspace.removeAbandonedPromise(packageName, user);
2632 }
2633 })
2634 .create().show();
2635 return;
2636 }
2637
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638 /**
2639 * Event handler for an app shortcut click.
2640 *
2641 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2642 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002643 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2645 Object tag = v.getTag();
2646 if (!(tag instanceof ShortcutInfo)) {
2647 throw new IllegalArgumentException("Input must be a Shortcut");
2648 }
2649
2650 // Open shortcut
2651 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002652
2653 if (shortcut.isDisabled != 0) {
2654 int error = R.string.activity_not_available;
2655 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2656 error = R.string.safemode_shortcut_error;
2657 }
2658 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2659 return;
2660 }
2661
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002662 final Intent intent = shortcut.intent;
2663
2664 // Check for special shortcuts
2665 if (intent.getComponent() != null) {
2666 final String shortcutClass = intent.getComponent().getClassName();
2667
2668 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2669 MemoryDumpActivity.startDump(this);
2670 return;
2671 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2672 toggleShowWeightWatcher();
2673 return;
2674 }
2675 }
2676
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002678 if ((v instanceof BubbleTextView)
2679 && shortcut.isPromise()
2680 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002681 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002682 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 new DialogInterface.OnClickListener() {
2684 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002685 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 return;
2689 }
2690
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002692 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002693
2694 if (mLauncherCallbacks != null) {
2695 mLauncherCallbacks.onClickAppShortcut(v);
2696 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 }
2698
Adam Cohen091440a2015-03-18 14:16:05 -07002699 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002701 final ShortcutInfo shortcut;
2702 final Intent intent;
2703 if (tag instanceof ShortcutInfo) {
2704 shortcut = (ShortcutInfo) tag;
2705 intent = shortcut.intent;
2706 int[] pos = new int[2];
2707 v.getLocationOnScreen(pos);
2708 intent.setSourceBounds(new Rect(pos[0], pos[1],
2709 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002710
Sunny Goyal508da152014-08-14 10:53:27 -07002711 } else if (tag instanceof AppInfo) {
2712 shortcut = null;
2713 intent = ((AppInfo) tag).intent;
2714 } else {
2715 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2716 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717
2718 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 mStats.recordLaunch(intent, shortcut);
2720
2721 if (success && v instanceof BubbleTextView) {
2722 mWaitingForResume = (BubbleTextView) v;
2723 mWaitingForResume.setStayPressed(true);
2724 }
2725 }
2726
2727 /**
2728 * Event handler for a folder icon click.
2729 *
2730 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2731 */
2732 protected void onClickFolderIcon(View v) {
2733 if (LOGD) Log.d(TAG, "onClickFolder");
2734 if (!(v instanceof FolderIcon)){
2735 throw new IllegalArgumentException("Input must be a FolderIcon");
2736 }
2737
2738 FolderIcon folderIcon = (FolderIcon) v;
2739 final FolderInfo info = folderIcon.getFolderInfo();
2740 Folder openFolder = mWorkspace.getFolderForTag(info);
2741
2742 // If the folder info reports that the associated folder is open, then verify that
2743 // it is actually opened. There have been a few instances where this gets out of sync.
2744 if (info.opened && openFolder == null) {
2745 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2746 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2747 info.opened = false;
2748 }
2749
2750 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2751 // Close any open folder
2752 closeFolder();
2753 // Open the requested folder
2754 openFolder(folderIcon);
2755 } else {
2756 // Find the open folder...
2757 int folderScreen;
2758 if (openFolder != null) {
2759 folderScreen = mWorkspace.getPageForView(openFolder);
2760 // .. and close it
2761 closeFolder(openFolder);
2762 if (folderScreen != mWorkspace.getCurrentPage()) {
2763 // Close any folder open on the current screen
2764 closeFolder();
2765 // Pull the folder onto this screen
2766 openFolder(folderIcon);
2767 }
2768 }
2769 }
Adam Cohen9211d422014-10-07 18:14:53 -07002770
2771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onClickFolderIcon(v);
2773 }
Michael Jurka5130e402011-10-13 04:55:35 -07002774 }
2775
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002776 /**
2777 * Event handler for the (Add) Widgets button that appears after a long press
2778 * on the home screen.
2779 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002780 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002782 if (mIsSafeModeEnabled) {
2783 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2784 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002785 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onClickAddWidgetButton(view);
2788 }
Adam Cohen9211d422014-10-07 18:14:53 -07002789 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002790 }
2791
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 /**
2793 * Event handler for the wallpaper picker button that appears after a long press
2794 * on the home screen.
2795 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002796 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002798 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2799 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002800
2801 if (mLauncherCallbacks != null) {
2802 mLauncherCallbacks.onClickWallpaperPicker(v);
2803 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 }
2805
2806 /**
2807 * Event handler for a click on the settings button that appears after a long press
2808 * on the home screen.
2809 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002810 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002812 if (mLauncherCallbacks != null) {
2813 mLauncherCallbacks.onClickSettingsButton(v);
2814 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002815 }
2816
Michael Jurka5130e402011-10-13 04:55:35 -07002817 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002818 // Provide the same haptic feedback that the system offers for virtual keys.
2819 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002820 }
2821
Adam Cohen61f560d2013-09-30 15:58:20 -07002822 public void performHapticFeedbackOnTouchDown(View v) {
2823 // Provide the same haptic feedback that the system offers for virtual keys.
2824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2825 }
2826
2827 public View.OnTouchListener getHapticFeedbackTouchListener() {
2828 if (mHapticFeedbackTouchListener == null) {
2829 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2830 @Override
2831 public boolean onTouch(View v, MotionEvent event) {
2832 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2834 }
2835 return false;
2836 }
2837 };
2838 }
2839 return mHapticFeedbackTouchListener;
2840 }
2841
Adam Cohen9211d422014-10-07 18:14:53 -07002842 public void onDragStarted(View view) {
2843 if (isOnCustomContent()) {
2844 // Custom content screen doesn't participate in drag and drop. If on custom
2845 // content screen, move to default.
2846 moveWorkspaceToDefaultScreen();
2847 }
2848
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onDragStarted(view);
2851 }
2852 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002853
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002854 /**
2855 * Called when the user stops interacting with the launcher.
2856 * This implies that the user is now on the homescreen and is not doing housekeeping.
2857 */
Adam Cohen9211d422014-10-07 18:14:53 -07002858 protected void onInteractionEnd() {
2859 if (mLauncherCallbacks != null) {
2860 mLauncherCallbacks.onInteractionEnd();
2861 }
2862 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863
2864 /**
2865 * Called when the user starts interacting with the launcher.
2866 * The possible interactions are:
2867 * - open all apps
2868 * - reorder an app shortcut, or a widget
2869 * - open the overview mode.
2870 * This is a good time to stop doing things that only make sense
2871 * when the user is on the homescreen and not doing housekeeping.
2872 */
Adam Cohen9211d422014-10-07 18:14:53 -07002873 protected void onInteractionBegin() {
2874 if (mLauncherCallbacks != null) {
2875 mLauncherCallbacks.onInteractionBegin();
2876 }
2877 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002878
Kenny Guyf07af7b2014-07-31 11:39:16 +01002879 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002880 try {
2881 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002882 launcherApps.showAppDetailsForProfile(componentName, user);
2883 } catch (SecurityException e) {
2884 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2885 Log.e(TAG, "Launcher does not have permission to launch settings");
2886 } catch (ActivityNotFoundException e) {
2887 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2888 Log.e(TAG, "Unable to launch settings");
2889 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002890 }
2891
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002893 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2894 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002895 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 // System applications cannot be installed. For now, show a toast explaining that.
2897 // We may give them the option of disabling apps this way.
2898 int messageId = R.string.uninstall_system_app_text;
2899 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002902 String packageName = componentName.getPackageName();
2903 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002904 Intent intent = new Intent(
2905 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002906 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2907 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002908 if (user != null) {
2909 user.addToIntent(intent, Intent.EXTRA_USER);
2910 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002911 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002912 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002913 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002914 }
2915
Michael Jurka86a720e2012-05-09 11:23:23 -07002916 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002917 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002919 // Only launch using the new animation if the shortcut has not opted out (this is a
2920 // private contract between launcher and may be ignored in the future).
2921 boolean useLaunchAnimation = (v != null) &&
2922 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002923 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2924 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002925
Kenny Guy1317e2d2014-05-08 18:52:50 +01002926 UserHandleCompat user = null;
2927 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2928 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2929 user = userManager.getUserForSerialNumber(serialNumber);
2930 }
2931
2932 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002933 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002934 ActivityOptions opts = null;
2935 if (sClipRevealMethod != null) {
2936 // TODO: call method directly when Launcher3 can depend on M APIs
2937 int left = 0, top = 0;
2938 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2939 if (v instanceof TextView) {
2940 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002941 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2942 if (icon != null) {
2943 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002944 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002945 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002946 width = bounds.width();
2947 height = bounds.height();
2948 }
2949 }
2950 try {
2951 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2952 left, top, width, height);
2953 } catch (IllegalAccessException e) {
2954 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2955 sClipRevealMethod = null;
2956 } catch (InvocationTargetException e) {
2957 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2958 sClipRevealMethod = null;
2959 }
2960 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002961 if (opts == null && !Utilities.isLmpOrAbove()) {
2962 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002963 v.getMeasuredWidth(), v.getMeasuredHeight());
2964 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002965 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002966 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002967
2968 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2969 // Could be launching some bookkeeping activity
2970 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002971 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002972 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002973 launcherApps.startActivityForProfile(intent.getComponent(), user,
2974 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002975 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002976 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 } catch (SecurityException e) {
2978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002979 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002981 "or use the exported attribute for this activity. "
2982 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002984 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002986
Michael Jurka86a720e2012-05-09 11:23:23 -07002987 boolean startActivitySafely(View v, Intent intent, Object tag) {
2988 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002989 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2990 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2991 return false;
2992 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002993 try {
2994 success = startActivity(v, intent, tag);
2995 } catch (ActivityNotFoundException e) {
2996 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2997 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2998 }
2999 return success;
3000 }
3001
Adam Cohen268c4752012-06-06 17:47:33 -07003002 /**
3003 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3004 * in the DragLayer in the exact absolute location of the original FolderIcon.
3005 */
3006 private void copyFolderIconToImage(FolderIcon fi) {
3007 final int width = fi.getMeasuredWidth();
3008 final int height = fi.getMeasuredHeight();
3009
3010 // Lazy load ImageView, Bitmap and Canvas
3011 if (mFolderIconImageView == null) {
3012 mFolderIconImageView = new ImageView(this);
3013 }
3014 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3015 mFolderIconBitmap.getHeight() != height) {
3016 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3017 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3018 }
3019
3020 DragLayer.LayoutParams lp;
3021 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3022 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3023 } else {
3024 lp = new DragLayer.LayoutParams(width, height);
3025 }
3026
Adam Cohen307fe232012-08-16 17:55:58 -07003027 // The layout from which the folder is being opened may be scaled, adjust the starting
3028 // view size by this scale factor.
3029 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003030 lp.customPosition = true;
3031 lp.x = mRectForFolderAnimation.left;
3032 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003033 lp.width = (int) (scale * width);
3034 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003035
3036 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3037 fi.draw(mFolderIconCanvas);
3038 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003039 if (fi.getFolder() != null) {
3040 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3041 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003042 }
Adam Cohen268c4752012-06-06 17:47:33 -07003043 // Just in case this image view is still in the drag layer from a previous animation,
3044 // we remove it and re-add it.
3045 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3046 mDragLayer.removeView(mFolderIconImageView);
3047 }
3048 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003049 if (fi.getFolder() != null) {
3050 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003051 }
Adam Cohen268c4752012-06-06 17:47:33 -07003052 }
3053
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003055 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3057 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3058 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3059
Adam Cohenc51934b2011-07-26 21:07:43 -07003060 FolderInfo info = (FolderInfo) fi.getTag();
3061 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3062 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003063 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3064 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003065 }
3066
Adam Cohen268c4752012-06-06 17:47:33 -07003067 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3068 copyFolderIconToImage(fi);
3069 fi.setVisibility(View.INVISIBLE);
3070
Michael Jurka2ecf9952012-06-18 12:52:28 -07003071 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003072 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003073 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003074 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3075 }
3076 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003077 oa.start();
3078 }
3079
Adam Cohen268c4752012-06-06 17:47:33 -07003080 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003081 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003082 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3083 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3084 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3085
Adam Cohen268c4752012-06-06 17:47:33 -07003086 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003087
Adam Cohen268c4752012-06-06 17:47:33 -07003088 // We remove and re-draw the FolderIcon in-case it has changed
3089 mDragLayer.removeView(mFolderIconImageView);
3090 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003091 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003092 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003093 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003094 oa.addListener(new AnimatorListenerAdapter() {
3095 @Override
3096 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003097 if (cl != null) {
3098 cl.clearFolderLeaveBehind();
3099 // Remove the ImageView copy of the FolderIcon and make the original visible.
3100 mDragLayer.removeView(mFolderIconImageView);
3101 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003102 }
3103 }
3104 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003105 oa.start();
3106 }
3107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003109 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 * is animated relative to the specified View. If the View is null, no animation
3111 * is played.
3112 *
3113 * @param folderInfo The FolderInfo describing the folder to open.
3114 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003115 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003116 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003117 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Adam Cohena9cf38f2011-05-02 15:36:58 -07003119 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120
Adam Cohen4554ee12011-08-03 16:13:21 -07003121 // Just verify that the folder hasn't already been added to the DragLayer.
3122 // There was a one-off crash where the folder had a parent already.
3123 if (folder.getParent() == null) {
3124 mDragLayer.addView(folder);
3125 mDragController.addDropTarget((DropTarget) folder);
3126 } else {
3127 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3128 folder.getParent() + ").");
3129 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003130 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003131 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003132
3133 // Notify the accessibility manager that this folder "window" has appeared and occluded
3134 // the workspace items
3135 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3136 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003137 }
3138
3139 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003140 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003141 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003142 if (folder.isEditingName()) {
3143 folder.dismissEditingName();
3144 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003145 closeFolder(folder);
3146 }
3147 }
3148
3149 void closeFolder(Folder folder) {
3150 folder.getInfo().opened = false;
3151
3152 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3153 if (parent != null) {
3154 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3155 shrinkAndFadeInFolderIcon(fi);
3156 }
3157 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003158
3159 // Notify the accessibility manager that this folder "window" has disappeard and no
3160 // longer occludeds the workspace items
3161 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003165 if (!isDraggingEnabled()) return false;
3166 if (isWorkspaceLocked()) return false;
3167 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168
Adam Cohen1697b792013-09-17 19:08:21 -07003169 if (v instanceof Workspace) {
3170 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003171 if (mWorkspace.enterOverviewMode()) {
3172 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3173 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3174 return true;
3175 } else {
3176 return false;
3177 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003178 } else {
3179 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003180 }
Adam Cohen1697b792013-09-17 19:08:21 -07003181 }
3182
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003183 CellLayout.CellInfo longClickCellInfo = null;
3184 View itemUnderLongClick = null;
3185 if (v.getTag() instanceof ItemInfo) {
3186 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003187 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003188 itemUnderLongClick = longClickCellInfo.cell;
3189 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 }
3191
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 // The hotseat touch handling does not go through Workspace, and we always allow long press
3193 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003194 final boolean inHotseat = isHotseatLayout(v);
3195 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003197 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003198 // User long pressed on empty space
3199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003201 if (mWorkspace.isInOverviewMode()) {
3202 mWorkspace.startReordering(v);
3203 } else {
3204 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003207 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3208 mHotseat.getOrderInHotseat(
3209 longClickCellInfo.cellX,
3210 longClickCellInfo.cellY));
3211 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003213 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 }
3215 }
3216 }
3217 return true;
3218 }
3219
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 boolean isHotseatLayout(View layout) {
3221 return mHotseat != null && layout != null &&
3222 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3223 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003224
Winson Chung3d503fb2011-07-13 17:25:49 -07003225 /**
3226 * Returns the CellLayout of the specified container at the specified screen.
3227 */
Sunny Goyal92820592015-03-02 11:31:35 -08003228 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003229 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3230 if (mHotseat != null) {
3231 return mHotseat.getLayout();
3232 } else {
3233 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003234 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003236 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003237 }
3238 }
3239
Winson Chungb745afb2015-03-02 11:51:23 -08003240 /**
3241 * For overridden classes.
3242 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003243 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003244 return isAppsViewVisible();
3245 }
3246
3247 public boolean isAppsViewVisible() {
3248 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3249 }
3250
3251 public boolean isWidgetsViewVisible() {
3252 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003253 }
3254
Craig Mautner360310b2012-10-26 15:13:08 -07003255 private void setWorkspaceBackground(boolean workspace) {
3256 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003257 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003258 }
3259
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003260 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003261 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3262 int curflags = getWindow().getAttributes().flags
3263 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3264 if (wpflags != curflags) {
3265 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3266 }
Craig Mautner360310b2012-10-26 15:13:08 -07003267 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003268 }
3269
Michael Jurkae326f182011-11-21 14:05:46 -08003270 @Override
3271 public void onTrimMemory(int level) {
3272 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003273 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3274 // The widget preview db can result in holding onto over
3275 // 3MB of memory for caching which isn't necessary.
3276 SQLiteDatabase.releaseMemory();
3277
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003278 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003279 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003280 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003281 }
Michael Jurkae326f182011-11-21 14:05:46 -08003282 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003283 if (mLauncherCallbacks != null) {
3284 mLauncherCallbacks.onTrimMemory(level);
3285 }
Michael Jurkae326f182011-11-21 14:05:46 -08003286 }
3287
Winson Chungb745afb2015-03-02 11:51:23 -08003288 @Override
3289 public void onStateTransitionHideSearchBar() {
3290 // Hide the search bar
3291 if (mSearchDropTargetBar != null) {
3292 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3293 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003294 }
3295
Winson Chungb745afb2015-03-02 11:51:23 -08003296 protected void showWorkspace(boolean animated) {
3297 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003298 }
3299
Adam Cohened66b2b2012-01-23 17:28:51 -08003300 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003301 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003302 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003304 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3305 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306
Winson Chungc7d2b602012-05-16 17:02:20 -07003307 // Show the search bar (only animate if we were showing the drop target bar in spring
3308 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003309 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003310 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003311 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003312
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 // Set focus to the AppsCustomize button
3314 if (mAllAppsButton != null) {
3315 mAllAppsButton.requestFocus();
3316 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003317 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003318
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003319 // Change the state *after* we've called all the transition code
3320 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003321
Winson Chung5fb63472011-02-02 17:03:37 -08003322 // Resume the auto-advance of widgets
3323 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003324 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003325
alanv1d4fde62012-10-17 13:15:47 -07003326 // Send an accessibility event to announce the context change
3327 getWindow().getDecorView()
3328 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003329
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003330 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003331 }
3332
Adam Cohened307df2013-10-02 09:37:31 -07003333 void showOverviewMode(boolean animated) {
3334 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003335 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3336 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003337 mState = State.WORKSPACE;
3338 onWorkspaceShown(animated);
3339 }
3340
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003341 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003342 }
3343
Winson Chungb745afb2015-03-02 11:51:23 -08003344 /**
3345 * Shows the apps view.
3346 */
3347 void showAppsView(boolean animated, boolean resetListToTop) {
3348 if (resetListToTop) {
3349 mAppsView.scrollToTop();
3350 }
3351 showAppsOrWidgets(animated, State.APPS);
3352 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353
Winson Chungb745afb2015-03-02 11:51:23 -08003354 /**
3355 * Shows the widgets view.
3356 */
3357 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003358 if (resetPageToZero) {
3359 mAppsCustomizeTabHost.reset();
3360 }
Winson Chungb745afb2015-03-02 11:51:23 -08003361 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003362 mAppsCustomizeTabHost.post(new Runnable() {
3363 @Override
3364 public void run() {
3365 // We post this in-case the all apps view isn't yet constructed.
3366 mAppsCustomizeTabHost.requestFocus();
3367 }
3368 });
Winson Chungb745afb2015-03-02 11:51:23 -08003369 }
3370
3371 /**
3372 * Sets up the transition to show the apps/widgets view.
3373 */
3374 private void showAppsOrWidgets(boolean animated, State toState) {
3375 if (mState != State.WORKSPACE) return;
3376 if (toState != State.APPS && toState != State.WIDGETS) return;
3377
3378 if (toState == State.APPS) {
3379 mStateTransitionAnimation.startAnimationToAllApps(animated);
3380 } else {
3381 mStateTransitionAnimation.startAnimationToWidgets(animated);
3382 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003385 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003386
3387 // Pause the auto-advance of widgets until we are out of AllApps
3388 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003389 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390 closeFolder();
3391
3392 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003393 getWindow().getDecorView()
3394 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003395 }
3396
Adam Cohen7777d962011-08-18 18:58:38 -07003397 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003398 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3399 mState == State.WIDGETS_SPRING_LOADED) {
3400 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003401 }
Winson Chungb745afb2015-03-02 11:51:23 -08003402
3403 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3404 true /* animated */, null /* onCompleteRunnable */);
3405 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003406 }
Adam Cohen7777d962011-08-18 18:58:38 -07003407
Adam Cohenad4e15c2013-10-17 16:21:35 -07003408 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003409 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003410 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003411
Winson Chunge7a03942011-08-05 15:05:12 -07003412 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003413 @Override
3414 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003415 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003416 // Before we show workspace, hide all apps again because
3417 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3418 // clean up our state transition functions
3419 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003420 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003421 } else {
3422 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003423 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003424 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003425 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003426 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427
Michael Jurkad3ef3062010-11-23 16:23:58 -08003428 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003429 if (mState == State.APPS_SPRING_LOADED) {
3430 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3431 mState = State.APPS;
3432 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3433 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3434 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003435 }
3436 // Otherwise, we are not in spring loaded mode, so don't do anything.
3437 }
3438
Michael Jurkab3e22d92011-10-31 15:58:33 -07003439 void lockAllApps() {
3440 // TODO
3441 }
3442
3443 void unlockAllApps() {
3444 // TODO
3445 }
3446
Sunny Goyal594d76d2014-11-06 10:12:54 -08003447 protected void disableVoiceButtonProxy(boolean disable) {
3448 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003449 }
3450
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003451 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003452 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3453 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003454 }
3455
Adam Cohen24ce0b32014-01-14 16:18:14 -08003456 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003457 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3458 if (searchProvider == null) {
3459 return null;
3460 }
3461
3462 Bundle opts = new Bundle();
3463 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003464 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003465
3466 SharedPreferences sp = getSharedPreferences(
3467 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3468 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003469 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003470 if (!searchProvider.provider.flattenToString().equals(
3471 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003472 || (widgetInfo == null)
3473 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003474 // A valid widget is not already bound.
3475 if (widgetId > -1) {
3476 mAppWidgetHost.deleteAppWidgetId(widgetId);
3477 widgetId = -1;
3478 }
3479
3480 // Try to bind a new widget
3481 widgetId = mAppWidgetHost.allocateAppWidgetId();
3482
3483 if (!AppWidgetManagerCompat.getInstance(this)
3484 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3485 mAppWidgetHost.deleteAppWidgetId(widgetId);
3486 widgetId = -1;
3487 }
3488
3489 sp.edit()
3490 .putInt(QSB_WIDGET_ID, widgetId)
3491 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3492 .commit();
3493 }
3494
3495 if (widgetId != -1) {
3496 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3497 mQsb.updateAppWidgetOptions(opts);
3498 mQsb.setPadding(0, 0, 0, 0);
3499 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003500 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003501 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003502 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003503 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003504 }
3505
Sunny Goyal22235bc2015-04-03 09:23:43 -07003506 private void reinflateQSBIfNecessary() {
3507 if (mQsb instanceof LauncherAppWidgetHostView &&
3508 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3509 mSearchDropTargetBar.removeView(mQsb);
3510 mQsb = null;
3511 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3512 }
3513 }
3514
Michael Jurkad7c28052012-04-27 15:43:36 -07003515 @Override
3516 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003517 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003518 final List<CharSequence> text = event.getText();
3519 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003520 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003521 if (mState == State.APPS) {
3522 text.add("Apps");
3523 } else if (mState == State.WIDGETS) {
3524 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003525 } else {
3526 text.add(getString(R.string.all_apps_home_button_label));
3527 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003528 return result;
3529 }
3530
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003531 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003532 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003533 */
Adam Cohen091440a2015-03-18 14:16:05 -07003534 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003535 @Override
3536 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003537 closeSystemDialogs();
3538 }
3539 }
3540
3541 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003542 * Receives notifications whenever the appwidgets are reset.
3543 */
3544 private class AppWidgetResetObserver extends ContentObserver {
3545 public AppWidgetResetObserver() {
3546 super(new Handler());
3547 }
3548
3549 @Override
3550 public void onChange(boolean selfChange) {
3551 onAppWidgetReset();
3552 }
3553 }
3554
3555 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003556 * If the activity is currently paused, signal that we need to run the passed Runnable
3557 * in onResume.
3558 *
3559 * This needs to be called from incoming places where resources might have been loaded
3560 * while we are paused. That is becaues the Configuration might be wrong
3561 * when we're not running, and if it comes back to what it was when we
3562 * were paused, we are not restarted.
3563 *
3564 * Implementation of the method from LauncherModel.Callbacks.
3565 *
3566 * @return true if we are currently paused. The caller might be able to
3567 * skip some work in that case since we will come back again.
3568 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003569 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003570 if (mPaused) {
3571 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003572 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003573 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003574 }
3575 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003576 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003577 return true;
3578 } else {
3579 return false;
3580 }
3581 }
3582
Michael Jurkac402cd92013-05-20 15:49:32 +02003583 private boolean waitUntilResume(Runnable run) {
3584 return waitUntilResume(run, false);
3585 }
3586
Michael Jurka1e2f4652013-07-08 18:03:46 -07003587 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003588 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003589 }
3590
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003592 * If the activity is currently paused, signal that we need to re-run the loader
3593 * in onResume.
3594 *
3595 * This needs to be called from incoming places where resources might have been loaded
3596 * while we are paused. That is becaues the Configuration might be wrong
3597 * when we're not running, and if it comes back to what it was when we
3598 * were paused, we are not restarted.
3599 *
3600 * Implementation of the method from LauncherModel.Callbacks.
3601 *
3602 * @return true if we are currently paused. The caller might be able to
3603 * skip some work in that case since we will come back again.
3604 */
3605 public boolean setLoadOnResume() {
3606 if (mPaused) {
3607 Log.i(TAG, "setLoadOnResume");
3608 mOnResumeNeedsLoad = true;
3609 return true;
3610 } else {
3611 return false;
3612 }
3613 }
3614
3615 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003617 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003619 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003620 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003621 } else {
3622 return SCREEN_COUNT / 2;
3623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003625
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 /**
3627 * Refreshes the shortcuts shown on the workspace.
3628 *
3629 * Implementation of the method from LauncherModel.Callbacks.
3630 */
3631 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003632 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003633
Michael Jurka7607c2f2013-04-03 14:33:19 -07003634 // If we're starting binding all over again, clear any bind calls we'd postponed in
3635 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3636 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003637 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003638
Winson Chungd64d1762013-08-20 14:37:16 -07003639 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003640 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003641 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003642
Michael Jurka05bf6442011-11-30 20:28:53 -08003643 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003644 if (mHotseat != null) {
3645 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003646 }
3647 }
3648
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 @Override
3650 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003651 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003652
Adam Cohen5084cba2013-09-03 12:01:16 -07003653 // If there are no screens, we need to have an empty screen
3654 if (orderedScreenIds.size() == 0) {
3655 mWorkspace.addExtraEmptyScreen();
3656 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003657
3658 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003659 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003660 if (hasCustomContentToLeft()) {
3661 mWorkspace.createCustomContentContainer();
3662 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003663 }
Winson Chung64359a52013-07-08 17:17:08 -07003664 }
3665
3666 @Override
3667 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003668 // Log to disk
3669 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3670 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3671 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003672 int count = orderedScreenIds.size();
3673 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003674 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003675 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003676 }
3677
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003678 @Override
3679 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3680 final long screenId, final int[] cell, final int spanX, final int spanY) {
3681 showWorkspace(true, new Runnable() {
3682
3683 @Override
3684 public void run() {
3685 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3686 addPendingItem(info, container, screenId, cell, spanX, spanY);
3687 }
3688 });
3689 }
3690
Adam Cohen39a06042013-07-19 14:30:12 -07003691 private boolean shouldShowWeightWatcher() {
3692 String spKey = LauncherAppState.getSharedPreferencesKey();
3693 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003694 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003695
3696 return show;
3697 }
3698
3699 private void toggleShowWeightWatcher() {
3700 String spKey = LauncherAppState.getSharedPreferencesKey();
3701 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3702 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3703
3704 show = !show;
3705
3706 SharedPreferences.Editor editor = sp.edit();
3707 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3708 editor.commit();
3709
3710 if (mWeightWatcher != null) {
3711 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3712 }
3713 }
3714
Winson Chungd64d1762013-08-20 14:37:16 -07003715 public void bindAppsAdded(final ArrayList<Long> newScreens,
3716 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003717 final ArrayList<ItemInfo> addAnimated,
3718 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003719 Runnable r = new Runnable() {
3720 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003721 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003722 }
3723 };
3724 if (waitUntilResume(r)) {
3725 return;
3726 }
3727
Winson Chungd64d1762013-08-20 14:37:16 -07003728 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003729 if (newScreens != null) {
3730 bindAddScreens(newScreens);
3731 }
Winson Chungd64d1762013-08-20 14:37:16 -07003732
3733 // We add the items without animation on non-visible pages, and with
3734 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003735 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 bindItems(addNotAnimated, 0,
3737 addNotAnimated.size(), false);
3738 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003739 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 bindItems(addAnimated, 0,
3741 addAnimated.size(), true);
3742 }
Winson Chungc58497e2013-09-03 17:48:37 -07003743
Winson Chung87412982013-10-03 18:34:14 -07003744 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003745 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003746
Winson Chungb745afb2015-03-02 11:51:23 -08003747 if (addedApps != null && mAppsView != null) {
3748 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003749 }
Winson Chungd64d1762013-08-20 14:37:16 -07003750 }
3751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003752 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 * Bind the items start-end from the list.
3754 *
3755 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003756 */
Winson Chung64359a52013-07-08 17:17:08 -07003757 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3758 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003759 Runnable r = new Runnable() {
3760 public void run() {
3761 bindItems(shortcuts, start, end, forceAnimateIcons);
3762 }
3763 };
3764 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003765 return;
3766 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003767
Winson Chungf0c6ae02012-03-21 16:10:31 -07003768 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003769 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3770 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003771 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003772 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003773 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003774 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003776
3777 // Short circuit if we are loading dock items for a configuration which has no dock
3778 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3779 mHotseat == null) {
3780 continue;
3781 }
3782
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 switch (item.itemType) {
3784 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3785 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003786 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003787 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003788
Winson Chung64359a52013-07-08 17:17:08 -07003789 /*
3790 * TODO: FIX collision case
3791 */
Winson Chungce376632013-07-11 16:12:41 -07003792 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3793 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3794 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003795 View v = cl.getChildAt(item.cellX, item.cellY);
3796 Object tag = v.getTag();
3797 String desc = "Collision while binding workspace item: " + item
3798 + ". Collides with " + tag;
3799 if (LauncherAppState.isDogfoodBuild()) {
3800 throw (new RuntimeException(desc));
3801 } else {
3802 Log.d(TAG, desc);
3803 }
Winson Chungce376632013-07-11 16:12:41 -07003804 }
Winson Chung64359a52013-07-08 17:17:08 -07003805 }
3806
Adam Cohendcd297f2013-06-18 13:13:40 -07003807 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3808 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003809 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003810 // Animate all the applications up now
3811 shortcut.setAlpha(0f);
3812 shortcut.setScaleX(0f);
3813 shortcut.setScaleY(0f);
3814 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003815 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003816 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003818 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003819 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003820 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003821 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003822 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3823 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003824 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003825 default:
3826 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003828 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003829
Winson Chung997a9232013-07-24 15:33:46 -07003830 if (animateIcons) {
3831 // Animate to the correct page
3832 if (newShortcutsScreenId > -1) {
3833 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003834 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003835 final Runnable startBounceAnimRunnable = new Runnable() {
3836 public void run() {
3837 anim.playTogether(bounceAnims);
3838 anim.start();
3839 }
3840 };
Winson Chung997a9232013-07-24 15:33:46 -07003841 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003842 // We post the animation slightly delayed to prevent slowdowns
3843 // when we are loading right after we return to launcher.
3844 mWorkspace.postDelayed(new Runnable() {
3845 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003846 if (mWorkspace != null) {
3847 mWorkspace.snapToPage(newScreenIndex);
3848 mWorkspace.postDelayed(startBounceAnimRunnable,
3849 NEW_APPS_ANIMATION_DELAY);
3850 }
Winson Chung94d67682013-09-25 16:29:40 -07003851 }
3852 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003853 } else {
3854 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003855 }
3856 }
Winson Chung64359a52013-07-08 17:17:08 -07003857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003859 }
3860
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 /**
3862 * Implementation of the method from LauncherModel.Callbacks.
3863 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003864 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003865 Runnable r = new Runnable() {
3866 public void run() {
3867 bindFolders(folders);
3868 }
3869 };
3870 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003871 return;
3872 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003873 sFolders.clear();
3874 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876
3877 /**
3878 * Add the views for a widget to the workspace.
3879 *
3880 * Implementation of the method from LauncherModel.Callbacks.
3881 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003883 Runnable r = new Runnable() {
3884 public void run() {
3885 bindAppWidget(item);
3886 }
3887 };
3888 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003889 return;
3890 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003891
Daniel Sandler843e8602010-06-07 14:59:01 -04003892 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3893 if (DEBUG_WIDGETS) {
3894 Log.d(TAG, "bindAppWidget: " + item);
3895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 final Workspace workspace = mWorkspace;
3897
Adam Cohen59400422014-03-05 18:07:04 -08003898 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003899 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003900
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003901 if (!mIsSafeModeEnabled
3902 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3903 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003904 if (appWidgetInfo == null) {
3905 if (DEBUG_WIDGETS) {
3906 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3907 + " belongs to component " + item.providerName
3908 + ", as the povider is null");
3909 }
3910 LauncherModel.deleteItemFromDatabase(this, item);
3911 return;
3912 }
3913 // Note: This assumes that the id remap broadcast is received before this step.
3914 // If that is not the case, the id remap will be ignored and user may see the
3915 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003916 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003917 pendingInfo.spanX = item.spanX;
3918 pendingInfo.spanY = item.spanY;
3919 pendingInfo.minSpanX = item.minSpanX;
3920 pendingInfo.minSpanY = item.minSpanY;
3921 Bundle options =
3922 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3923
Sunny Goyalff572272014-07-23 13:58:07 -07003924 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003925 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3926 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003927
3928 // TODO consider showing a permission dialog when the widget is clicked.
3929 if (!success) {
3930 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3931 if (DEBUG_WIDGETS) {
3932 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3933 + " belongs to component " + item.providerName
3934 + ", as the launcher is unable to bing a new widget id");
3935 }
3936 LauncherModel.deleteItemFromDatabase(this, item);
3937 return;
3938 }
3939
3940 item.appWidgetId = newWidgetId;
3941
3942 // If the widget has a configure activity, it is still needs to set it up, otherwise
3943 // the widget is ready to go.
3944 item.restoreStatus = (appWidgetInfo.configure == null)
3945 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3946 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3947
3948 LauncherModel.updateItemInDatabase(this, item);
3949 }
3950
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003951 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003952 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003953 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003954 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3955 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003956 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003957
Sunny Goyal651077b2014-06-30 14:15:31 -07003958 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3959 } else {
3960 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003961 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3962 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003963 view.updateIcon(mIconCache);
3964 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003965 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003966 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003970 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971
Adam Cohendcd297f2013-06-18 13:13:40 -07003972 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003974 if (!item.isCustomWidget()) {
3975 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3976 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003977
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 workspace.requestLayout();
3979
Daniel Sandler843e8602010-06-07 14:59:01 -04003980 if (DEBUG_WIDGETS) {
3981 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3982 + (SystemClock.uptimeMillis()-start) + "ms");
3983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 }
3985
Sunny Goyalff572272014-07-23 13:58:07 -07003986 /**
3987 * Restores a pending widget.
3988 *
3989 * @param appWidgetId The app widget id
3990 * @param cellInfo The position on screen where to create the widget.
3991 */
3992 private void completeRestoreAppWidget(final int appWidgetId) {
3993 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3994 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3995 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3996 return;
3997 }
3998
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003999 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004000 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4001
4002 mWorkspace.reinflateWidgetsIfNecessary();
4003 LauncherModel.updateItemInDatabase(this, info);
4004 }
4005
Adam Cohen1462de32012-07-24 22:34:36 -07004006 public void onPageBoundSynchronously(int page) {
4007 mSynchronouslyBoundPages.add(page);
4008 }
4009
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 /**
4011 * Callback saying that there aren't any more items to bind.
4012 *
4013 * Implementation of the method from LauncherModel.Callbacks.
4014 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004015 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004016 Runnable r = new Runnable() {
4017 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004018 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004019 }
4020 };
4021 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004022 return;
4023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 if (mSavedState != null) {
4025 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004026 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004028 mSavedState = null;
4029 }
4030
Adam Cohen1462de32012-07-24 22:34:36 -07004031 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004033 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004034 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004035
4036 // If we received the result of any pending adds while the loader was running (e.g. the
4037 // widget configuration forced an orientation change), process them now.
4038 if (sPendingAddItem != null) {
4039 final long screenId = completeAdd(sPendingAddItem);
4040
4041 // TODO: this moves the user to the page where the pending item was added. Ideally,
4042 // the screen would be guaranteed to exist after bind, and the page would be set through
4043 // the workspace restore process.
4044 mWorkspace.post(new Runnable() {
4045 @Override
4046 public void run() {
4047 mWorkspace.snapToScreenId(screenId);
4048 }
4049 });
4050 sPendingAddItem = null;
4051 }
4052
Sunny Goyale755d462014-07-22 13:48:29 -07004053 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004054
4055 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004056 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004057 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004058 }
4059
Adam Cohen3ed316a2014-07-23 18:21:20 -07004060 private void sendLoadingCompleteBroadcastIfNecessary() {
4061 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4062 String permission =
4063 getResources().getString(R.string.receive_first_load_broadcast_permission);
4064 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4065 sendBroadcast(intent, permission);
4066 SharedPreferences.Editor editor = mSharedPrefs.edit();
4067 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4068 editor.apply();
4069 }
4070 }
4071
Winson Chunga0b7e862013-09-05 16:03:15 -07004072 public boolean isAllAppsButtonRank(int rank) {
4073 if (mHotseat != null) {
4074 return mHotseat.isAllAppsButtonRank(rank);
4075 }
4076 return false;
4077 }
4078
Winson Chunga2413752012-04-03 14:22:34 -07004079 private boolean canRunNewAppsAnimation() {
4080 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4081 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4082 }
4083
Winson Chungc9168342013-06-26 14:54:55 -07004084 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4085 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4086 PropertyValuesHolder.ofFloat("alpha", 1f),
4087 PropertyValuesHolder.ofFloat("scaleX", 1f),
4088 PropertyValuesHolder.ofFloat("scaleY", 1f));
4089 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4090 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4091 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4092 return bounceAnim;
4093 }
4094
Adam Cohen27772732013-11-11 14:00:56 +00004095 public boolean useVerticalBarLayout() {
4096 return LauncherAppState.getInstance().getDynamicGrid().
4097 getDeviceProfile().isVerticalBarLayout();
4098 }
4099
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004100 protected Rect getSearchBarBounds() {
4101 return LauncherAppState.getInstance().getDynamicGrid().
4102 getDeviceProfile().getSearchBarBounds();
4103 }
4104
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004105 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004106 if (mSearchDropTargetBar == null) {
4107 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004108 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004109 if (mQsb != null) {
4110 mSearchDropTargetBar.removeView(mQsb);
4111 mQsb = null;
4112 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004113 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004114 }
4115
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004116 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 * Add the icons for all apps.
4118 *
4119 * Implementation of the method from LauncherModel.Callbacks.
4120 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004121 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004122 if (mAppsView != null) {
4123 mAppsView.setApps(apps);
4124 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004125 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004126 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Song70a48be2015-03-11 16:36:52 -07004127 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chung785d2eb2011-04-14 16:08:02 -07004128 }
Adam Cohen9211d422014-10-07 18:14:53 -07004129 if (mLauncherCallbacks != null) {
4130 mLauncherCallbacks.bindAllApplications(apps);
4131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004132 }
4133
4134 /**
4135 * A package was updated.
4136 *
4137 * Implementation of the method from LauncherModel.Callbacks.
4138 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004139 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004140 Runnable r = new Runnable() {
4141 public void run() {
4142 bindAppsUpdated(apps);
4143 }
4144 };
4145 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004146 return;
4147 }
4148
Winson Chungb745afb2015-03-02 11:51:23 -08004149 if (mAppsView != null) {
4150 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 }
4153
Sunny Goyal4390ace2014-10-13 11:33:11 -07004154 @Override
4155 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4156 Runnable r = new Runnable() {
4157 public void run() {
4158 bindWidgetsRestored(widgets);
4159 }
4160 };
4161 if (waitUntilResume(r)) {
4162 return;
4163 }
4164 mWorkspace.widgetsRestored(widgets);
4165 }
4166
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004168 * Some shortcuts were updated in the background.
4169 *
4170 * Implementation of the method from LauncherModel.Callbacks.
4171 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004172 @Override
4173 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4174 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004175 Runnable r = new Runnable() {
4176 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004177 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004178 }
4179 };
4180 if (waitUntilResume(r)) {
4181 return;
4182 }
4183
Sunny Goyal4390ace2014-10-13 11:33:11 -07004184 if (!updated.isEmpty()) {
4185 mWorkspace.updateShortcuts(updated);
4186 }
4187
4188 if (!removed.isEmpty()) {
4189 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4190 for (ShortcutInfo si : removed) {
4191 removedComponents.add(si.getTargetComponent());
4192 }
4193 mWorkspace.removeItemsByComponentName(removedComponents, user);
4194 // Notify the drag controller
4195 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004196 }
4197 }
4198
4199 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004200 * Update the state of a package, typically related to install state.
4201 *
4202 * Implementation of the method from LauncherModel.Callbacks.
4203 */
Sunny Goyale755d462014-07-22 13:48:29 -07004204 @Override
4205 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004206 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004207 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004208 }
4209 }
4210
4211 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004212 * Update the label and icon of all the icons in a package
4213 *
4214 * Implementation of the method from LauncherModel.Callbacks.
4215 */
4216 @Override
4217 public void updatePackageBadge(String packageName) {
4218 if (mWorkspace != null) {
4219 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4220 }
4221 }
4222
4223 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004224 * A package was uninstalled. We take both the super set of packageNames
4225 * in addition to specific applications to remove, the reason being that
4226 * this can be called when a package is updated as well. In that scenario,
4227 * we only remove specific components from the workspace, where as
4228 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004229 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004230 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004231 * Implementation of the method from LauncherModel.Callbacks.
4232 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004233 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004234 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004235 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004236 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004237 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004238 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004239 }
Winson Chungd64d1762013-08-20 14:37:16 -07004240 };
4241 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004242 return;
4243 }
4244
Sunny Goyal1a745e82014-10-02 15:58:31 -07004245 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004246 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4247 for (AppInfo info : appInfos) {
4248 removedComponents.add(info.componentName);
4249 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004250 if (!packageNames.isEmpty()) {
4251 mWorkspace.removeItemsByPackageName(packageNames, user);
4252 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004253 if (!removedComponents.isEmpty()) {
4254 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004255 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004256 // Notify the drag controller
4257 mDragController.onAppsRemoved(packageNames, removedComponents);
4258
Sunny Goyal1a745e82014-10-02 15:58:31 -07004259 } else {
4260 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004261 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004262
Winson Chungdf95eb12013-10-16 14:57:07 -07004263 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004264 if (mAppsView != null) {
4265 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004266 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004267 }
Joe Onoratobe386092009-11-17 17:32:16 -08004268
4269 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004270 * A number of packages were updated.
4271 */
Adam Cohen091440a2015-03-18 14:16:05 -07004272 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004273 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004274 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004275 bindPackagesUpdated(mWidgetsAndShortcuts);
4276 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004277 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004278 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4280 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4281 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004282 return;
4283 }
4284
Winson Chung64359a52013-07-08 17:17:08 -07004285 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004286 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004287 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004288 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004289 }
4290
Winson Chung400438b2011-01-16 17:53:48 -08004291 private int mapConfigurationOriActivityInfoOri(int configOri) {
4292 final Display d = getWindowManager().getDefaultDisplay();
4293 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4294 switch (d.getRotation()) {
4295 case Surface.ROTATION_0:
4296 case Surface.ROTATION_180:
4297 // We are currently in the same basic orientation as the natural orientation
4298 naturalOri = configOri;
4299 break;
4300 case Surface.ROTATION_90:
4301 case Surface.ROTATION_270:
4302 // We are currently in the other basic orientation to the natural orientation
4303 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4304 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4305 break;
4306 }
4307
4308 int[] oriMap = {
4309 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4310 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4311 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4312 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4313 };
4314 // Since the map starts at portrait, we need to offset if this device's natural orientation
4315 // is landscape.
4316 int indexOffset = 0;
4317 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4318 indexOffset = 1;
4319 }
4320 return oriMap[(d.getRotation() + indexOffset) % 4];
4321 }
Adam Cohen446e9402011-09-15 18:21:21 -07004322
Winson Chung4b919f82012-05-01 10:44:08 -07004323 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004324 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004325 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4326 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4327 .getConfiguration().orientation));
4328 } else {
4329 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4330 }
Winson Chung4b919f82012-05-01 10:44:08 -07004331 }
4332 }
4333 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004334 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004335 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004336 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004337 } else {
4338 mHandler.postDelayed(new Runnable() {
4339 public void run() {
4340 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4341 }
4342 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004343 }
Winson Chung4b919f82012-05-01 10:44:08 -07004344 }
Winson Chung400438b2011-01-16 17:53:48 -08004345 }
4346
Winson Chunge43a1e72014-01-15 10:33:02 -08004347 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004348 if (mLauncherCallbacks != null) {
4349 return mLauncherCallbacks.isLauncherPreinstalled();
4350 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004351 PackageManager pm = getPackageManager();
4352 try {
4353 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4354 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4355 return true;
4356 } else {
4357 return false;
4358 }
4359 } catch (NameNotFoundException e) {
4360 e.printStackTrace();
4361 return false;
4362 }
4363 }
4364
Adam Cohenea90f832014-05-21 15:03:34 -07004365 /**
4366 * This method indicates whether or not we should suggest default wallpaper dimensions
4367 * when our wallpaper cropper was not yet used to set a wallpaper.
4368 */
4369 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004370 if (mLauncherCallbacks != null) {
4371 return mLauncherCallbacks.overrideWallpaperDimensions();
4372 }
Adam Cohenea90f832014-05-21 15:03:34 -07004373 return true;
4374 }
4375
Adam Cohen432609a2014-03-13 17:03:22 -07004376 /**
4377 * To be overridden by subclasses to indicate that there is an activity to launch
4378 * before showing the standard launcher experience.
4379 */
4380 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004381 if (mLauncherCallbacks != null) {
4382 return mLauncherCallbacks.hasFirstRunActivity();
4383 }
Adam Cohen432609a2014-03-13 17:03:22 -07004384 return false;
4385 }
4386
4387 /**
4388 * To be overridden by subclasses to launch any first run activity
4389 */
4390 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004391 if (mLauncherCallbacks != null) {
4392 return mLauncherCallbacks.getFirstRunActivity();
4393 }
Adam Cohen432609a2014-03-13 17:03:22 -07004394 return null;
4395 }
4396
Winson Chunga6945242014-01-08 14:04:34 -08004397 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004398 return !ActivityManager.isRunningInTestHarness() &&
4399 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004400 }
4401
Adam Cohen4a9423d2014-03-28 14:22:31 -07004402 protected boolean hasRunFirstRunActivity() {
4403 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4404 }
4405
Adam Cohen432609a2014-03-13 17:03:22 -07004406 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004407 if (shouldRunFirstRunActivity() &&
4408 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004409 Intent firstRunIntent = getFirstRunActivity();
4410 if (firstRunIntent != null) {
4411 startActivity(firstRunIntent);
4412 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004413 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004414 }
4415 }
Adam Cohen432609a2014-03-13 17:03:22 -07004416 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004417 }
4418
4419 private void markFirstRunActivityShown() {
4420 SharedPreferences.Editor editor = mSharedPrefs.edit();
4421 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4422 editor.apply();
4423 }
4424
Adam Cohen432609a2014-03-13 17:03:22 -07004425 /**
4426 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4427 * screen that must be displayed and dismissed.
4428 */
4429 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004430 if (mLauncherCallbacks != null) {
4431 return mLauncherCallbacks.hasDismissableIntroScreen();
4432 }
Adam Cohen432609a2014-03-13 17:03:22 -07004433 return false;
4434 }
4435
4436 /**
4437 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4438 */
4439 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004440 if (mLauncherCallbacks != null) {
4441 return mLauncherCallbacks.getIntroScreen();
4442 }
Adam Cohen432609a2014-03-13 17:03:22 -07004443 return null;
4444 }
4445
4446 /**
4447 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4448 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4449 */
4450 private boolean shouldShowIntroScreen() {
4451 return hasDismissableIntroScreen() &&
4452 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4453 }
4454
4455 protected void showIntroScreen() {
4456 View introScreen = getIntroScreen();
4457 changeWallpaperVisiblity(false);
4458 if (introScreen != null) {
4459 mDragLayer.showOverlayView(introScreen);
4460 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004461 if (mLauncherOverlayContainer != null) {
4462 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4463 }
Adam Cohen432609a2014-03-13 17:03:22 -07004464 }
4465
4466 public void dismissIntroScreen() {
4467 markIntroScreenDismissed();
4468 if (showFirstRunActivity()) {
4469 // We delay hiding the intro view until the first run activity is showing. This
4470 // avoids a blip.
4471 mWorkspace.postDelayed(new Runnable() {
4472 @Override
4473 public void run() {
4474 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004475 if (mLauncherOverlayContainer != null) {
4476 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4477 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004478 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004479 }
4480 }, ACTIVITY_START_DELAY);
4481 } else {
4482 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004483 if (mLauncherOverlayContainer != null) {
4484 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4485 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004486 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004487 }
4488 changeWallpaperVisiblity(true);
4489 }
4490
4491 private void markIntroScreenDismissed() {
4492 SharedPreferences.Editor editor = mSharedPrefs.edit();
4493 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4494 editor.apply();
4495 }
4496
Adam Cohen091440a2015-03-18 14:16:05 -07004497 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004498 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4499 // on the device, then we always show the first run cling experience (or if there is no
4500 // launcher2). Otherwise, we prompt the user upon started for migration
4501 LauncherClings launcherClings = new LauncherClings(this);
4502 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4503 if (mModel.canMigrateFromOldLauncherDb(this)) {
4504 launcherClings.showMigrationCling();
4505 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004506 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004507 }
4508 }
4509 }
4510
Winson Chunga6945242014-01-08 14:04:34 -08004511 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004512 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4513 if (mHotseat != null) mHotseat.setAlpha(1f);
4514 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4515 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004516 }
4517
4518 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004519 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4520 if (mHotseat != null) mHotseat.setAlpha(0f);
4521 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4522 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004523 }
4524
Mathew Inwood72fbec12013-11-19 15:45:07 +00004525 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004526 // Called from search suggestion, not supported in other profiles.
4527 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4528 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4529 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4530 myUser);
4531 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004532 return null;
4533 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004534 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004535 }
4536
4537 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4538 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004539 // Called from search suggestion, not supported in other profiles.
4540 return createShortcutDragInfo(shortcutIntent, caption, icon,
4541 UserHandleCompat.myUserHandle());
4542 }
4543
4544 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4545 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004546 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004547 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004548 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004549 }
4550
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004551 protected void moveWorkspaceToDefaultScreen() {
4552 mWorkspace.moveToDefaultScreen(false);
4553 }
4554
Mathew Inwood72fbec12013-11-19 15:45:07 +00004555 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4556 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004557 mWorkspace.onExternalDragStartedWithItem(dragView);
4558 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004559 }
4560
Anjali Koppalf5d29132014-02-28 13:33:27 -08004561 @Override
4562 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004563 if (mLauncherCallbacks != null) {
4564 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4565 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004566 }
4567
Winson Chung80baf5a2010-08-09 16:03:15 -07004568 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004569 * Prints out out state for debugging.
4570 */
4571 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004572 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004573 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004574 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4575 Log.d(TAG, "mRestoring=" + mRestoring);
4576 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4577 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004578 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004579 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004580
Winson Chung785d2eb2011-04-14 16:08:02 -07004581 if (mAppsCustomizeContent != null) {
4582 mAppsCustomizeContent.dumpState();
4583 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004584 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004585 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004586
4587 @Override
4588 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4589 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004590 synchronized (sDumpLogs) {
4591 writer.println(" ");
4592 writer.println("Debug logs: ");
4593 for (int i = 0; i < sDumpLogs.size(); i++) {
4594 writer.println(" " + sDumpLogs.get(i));
4595 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004596 }
Adam Cohen9211d422014-10-07 18:14:53 -07004597 if (mLauncherCallbacks != null) {
4598 mLauncherCallbacks.dump(prefix, fd, writer, args);
4599 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004600 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004601
4602 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004603 if (DEBUG_DUMP_LOG) {
4604 synchronized (sDumpLogs) {
4605 Log.d(TAG, "");
4606 Log.d(TAG, "*********************");
4607 Log.d(TAG, "Launcher debug logs: ");
4608 for (int i = 0; i < sDumpLogs.size(); i++) {
4609 Log.d(TAG, " " + sDumpLogs.get(i));
4610 }
4611 Log.d(TAG, "*********************");
4612 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004613 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004614 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 }
4616
4617 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004618 addDumpLog(tag, log, null, debugLog);
4619 }
4620
4621 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004622 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004623 if (e != null) {
4624 Log.d(tag, log, e);
4625 } else {
4626 Log.d(tag, log);
4627 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004628 }
Winson Chungede41292013-09-19 16:27:36 -07004629 if (DEBUG_DUMP_LOG) {
4630 sDateStamp.setTime(System.currentTimeMillis());
4631 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004632 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4633 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004634 }
Winson Chungede41292013-09-19 16:27:36 -07004635 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004636 }
4637
Adam Cohen59400422014-03-05 18:07:04 -08004638 public static CustomAppWidget getCustomAppWidget(String name) {
4639 return sCustomAppWidgets.get(name);
4640 }
4641
4642 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4643 return sCustomAppWidgets;
4644 }
4645
Winson Chungede41292013-09-19 16:27:36 -07004646 public void dumpLogsToLocalData() {
4647 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004648 new AsyncTask<Void, Void, Void>() {
4649 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004650 boolean success = false;
4651 sDateStamp.setTime(sRunStart);
4652 String FILENAME = sDateStamp.getMonth() + "-"
4653 + sDateStamp.getDay() + "_"
4654 + sDateStamp.getHours() + "-"
4655 + sDateStamp.getMinutes() + "_"
4656 + sDateStamp.getSeconds() + ".txt";
4657
4658 FileOutputStream fos = null;
4659 File outFile = null;
4660 try {
4661 outFile = new File(getFilesDir(), FILENAME);
4662 outFile.createNewFile();
4663 fos = new FileOutputStream(outFile);
4664 } catch (Exception e) {
4665 e.printStackTrace();
4666 }
4667 if (fos != null) {
4668 PrintWriter writer = new PrintWriter(fos);
4669
4670 writer.println(" ");
4671 writer.println("Debug logs: ");
4672 synchronized (sDumpLogs) {
4673 for (int i = 0; i < sDumpLogs.size(); i++) {
4674 writer.println(" " + sDumpLogs.get(i));
4675 }
4676 }
4677 writer.close();
4678 }
4679 try {
4680 if (fos != null) {
4681 fos.close();
4682 success = true;
4683 }
4684 } catch (IOException e) {
4685 e.printStackTrace();
4686 }
Michael Jurka43467462013-11-14 12:03:58 +01004687 return null;
Winson Chungede41292013-09-19 16:27:36 -07004688 }
Michael Jurka43467462013-11-14 12:03:58 +01004689 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004690 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004692}
Michael Jurka2763be32011-02-24 11:19:57 -08004693
Michael Jurkaabded662011-03-04 12:06:57 -08004694interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004695 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004696 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004697 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004698 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004699 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004700}
Dan Sandlerd5024042014-01-09 15:01:33 -05004701
4702interface DebugIntents {
4703 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4704 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004705}