blob: 8a21d624c08120ec04e47fd8e7c70a418f51dd2c [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 Sandler6053b8002013-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();
Adam Cohenf9426d52012-06-04 17:26:21 -07001050
Winson Chung780fe592013-09-26 14:48:44 -07001051 // Process any items that were added while Launcher was away.
1052 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1053
Michael Jurka447bf842013-05-15 14:52:15 +02001054 if (DEBUG_RESUME_TIME) {
1055 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1056 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001057
1058 if (mWorkspace.getCustomContentCallbacks() != null) {
1059 // If we are resuming and the custom content is the current page, we call onShow().
1060 // It is also poassible that onShow will instead be called slightly after first layout
1061 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1062 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001063 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001064 }
1065 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001066 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001067 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001068
1069 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001070
1071 if (mLauncherCallbacks != null) {
1072 mLauncherCallbacks.onResume();
1073 }
Adam Cohen06dff352012-06-01 17:17:08 -07001074 }
1075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001078 // Ensure that items added to Launcher are queued until Launcher returns
1079 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001080 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001083 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001084 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001085 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001086
1087 // We call onHide() aggressively. The custom content callbacks should be able to
1088 // debounce excess onHide calls.
1089 if (mWorkspace.getCustomContentCallbacks() != null) {
1090 mWorkspace.getCustomContentCallbacks().onHide();
1091 }
Romain Guycbb89e42009-06-08 15:52:54 -07001092
Adam Cohen9211d422014-10-07 18:14:53 -07001093 if (mLauncherCallbacks != null) {
1094 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Adam Cohenc2d6e892014-10-16 09:49:24 -07001113 public interface LauncherOverlay {
1114
1115 /**
1116 * Touch interaction leading to overscroll has begun
1117 */
1118 public void onScrollInteractionBegin();
1119
1120 /**
1121 * Touch interaction related to overscroll has ended
1122 */
1123 public void onScrollInteractionEnd();
1124
1125 /**
1126 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1127 * screen (or in the case of RTL, the rightmost screen).
1128 */
1129 public void onScrollChange(int progress, boolean rtl);
1130
1131 /**
1132 * Screen has stopped scrolling
1133 */
1134 public void onScrollSettled();
1135
1136 /**
1137 * This method can be called by the Launcher in order to force the LauncherOverlay
1138 * to exit fully immersive mode.
1139 */
1140 public void forceExitFullImmersion();
1141 }
1142
1143 public interface LauncherOverlayCallbacks {
1144 /**
1145 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1146 * however it doesn't modify any state within the launcher.
1147 */
1148 public boolean canEnterFullImmersion();
1149
1150 /**
1151 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1152 * eg. by occupying the full screen and handling all touch events.
1153 *
1154 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1155 * case, Launcher will modify any necessary state and assumes the overlay is
1156 * handling all interaction. If false, the LauncherOverlay should cancel any
1157 *
1158 */
1159 public boolean enterFullImmersion();
1160
1161 /**
1162 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1163 * full control over UI and state.
1164 */
1165 public void exitFullImmersion();
1166 }
1167
1168 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1169
1170 @Override
1171 public boolean canEnterFullImmersion() {
1172 return mState == State.WORKSPACE;
1173 }
1174
1175 @Override
1176 public boolean enterFullImmersion() {
1177 if (mState == State.WORKSPACE) {
1178 // When fully immersed, disregard any touches which fall through.
1179 mDragLayer.setBlockTouch(true);
1180 return true;
1181 }
1182 return false;
1183 }
1184
1185 @Override
1186 public void exitFullImmersion() {
1187 mDragLayer.setBlockTouch(false);
1188 }
1189 }
1190
Jorim Jaggid017f882014-01-14 17:08:48 -08001191 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001192 if (mLauncherCallbacks != null) {
1193 return mLauncherCallbacks.hasSettings();
1194 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001195 return false;
1196 }
1197
Adam Cohenbffe7452013-07-22 18:21:45 -07001198
Adam Cohen9211d422014-10-07 18:14:53 -07001199 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001200 CustomContentCallbacks callbacks, String description) {
1201 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001202 }
1203
Adam Cohenedb40762013-07-18 16:45:45 -07001204 // The custom content needs to offset its content to account for the QSB
1205 public int getTopOffsetForCustomContent() {
1206 return mWorkspace.getPaddingTop();
1207 }
1208
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 @Override
1210 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001212 if (mModel.isCurrentCallbacks(this)) {
1213 mModel.stopLoader();
1214 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 if (mAppsCustomizeContent != null) {
1216 mAppsCustomizeContent.surrender();
1217 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001218 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001219 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 @Override
1223 public void onWindowFocusChanged(boolean hasFocus) {
1224 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001225 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001226
1227 if (mLauncherCallbacks != null) {
1228 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1229 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 private boolean acceptFilter() {
1233 final InputMethodManager inputManager = (InputMethodManager)
1234 getSystemService(Context.INPUT_METHOD_SERVICE);
1235 return !inputManager.isFullscreenMode();
1236 }
1237
1238 @Override
1239 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001240 final int uniChar = event.getUnicodeChar();
1241 final boolean handled = super.onKeyDown(keyCode, event);
1242 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1243 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1245 keyCode, event);
1246 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001248 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 // showSearchDialog()
1250 // If there are multiple keystrokes before the search dialog takes focus,
1251 // onSearchRequested() will be called for every keystroke,
1252 // but it is idempotent, so it's fine.
1253 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255 }
1256
Joe Onorato8a9625e2010-01-28 15:55:35 -08001257 // Eat the long press event so the keyboard doesn't come up.
1258 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1259 return true;
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 return handled;
1263 }
1264
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 private String getTypedText() {
1266 return mDefaultKeySsb.toString();
1267 }
1268
1269 private void clearTypedText() {
1270 mDefaultKeySsb.clear();
1271 mDefaultKeySsb.clearSpans();
1272 Selection.setSelection(mDefaultKeySsb, 0);
1273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001276 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1277 * State
1278 */
1279 private static State intToState(int stateOrdinal) {
1280 State state = State.WORKSPACE;
1281 final State[] stateValues = State.values();
1282 for (int i = 0; i < stateValues.length; i++) {
1283 if (stateValues[i].ordinal() == stateOrdinal) {
1284 state = stateValues[i];
1285 break;
1286 }
1287 }
1288 return state;
1289 }
1290
1291 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 * Restores the previous state, if it exists.
1293 *
1294 * @param savedState The previous state.
1295 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001296 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 private void restoreState(Bundle savedState) {
1298 if (savedState == null) {
1299 return;
1300 }
1301
Michael Jurka883f55b2010-10-21 15:47:14 -07001302 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001303 if (state == State.APPS || state == State.WIDGETS) {
1304 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001305 }
1306
Adam Cohen21cd0022013-10-09 18:57:02 -07001307 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1308 PagedView.INVALID_RESTORE_PAGE);
1309 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001310 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1317 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1320 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001321 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1322 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001323 AppWidgetProviderInfo info = savedState.getParcelable(
1324 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1325 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001326 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001327 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 mRestoring = true;
1329 }
1330
1331 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1332 if (renameFolder) {
1333 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001334 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 mRestoring = true;
1336 }
Winson Chunga12a2502010-12-20 14:41:35 -08001337
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 // Restore the AppsCustomize tab
1339 if (mAppsCustomizeTabHost != null) {
1340 String curTab = savedState.getString("apps_customize_currentTab");
1341 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001342 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001344 mAppsCustomizeContent.loadAssociatedPages(
1345 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
1347
Winson Chung5afbf7b2011-07-25 11:53:08 -07001348 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1349 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001351 mItemIdToViewId = (HashMap<Integer, Integer>)
1352 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
1355 /**
1356 * Finds all the views we need and configure them properly.
1357 */
1358 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001359 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001360
Craig Mautner360310b2012-10-26 15:13:08 -07001361 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001362 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001363 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1364 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001365 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001366 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001367
John Spurlock77e1f472013-09-11 10:09:51 -04001368 mLauncherView.setSystemUiVisibility(
1369 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001370 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
Winson Chung4c98d922011-05-31 16:50:48 -07001372 // Setup the drag layer
1373 mDragLayer.setup(this, dragController);
1374
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 // Setup the hotseat
1376 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1377 if (mHotseat != null) {
1378 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001379 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 }
1381
Jorim Jaggid017f882014-01-14 17:08:48 -08001382 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001383 View widgetButton = findViewById(R.id.widget_button);
1384 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 @Override
1386 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001387 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001388 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001389 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 }
1391 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001392 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1393
1394 View wallpaperButton = findViewById(R.id.wallpaper_button);
1395 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 @Override
1397 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001398 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001399 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001400 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 }
1402 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001403 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404
1405 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 if (hasSettings()) {
1407 settingsButton.setOnClickListener(new OnClickListener() {
1408 @Override
1409 public void onClick(View arg0) {
1410 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001411 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001413 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 });
1415 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1416 } else {
1417 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001418 }
1419
Adam Cohendbdff6b2013-09-18 19:09:15 -07001420 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421
Winson Chung4c98d922011-05-31 16:50:48 -07001422 // Setup the workspace
1423 mWorkspace.setHapticFeedbackEnabled(false);
1424 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001425 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001426 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001427
Winson Chungf0ea4d32011-06-06 14:27:16 -07001428 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001429 mSearchDropTargetBar = (SearchDropTargetBar)
1430 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001431
Winson Chungb745afb2015-03-02 11:51:23 -08001432 // Setup Apps
1433 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1434
Winson Chungf0ea4d32011-06-06 14:27:16 -07001435 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001436 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001437 mAppsCustomizeContent = (AppsCustomizePagedView)
1438 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1439 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001440
Winson Chung3d503fb2011-07-13 17:25:49 -07001441 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001442 dragController.setDragScoller(mWorkspace);
1443 dragController.setScrollView(mDragLayer);
1444 dragController.setMoveTarget(mWorkspace);
1445 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001446 if (mSearchDropTargetBar != null) {
1447 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001448 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001449 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001450
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001451 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001452 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001453 mWeightWatcher = new WeightWatcher(this);
1454 mWeightWatcher.setAlpha(0.5f);
1455 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001456 new FrameLayout.LayoutParams(
1457 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001458 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 Gravity.BOTTOM)
1460 );
Adam Cohen39a06042013-07-19 14:30:12 -07001461
1462 boolean show = shouldShowWeightWatcher();
1463 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466
1467 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 * Sets the all apps button. This method is called from {@link Hotseat}.
1469 */
1470 public void setAllAppsButton(View allAppsButton) {
1471 mAllAppsButton = allAppsButton;
1472 }
1473
1474 public View getAllAppsButton() {
1475 return mAllAppsButton;
1476 }
1477
1478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Creates a view representing a shortcut.
1480 *
1481 * @param info The data structure describing the shortcut.
1482 *
1483 * @return A View inflated from R.layout.application.
1484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001487 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
1493 * @param layoutResId The id of the XML layout used to create the shortcut.
1494 * @param parent The group the shortcut belongs to.
1495 * @param info The data structure describing the shortcut.
1496 *
1497 * @return A View inflated from layoutResId.
1498 */
Adam Cohen59400422014-03-05 18:07:04 -08001499 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001500 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001501 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
1511 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
1516 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520
Sunny Goyal2350bc92014-10-14 16:42:54 -07001521 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001522 if (info == null) {
1523 return;
1524 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 final View view = createShortcut(info);
1526
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 // First we check if we already know the exact location where we want to add this item.
1528 if (cellX >= 0 && cellY >= 0) {
1529 cellXY[0] = cellX;
1530 cellXY[1] = cellY;
1531 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001532
1533 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 true, null,null)) {
1536 return;
1537 }
1538 DragObject dragObject = new DragObject();
1539 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1541 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 return;
1543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 foundCellSpan = (result != null);
1548 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 }
1551
1552 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001553 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 return;
1555 }
1556
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001557 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
1564
Adam Cohen2f093b62012-04-30 18:59:53 -07001565 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1566 int minHeight) {
1567 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001568 // We want to account for the extra amount of padding that we are adding to the widget
1569 // to ensure that it gets the full amount of space that it has requested
1570 int requiredWidth = minWidth + padding.left + padding.right;
1571 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001572 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1576 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001577 }
1578
Adam Cohen2f093b62012-04-30 18:59:53 -07001579 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1580 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001581 }
1582
Adam Cohen2f093b62012-04-30 18:59:53 -07001583 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1584 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001585 }
1586
Adam Cohen2f093b62012-04-30 18:59:53 -07001587 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1588 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001589 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001593 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001595 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 */
Adam Cohen091440a2015-03-18 14:16:05 -07001597 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001598 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1599
1600 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001602 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1603 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Adam Cohen59400422014-03-05 18:07:04 -08001606 if (appWidgetInfo.isCustomWidget) {
1607 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001608 }
1609
Adam Cohen59400422014-03-05 18:07:04 -08001610 LauncherAppWidgetInfo launcherInfo;
1611 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1612 launcherInfo.spanX = info.spanX;
1613 launcherInfo.spanY = info.spanY;
1614 launcherInfo.minSpanX = info.minSpanX;
1615 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001616 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001619 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620
1621 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 if (hostView == null) {
1623 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001624 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1625 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 } else {
1627 // The AppWidgetHostView has already been inflated and instantiated
1628 launcherInfo.hostView = hostView;
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001631 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001632 launcherInfo.notifyWidgetSizeChanged(this);
1633
Adam Cohen59400422014-03-05 18:07:04 -08001634 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1635 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001636
1637 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001639 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1643 @Override
1644 public void onReceive(Context context, Intent intent) {
1645 final String action = intent.getAction();
1646 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1647 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001648 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001649 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001650
Winson Chungc100e8e2011-08-09 16:02:43 -07001651 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001652 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001653 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1654 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001655 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001656 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001657 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1658 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001660 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1661 mModel.resetLoadedState(false, true);
1662 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1663 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1664 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1665 mModel.resetLoadedState(false, true);
1666 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1667 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1668 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001669 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1670 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1671 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 }
1673 }
1674 };
1675
1676 @Override
1677 public void onAttachedToWindow() {
1678 super.onAttachedToWindow();
1679
1680 // Listen for broadcasts related to user-presence
1681 final IntentFilter filter = new IntentFilter();
1682 filter.addAction(Intent.ACTION_SCREEN_OFF);
1683 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001684 // For handling managed profiles
1685 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1686 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001687 if (ENABLE_DEBUG_INTENTS) {
1688 filter.addAction(DebugIntents.DELETE_DATABASE);
1689 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1690 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001692 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001693 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001694 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 mVisible = true;
1696 }
1697
Adam Cohen0b395352014-06-09 22:54:36 +00001698 /**
1699 * Sets up transparent navigation and status bars in LMP.
1700 * This method is a no-op for other platform versions.
1701 */
Sunny Goyald0091012015-01-20 15:55:34 -08001702 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001703 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001704 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001705 Window window = getWindow();
1706 window.getAttributes().systemUiVisibility |=
1707 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1708 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1709 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1710 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1711 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1712 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1713 window.setStatusBarColor(Color.TRANSPARENT);
1714 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001715 }
1716 }
1717
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 @Override
1719 public void onDetachedFromWindow() {
1720 super.onDetachedFromWindow();
1721 mVisible = false;
1722
Adam Cohend113e0c2010-11-11 10:48:05 -08001723 if (mAttached) {
1724 unregisterReceiver(mReceiver);
1725 mAttached = false;
1726 }
Winson Chungb745afb2015-03-02 11:51:23 -08001727 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 }
1729
1730 public void onWindowVisibilityChanged(int visibility) {
1731 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001732 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001733 // The following code used to be in onResume, but it turns out onResume is called when
1734 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1735 // is a more appropriate event to handle
1736 if (mVisible) {
1737 mAppsCustomizeTabHost.onWindowVisible();
1738 if (!mWorkspaceLoading) {
1739 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 // We want to let Launcher draw itself at least once before we force it to build
1741 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001742 // apps is nice and speedy.
1743 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001744 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001745 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001746 if (mStarted) return;
1747 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001748 // We delay the layer building a bit in order to give
1749 // other message processing a time to run. In particular
1750 // this avoids a delay in hiding the IME if it was
1751 // currently shown, because doing that may involve
1752 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001753 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001754 final ViewTreeObserver.OnDrawListener listener = this;
1755 mWorkspace.post(new Runnable() {
1756 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001757 if (mWorkspace != null &&
1758 mWorkspace.getViewTreeObserver() != null) {
1759 mWorkspace.getViewTreeObserver().
1760 removeOnDrawListener(listener);
1761 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001762 }
1763 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001764 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001765 }
1766 });
1767 }
1768 clearTypedText();
1769 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 }
1771
Adam Cohen091440a2015-03-18 14:16:05 -07001772 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 mHandler.removeMessages(ADVANCE_MSG);
1774 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1775 mHandler.sendMessageDelayed(msg, delay);
1776 mAutoAdvanceSentTime = System.currentTimeMillis();
1777 }
1778
Adam Cohen091440a2015-03-18 14:16:05 -07001779 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1781 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1782 mAutoAdvanceRunning = autoAdvanceRunning;
1783 if (autoAdvanceRunning) {
1784 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1785 sendAdvanceMessage(delay);
1786 } else {
1787 if (!mWidgetsToAdvance.isEmpty()) {
1788 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1789 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1790 }
1791 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001792 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
1794 }
1795 }
1796
1797 private final Handler mHandler = new Handler() {
1798 @Override
1799 public void handleMessage(Message msg) {
1800 if (msg.what == ADVANCE_MSG) {
1801 int i = 0;
1802 for (View key: mWidgetsToAdvance.keySet()) {
1803 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1804 final int delay = mAdvanceStagger * i;
1805 if (v instanceof Advanceable) {
1806 postDelayed(new Runnable() {
1807 public void run() {
1808 ((Advanceable) v).advance();
1809 }
1810 }, delay);
1811 }
1812 i++;
1813 }
1814 sendAdvanceMessage(mAdvanceInterval);
1815 }
1816 }
1817 };
1818
1819 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001820 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1822 if (v instanceof Advanceable) {
1823 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001824 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001825 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001826 }
1827 }
1828
1829 void removeWidgetToAutoAdvance(View hostView) {
1830 if (mWidgetsToAdvance.containsKey(hostView)) {
1831 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001832 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001833 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 }
1835
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001837 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 launcherInfo.hostView = null;
1839 }
1840
Winson Chung93eef082012-03-23 15:59:27 -07001841 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1842 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1843 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001844 }
1845
Winson Chunga6945242014-01-08 14:04:34 -08001846 public DragLayer getDragLayer() {
1847 return mDragLayer;
1848 }
1849
Winson Chungb745afb2015-03-02 11:51:23 -08001850 public AppsContainerView getAppsView() {
1851 return mAppsView;
1852 }
1853
1854 public AppsCustomizeTabHost getWidgetsView() {
1855 return mAppsCustomizeTabHost;
1856 }
1857
Winson Chunga6945242014-01-08 14:04:34 -08001858 public Workspace getWorkspace() {
1859 return mWorkspace;
1860 }
1861
1862 public Hotseat getHotseat() {
1863 return mHotseat;
1864 }
1865
Jorim Jaggid017f882014-01-14 17:08:48 -08001866 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001867 return mOverviewPanel;
1868 }
1869
1870 public SearchDropTargetBar getSearchBar() {
1871 return mSearchDropTargetBar;
1872 }
1873
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001874 public LauncherAppWidgetHost getAppWidgetHost() {
1875 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 }
Romain Guycbb89e42009-06-08 15:52:54 -07001877
Winson Chunga9abd0e2010-10-27 17:18:37 -07001878 public LauncherModel getModel() {
1879 return mModel;
1880 }
1881
Winson Chunga6945242014-01-08 14:04:34 -08001882 protected SharedPreferences getSharedPrefs() {
1883 return mSharedPrefs;
1884 }
1885
Bjorn Bringertc459e522013-06-07 19:36:01 +01001886 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001887 getWindow().closeAllPanels();
1888
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001889 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001890 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001891 }
1892
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 @Override
1894 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001895 long startTime = 0;
1896 if (DEBUG_RESUME_TIME) {
1897 startTime = System.currentTimeMillis();
1898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 super.onNewIntent(intent);
1900
1901 // Close the menu
1902 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001903 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001904 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001905
Adam Cohened307df2013-10-02 09:37:31 -07001906 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1907 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1908 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001909
Adam Cohen6fecd412013-10-02 17:41:50 -07001910 if (mWorkspace == null) {
1911 // Can be cases where mWorkspace is null, this prevents a NPE
1912 return;
1913 }
1914 Folder openFolder = mWorkspace.getOpenFolder();
1915 // In all these cases, only animate if we're already on home
1916 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001917
1918 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1919 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001921 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001922 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001923 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001924
Adam Cohen6fecd412013-10-02 17:41:50 -07001925 closeFolder();
1926 exitSpringLoadedDragMode();
1927
1928 // If we are already on home, then just animate back to the workspace,
1929 // otherwise, just wait until onResume to set the state back to Workspace
1930 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001931 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 } else {
1933 mOnResumeState = State.WORKSPACE;
1934 }
1935
1936 final View v = getWindow().peekDecorView();
1937 if (v != null && v.getWindowToken() != null) {
1938 InputMethodManager imm = (InputMethodManager)getSystemService(
1939 INPUT_METHOD_SERVICE);
1940 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1941 }
1942
Winson Chungb745afb2015-03-02 11:51:23 -08001943 // Reset the apps view
1944 if (!alreadyOnHome && mAppsView != null) {
1945 mAppsView.scrollToTop();
1946 }
1947
Adam Cohen6fecd412013-10-02 17:41:50 -07001948 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001949 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001950 mAppsCustomizeTabHost.reset();
1951 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001952
Adam Cohen9211d422014-10-07 18:14:53 -07001953 if (mLauncherCallbacks != null) {
1954 mLauncherCallbacks.onHomeIntent();
1955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
Adam Cohened307df2013-10-02 09:37:31 -07001957
Michael Jurka447bf842013-05-15 14:52:15 +02001958 if (DEBUG_RESUME_TIME) {
1959 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961
Adam Cohen9211d422014-10-07 18:14:53 -07001962 if (mLauncherCallbacks != null) {
1963 mLauncherCallbacks.onNewIntent(intent);
1964 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001968 public void onRestoreInstanceState(Bundle state) {
1969 super.onRestoreInstanceState(state);
1970 for (int page: mSynchronouslyBoundPages) {
1971 mWorkspace.restoreInstanceStateForChild(page);
1972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
1975 @Override
1976 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001977 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001978 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1979 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001980 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001981 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982
Michael Jurka883f55b2010-10-21 15:47:14 -07001983 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001984 // We close any open folder since it will not be re-opened, and we need to make sure
1985 // this state is reflected.
1986 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001989 mWaitingForResult) {
1990 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001992 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1993 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1995 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1996 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001997 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
1999
2000 if (mFolderInfo != null && mWaitingForResult) {
2001 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2002 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2003 }
Michael Jurka946ad472010-07-09 18:05:18 -07002004
Winson Chung785d2eb2011-04-14 16:08:02 -07002005 // Save the current AppsCustomize tab
2006 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08002007 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2008 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002009 if (currentTabTag != null) {
2010 outState.putString("apps_customize_currentTab", currentTabTag);
2011 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002012 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2013 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002014 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002015 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002016
2017 if (mLauncherCallbacks != null) {
2018 mLauncherCallbacks.onSaveInstanceState(outState);
2019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
2022 @Override
2023 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002025
Winson Chunge7a03942011-08-05 15:05:12 -07002026 // Remove all pending runnables
2027 mHandler.removeMessages(ADVANCE_MSG);
2028 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002029 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002030
Winson Chungcd2b0142011-06-08 16:02:26 -07002031 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002032 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002033
2034 // It's possible to receive onDestroy after a new Launcher activity has
2035 // been created. In this case, don't interfere with the new Launcher.
2036 if (mModel.isCurrentCallbacks(this)) {
2037 mModel.stopLoader();
2038 app.setLauncher(null);
2039 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002042 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002044 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002046 mAppWidgetHost = null;
2047
2048 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049
2050 TextKeyListener.getInstance().release();
2051
Winson Chung81b52252012-08-27 15:34:29 -07002052 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2053 // to prevent leaking Launcher activities on orientation change.
2054 if (mModel != null) {
2055 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2056 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002057
2058 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002059 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002061 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002063 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 mWorkspace = null;
2065 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002066
Michael Jurka2ecf9952012-06-18 12:52:28 -07002067 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002068
2069 if (mLauncherCallbacks != null) {
2070 mLauncherCallbacks.onDestroy();
2071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
2073
Adam Cohena9cf38f2011-05-02 15:36:58 -07002074 public DragController getDragController() {
2075 return mDragController;
2076 }
2077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 @Override
2079 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002080 onStartForResult(requestCode);
2081 super.startActivityForResult(intent, requestCode);
2082 }
2083
2084 @Override
2085 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2086 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2087 onStartForResult(requestCode);
2088 try {
2089 super.startIntentSenderForResult(intent, requestCode,
2090 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2091 } catch (IntentSender.SendIntentException e) {
2092 throw new ActivityNotFoundException();
2093 }
2094 }
2095
2096 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002097 if (requestCode >= 0) {
2098 setWaitingForResult(true);
2099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
2101
Winson Chung70d72102011-08-12 11:24:35 -07002102 /**
2103 * Indicates that we want global search for this activity by setting the globalSearch
2104 * argument for {@link #startSearch} to true.
2105 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002107 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002109
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002110 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002111
Karl Rosaen138a0412009-04-23 19:00:21 -07002112 if (initialQuery == null) {
2113 // Use any text typed in the launcher as the initial query
2114 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 if (appSearchData == null) {
2117 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002118 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
Winson Chungadf0c182012-08-23 14:59:07 -07002120 Rect sourceBounds = new Rect();
2121 if (mSearchDropTargetBar != null) {
2122 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2123 }
Romain Guycbb89e42009-06-08 15:52:54 -07002124
Ian Parkinson047036e2014-09-01 15:40:53 +01002125 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002126 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 if (clearTextImmediately) {
2128 clearTypedText();
2129 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 }
2131
Ian Parkinson047036e2014-09-01 15:40:53 +01002132 /**
2133 * Start a text search.
2134 *
2135 * @return {@code true} if the search will start immediately, so any further keypresses
2136 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2137 * to buffer keypresses.
2138 */
2139 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002140 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002141 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2142 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2143 sourceBounds);
2144 }
2145
Michael Jurkaa33411c2012-06-14 16:18:21 -07002146 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002147 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002148 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002149 }
2150
2151 /**
2152 * Starts the global search activity. This code is a copied from SearchManager
2153 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002154 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002155 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002156 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2158 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2159 if (globalSearchActivity == null) {
2160 Log.w(TAG, "No global search activity found.");
2161 return;
2162 }
2163 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2164 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2165 intent.setComponent(globalSearchActivity);
2166 // Make sure that we have a Bundle to put source in
2167 if (appSearchData == null) {
2168 appSearchData = new Bundle();
2169 } else {
2170 appSearchData = new Bundle(appSearchData);
2171 }
Adam Cohen9211d422014-10-07 18:14:53 -07002172 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002173 if (!appSearchData.containsKey("source")) {
2174 appSearchData.putString("source", getPackageName());
2175 }
2176 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2177 if (!TextUtils.isEmpty(initialQuery)) {
2178 intent.putExtra(SearchManager.QUERY, initialQuery);
2179 }
2180 if (selectInitialQuery) {
2181 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2182 }
2183 intent.setSourceBounds(sourceBounds);
2184 try {
2185 startActivity(intent);
2186 } catch (ActivityNotFoundException ex) {
2187 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
2190
Yura4f93ec62014-02-04 14:15:21 +00002191 public boolean isOnCustomContent() {
2192 return mWorkspace.isOnOrMovingToCustomContent();
2193 }
2194
Winson Chung70d72102011-08-12 11:24:35 -07002195 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002196 public boolean onPrepareOptionsMenu(Menu menu) {
2197 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002198 if (!isOnCustomContent()) {
2199 // Close any open folders
2200 closeFolder();
2201 // Stop resizing any widgets
2202 mWorkspace.exitWidgetResizeMode();
2203 if (!mWorkspace.isInOverviewMode()) {
2204 // Show the overview mode
2205 showOverviewMode(true);
2206 } else {
2207 showWorkspace(true);
2208 }
Winson Chunge0298742014-01-17 12:03:00 -08002209 }
Adam Cohen9211d422014-10-07 18:14:53 -07002210 if (mLauncherCallbacks != null) {
2211 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2212 }
2213
Michael Jurkab94f3f82013-09-11 18:08:54 +02002214 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002217 @Override
2218 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002219 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002220 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002221 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002222 }
2223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 public boolean isWorkspaceLocked() {
2225 return mWorkspaceLoading || mWaitingForResult;
2226 }
2227
Adam Cohen517a7f52014-03-01 12:12:59 -08002228 public boolean isWorkspaceLoading() {
2229 return mWorkspaceLoading;
2230 }
2231
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002232 private void setWorkspaceLoading(boolean value) {
2233 boolean isLocked = isWorkspaceLocked();
2234 mWorkspaceLoading = value;
2235 if (isLocked != isWorkspaceLocked()) {
2236 onWorkspaceLockedChanged();
2237 }
2238 }
2239
2240 private void setWaitingForResult(boolean value) {
2241 boolean isLocked = isWorkspaceLocked();
2242 mWaitingForResult = value;
2243 if (isLocked != isWorkspaceLocked()) {
2244 onWorkspaceLockedChanged();
2245 }
2246 }
2247
Adam Cohen9211d422014-10-07 18:14:53 -07002248 protected void onWorkspaceLockedChanged() {
2249 if (mLauncherCallbacks != null) {
2250 mLauncherCallbacks.onWorkspaceLockedChanged();
2251 }
2252 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002253
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2258 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002259 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002262
Sunny Goyale6b63a32015-01-16 16:14:29 -08002263 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002264 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002265 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2266 }
2267
Sunny Goyale6b63a32015-01-16 16:14:29 -08002268 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002269 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2270 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002271 if (appWidgetInfo.configure != null) {
2272 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002273 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002274
Bjorn Bringert7984c942009-12-09 15:38:25 +00002275 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002276 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2277 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002280 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002281 Runnable onComplete = new Runnable() {
2282 @Override
2283 public void run() {
2284 // Exit spring loaded mode if necessary after adding the widget
2285 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2286 null);
2287 }
2288 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002291 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 }
2293 }
Romain Guycbb89e42009-06-08 15:52:54 -07002294
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002295 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002296 // Close any folders that may be open.
2297 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002298 mWorkspace.moveToCustomContentScreen(animate);
2299 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002300
2301 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2302 int[] cell, int spanX, int spanY) {
2303 switch (info.itemType) {
2304 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2305 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2306 int span[] = new int[2];
2307 span[0] = spanX;
2308 span[1] = spanY;
2309 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2310 container, screenId, cell, span);
2311 break;
2312 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2313 processShortcutFromDrop(info.componentName, container, screenId, cell);
2314 break;
2315 default:
2316 throw new IllegalStateException("Unknown item type: " + info.itemType);
2317 }
2318 }
2319
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 /**
2321 * Process a shortcut drop.
2322 *
2323 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002327 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2328 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002329 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002332 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333
2334 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.cellX = cell[0];
2336 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002338
2339 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2340 createShortcutIntent.setComponent(componentName);
2341 processShortcut(createShortcutIntent);
2342 }
2343
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 /**
2345 * Process a widget drop.
2346 *
2347 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002351 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2352 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002356 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002357 mPendingAddInfo.minSpanX = info.minSpanX;
2358 mPendingAddInfo.minSpanY = info.minSpanY;
2359
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 mPendingAddInfo.cellX = cell[0];
2362 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002363 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002364 if (span != null) {
2365 mPendingAddInfo.spanX = span[0];
2366 mPendingAddInfo.spanY = span[1];
2367 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002368
Adam Cohened66b2b2012-01-23 17:28:51 -08002369 AppWidgetHostView hostView = info.boundWidget;
2370 int appWidgetId;
2371 if (hostView != null) {
2372 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002373 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002374 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002375 // In this case, we either need to start an activity to get permission to bind
2376 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002377 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002378 Bundle options = info.bindOptions;
2379
Sunny Goyalffe83f12014-08-14 17:39:34 -07002380 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2381 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002382 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002383 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002384 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2387 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2388 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002389 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2390 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002391 // TODO: we need to make sure that this accounts for the options bundle.
2392 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002393 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2394 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002395 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002396 }
2397
Joe Onoratodeb98af2010-02-19 14:59:39 -08002398 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002399 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 }
2401
Winson Chung24ab2f12010-09-16 14:10:47 -07002402 void processWallpaper(Intent intent) {
2403 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2404 }
2405
Adam Cohendcd297f2013-06-18 13:13:40 -07002406 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002407 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002408 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 folderInfo.title = getText(R.string.folder_name);
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002412 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2413 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002414 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415
2416 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 FolderIcon newFolder =
2418 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002419 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002420 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002421 // Force measure the new folder icon
2422 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2423 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002424 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 }
Romain Guycbb89e42009-06-08 15:52:54 -07002426
Joe Onorato9c1289c2009-08-17 11:03:03 -04002427 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002428 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 }
2430
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002431 /**
2432 * Registers various content observers. The current implementation registers
2433 * only a favorites observer to keep track of the favorites applications.
2434 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002435 private void registerContentObservers() {
2436 ContentResolver resolver = getContentResolver();
2437 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2438 true, mWidgetObserver);
2439 }
2440
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 @Override
2442 public boolean dispatchKeyEvent(KeyEvent event) {
2443 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2444 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002447 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002448 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002449 dumpState();
2450 return true;
2451 }
2452 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002453 }
2454 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2455 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002456 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 return true;
2458 }
2459 }
2460
2461 return super.dispatchKeyEvent(event);
2462 }
2463
Joe Onorato88ec0992009-11-19 13:16:06 -08002464 @Override
2465 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002466 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2467 return;
2468 }
2469
Winson Chung3d9490a2015-03-24 17:38:42 -07002470 LauncherAccessibilityDelegate delegate =
2471 LauncherAppState.getInstance().getAccessibilityDelegate();
2472 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002473 return;
2474 }
2475
Winson Chungb745afb2015-03-02 11:51:23 -08002476 if (isAppsViewVisible()) {
2477 showWorkspace(true);
2478 } else if (isWidgetsViewVisible()) {
2479 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002480 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002481 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002482 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002483 Folder openFolder = mWorkspace.getOpenFolder();
2484 if (openFolder.isEditingName()) {
2485 openFolder.dismissEditingName();
2486 } else {
2487 closeFolder();
2488 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002489 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002490 mWorkspace.exitWidgetResizeMode();
2491
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002492 // Back button is a no-op here, but give at least some feedback for the button press
2493 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002494 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002495 }
2496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002498 * Re-listen when widgets are reset.
2499 */
Adam Cohen091440a2015-03-18 14:16:05 -07002500 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002501 if (mAppWidgetHost != null) {
2502 mAppWidgetHost.startListening();
2503 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002504 }
2505
2506 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 * Launches the intent referred by the clicked shortcut.
2508 *
2509 * @param v The view representing the clicked shortcut.
2510 */
2511 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002512 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2513 // view has detached (it's possible for this to happen if the view is removed mid touch).
2514 if (v.getWindowToken() == null) {
2515 return;
2516 }
2517
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002518 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002519 return;
2520 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002521
Adam Cohen1697b792013-09-17 19:08:21 -07002522 if (v instanceof Workspace) {
2523 if (mWorkspace.isInOverviewMode()) {
2524 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002525 }
2526 return;
2527 }
2528
2529 if (v instanceof CellLayout) {
2530 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002531 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002532 }
2533 }
2534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002536 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002537 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002539 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002540 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002542 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002543 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002544 } else if (tag instanceof AppInfo) {
2545 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002546 } else if (tag instanceof LauncherAppWidgetInfo) {
2547 if (v instanceof PendingAppWidgetHostView) {
2548 onClickPendingWidget((PendingAppWidgetHostView) v);
2549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 }
2551 }
2552
Sunny Goyal508da152014-08-14 10:53:27 -07002553 public void onClickPagedViewIcon(View v) {
2554 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002555 if (mLauncherCallbacks != null) {
2556 mLauncherCallbacks.onClickPagedViewIcon(v);
2557 }
Sunny Goyal508da152014-08-14 10:53:27 -07002558 }
2559
Michael Jurka0e260592010-06-30 17:07:39 -07002560 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002561 return false;
2562 }
2563
Michael Jurkaaf442092010-06-10 17:01:57 -07002564 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002565 * Event handler for the app widget view which has not fully restored.
2566 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002568 if (mIsSafeModeEnabled) {
2569 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2570 return;
2571 }
2572
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002573 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002574 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002575 int widgetId = info.appWidgetId;
2576 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2577 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002578 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2579 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002580 mPendingAddInfo.copyFrom(info);
2581 mPendingAddWidgetId = widgetId;
2582
Sunny Goyalffe83f12014-08-14 17:39:34 -07002583 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2584 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002585 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 } else if (info.installProgress < 0) {
2587 // The install has not been queued
2588 final String packageName = info.providerName.getPackageName();
2589 showBrokenAppInstallDialog(packageName,
2590 new DialogInterface.OnClickListener() {
2591 public void onClick(DialogInterface dialog, int id) {
2592 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2593 }
2594 });
2595 } else {
2596 // Download has started.
2597 final String packageName = info.providerName.getPackageName();
2598 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002599 }
2600 }
2601
2602 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002603 * Event handler for the "grid" button that appears on the home screen, which
2604 * enters all apps mode.
2605 *
2606 * @param v The view that was clicked.
2607 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 protected void onClickAllAppsButton(View v) {
2609 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002610 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002611 showWorkspace(true);
2612 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002613 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 }
Adam Cohen9211d422014-10-07 18:14:53 -07002615 if (mLauncherCallbacks != null) {
2616 mLauncherCallbacks.onClickAllAppsButton(v);
2617 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 }
2619
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002620 private void showBrokenAppInstallDialog(final String packageName,
2621 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002622 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 .setTitle(R.string.abandoned_promises_title)
2624 .setMessage(R.string.abandoned_promise_explanation)
2625 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2626 .setNeutralButton(R.string.abandoned_clean_this,
2627 new DialogInterface.OnClickListener() {
2628 public void onClick(DialogInterface dialog, int id) {
2629 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2630 mWorkspace.removeAbandonedPromise(packageName, user);
2631 }
2632 })
2633 .create().show();
2634 return;
2635 }
2636
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 /**
2638 * Event handler for an app shortcut click.
2639 *
2640 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2641 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2644 Object tag = v.getTag();
2645 if (!(tag instanceof ShortcutInfo)) {
2646 throw new IllegalArgumentException("Input must be a Shortcut");
2647 }
2648
2649 // Open shortcut
2650 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651
2652 if (shortcut.isDisabled != 0) {
2653 int error = R.string.activity_not_available;
2654 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2655 error = R.string.safemode_shortcut_error;
2656 }
2657 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2658 return;
2659 }
2660
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 final Intent intent = shortcut.intent;
2662
2663 // Check for special shortcuts
2664 if (intent.getComponent() != null) {
2665 final String shortcutClass = intent.getComponent().getClassName();
2666
2667 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2668 MemoryDumpActivity.startDump(this);
2669 return;
2670 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2671 toggleShowWeightWatcher();
2672 return;
2673 }
2674 }
2675
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002677 if ((v instanceof BubbleTextView)
2678 && shortcut.isPromise()
2679 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002680 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002681 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 new DialogInterface.OnClickListener() {
2683 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002684 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 return;
2688 }
2689
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002691 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002692
2693 if (mLauncherCallbacks != null) {
2694 mLauncherCallbacks.onClickAppShortcut(v);
2695 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 }
2697
Adam Cohen091440a2015-03-18 14:16:05 -07002698 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002700 final ShortcutInfo shortcut;
2701 final Intent intent;
2702 if (tag instanceof ShortcutInfo) {
2703 shortcut = (ShortcutInfo) tag;
2704 intent = shortcut.intent;
2705 int[] pos = new int[2];
2706 v.getLocationOnScreen(pos);
2707 intent.setSourceBounds(new Rect(pos[0], pos[1],
2708 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002709
Sunny Goyal508da152014-08-14 10:53:27 -07002710 } else if (tag instanceof AppInfo) {
2711 shortcut = null;
2712 intent = ((AppInfo) tag).intent;
2713 } else {
2714 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2715 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716
2717 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 mStats.recordLaunch(intent, shortcut);
2719
2720 if (success && v instanceof BubbleTextView) {
2721 mWaitingForResume = (BubbleTextView) v;
2722 mWaitingForResume.setStayPressed(true);
2723 }
2724 }
2725
2726 /**
2727 * Event handler for a folder icon click.
2728 *
2729 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2730 */
2731 protected void onClickFolderIcon(View v) {
2732 if (LOGD) Log.d(TAG, "onClickFolder");
2733 if (!(v instanceof FolderIcon)){
2734 throw new IllegalArgumentException("Input must be a FolderIcon");
2735 }
2736
2737 FolderIcon folderIcon = (FolderIcon) v;
2738 final FolderInfo info = folderIcon.getFolderInfo();
2739 Folder openFolder = mWorkspace.getFolderForTag(info);
2740
2741 // If the folder info reports that the associated folder is open, then verify that
2742 // it is actually opened. There have been a few instances where this gets out of sync.
2743 if (info.opened && openFolder == null) {
2744 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2745 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2746 info.opened = false;
2747 }
2748
2749 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2750 // Close any open folder
2751 closeFolder();
2752 // Open the requested folder
2753 openFolder(folderIcon);
2754 } else {
2755 // Find the open folder...
2756 int folderScreen;
2757 if (openFolder != null) {
2758 folderScreen = mWorkspace.getPageForView(openFolder);
2759 // .. and close it
2760 closeFolder(openFolder);
2761 if (folderScreen != mWorkspace.getCurrentPage()) {
2762 // Close any folder open on the current screen
2763 closeFolder();
2764 // Pull the folder onto this screen
2765 openFolder(folderIcon);
2766 }
2767 }
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onClickFolderIcon(v);
2772 }
Michael Jurka5130e402011-10-13 04:55:35 -07002773 }
2774
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002775 /**
2776 * Event handler for the (Add) Widgets button that appears after a long press
2777 * on the home screen.
2778 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002779 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002780 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002781 if (mIsSafeModeEnabled) {
2782 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2783 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002784 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002785 if (mLauncherCallbacks != null) {
2786 mLauncherCallbacks.onClickAddWidgetButton(view);
2787 }
Adam Cohen9211d422014-10-07 18:14:53 -07002788 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002789 }
2790
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 /**
2792 * Event handler for the wallpaper picker button that appears after a long press
2793 * on the home screen.
2794 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002795 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002797 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2798 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002799
2800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onClickWallpaperPicker(v);
2802 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 }
2804
2805 /**
2806 * Event handler for a click on the settings button that appears after a long press
2807 * on the home screen.
2808 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002809 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002810 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002811 if (mLauncherCallbacks != null) {
2812 mLauncherCallbacks.onClickSettingsButton(v);
2813 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002814 }
2815
Michael Jurka5130e402011-10-13 04:55:35 -07002816 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002817 // Provide the same haptic feedback that the system offers for virtual keys.
2818 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002819 }
2820
Adam Cohen61f560d2013-09-30 15:58:20 -07002821 public void performHapticFeedbackOnTouchDown(View v) {
2822 // Provide the same haptic feedback that the system offers for virtual keys.
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825
2826 public View.OnTouchListener getHapticFeedbackTouchListener() {
2827 if (mHapticFeedbackTouchListener == null) {
2828 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2829 @Override
2830 public boolean onTouch(View v, MotionEvent event) {
2831 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2832 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2833 }
2834 return false;
2835 }
2836 };
2837 }
2838 return mHapticFeedbackTouchListener;
2839 }
2840
Adam Cohen9211d422014-10-07 18:14:53 -07002841 public void onDragStarted(View view) {
2842 if (isOnCustomContent()) {
2843 // Custom content screen doesn't participate in drag and drop. If on custom
2844 // content screen, move to default.
2845 moveWorkspaceToDefaultScreen();
2846 }
2847
2848 if (mLauncherCallbacks != null) {
2849 mLauncherCallbacks.onDragStarted(view);
2850 }
2851 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002852
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853 /**
2854 * Called when the user stops interacting with the launcher.
2855 * This implies that the user is now on the homescreen and is not doing housekeeping.
2856 */
Adam Cohen9211d422014-10-07 18:14:53 -07002857 protected void onInteractionEnd() {
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onInteractionEnd();
2860 }
2861 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002862
2863 /**
2864 * Called when the user starts interacting with the launcher.
2865 * The possible interactions are:
2866 * - open all apps
2867 * - reorder an app shortcut, or a widget
2868 * - open the overview mode.
2869 * This is a good time to stop doing things that only make sense
2870 * when the user is on the homescreen and not doing housekeeping.
2871 */
Adam Cohen9211d422014-10-07 18:14:53 -07002872 protected void onInteractionBegin() {
2873 if (mLauncherCallbacks != null) {
2874 mLauncherCallbacks.onInteractionBegin();
2875 }
2876 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002877
Kenny Guyf07af7b2014-07-31 11:39:16 +01002878 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002879 try {
2880 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002881 launcherApps.showAppDetailsForProfile(componentName, user);
2882 } catch (SecurityException e) {
2883 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2884 Log.e(TAG, "Launcher does not have permission to launch settings");
2885 } catch (ActivityNotFoundException e) {
2886 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2887 Log.e(TAG, "Unable to launch settings");
2888 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002889 }
2890
Michael Jurka1e2f4652013-07-08 18:03:46 -07002891 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002892 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2893 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002894 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002895 // System applications cannot be installed. For now, show a toast explaining that.
2896 // We may give them the option of disabling apps this way.
2897 int messageId = R.string.uninstall_system_app_text;
2898 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002899 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002900 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002901 String packageName = componentName.getPackageName();
2902 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002903 Intent intent = new Intent(
2904 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002905 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2906 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002907 if (user != null) {
2908 user.addToIntent(intent, Intent.EXTRA_USER);
2909 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002910 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002911 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002912 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002913 }
2914
Michael Jurka86a720e2012-05-09 11:23:23 -07002915 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002916 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002918 // Only launch using the new animation if the shortcut has not opted out (this is a
2919 // private contract between launcher and may be ignored in the future).
2920 boolean useLaunchAnimation = (v != null) &&
2921 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002922 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2923 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002924
Kenny Guy1317e2d2014-05-08 18:52:50 +01002925 UserHandleCompat user = null;
2926 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2927 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2928 user = userManager.getUserForSerialNumber(serialNumber);
2929 }
2930
2931 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002932 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002933 ActivityOptions opts = null;
2934 if (sClipRevealMethod != null) {
2935 // TODO: call method directly when Launcher3 can depend on M APIs
2936 int left = 0, top = 0;
2937 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2938 if (v instanceof TextView) {
2939 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002940 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2941 if (icon != null) {
2942 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002943 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002944 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002945 width = bounds.width();
2946 height = bounds.height();
2947 }
2948 }
2949 try {
2950 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2951 left, top, width, height);
2952 } catch (IllegalAccessException e) {
2953 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2954 sClipRevealMethod = null;
2955 } catch (InvocationTargetException e) {
2956 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2957 sClipRevealMethod = null;
2958 }
2959 }
2960 if (opts == null) {
2961 opts = Utilities.isLmpOrAbove() ?
2962 ActivityOptions.makeCustomAnimation(this,
2963 R.anim.task_open_enter, R.anim.no_anim) :
2964 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2965 v.getMeasuredWidth(), v.getMeasuredHeight());
2966 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002967 optsBundle = opts.toBundle();
2968 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002969
2970 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2971 // Could be launching some bookkeeping activity
2972 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002973 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002974 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002975 launcherApps.startActivityForProfile(intent.getComponent(), user,
2976 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002977 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002978 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 } catch (SecurityException e) {
2980 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002981 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002983 "or use the exported attribute for this activity. "
2984 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002986 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002988
Michael Jurka86a720e2012-05-09 11:23:23 -07002989 boolean startActivitySafely(View v, Intent intent, Object tag) {
2990 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002991 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2992 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2993 return false;
2994 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002995 try {
2996 success = startActivity(v, intent, tag);
2997 } catch (ActivityNotFoundException e) {
2998 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2999 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3000 }
3001 return success;
3002 }
3003
Adam Cohen268c4752012-06-06 17:47:33 -07003004 /**
3005 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3006 * in the DragLayer in the exact absolute location of the original FolderIcon.
3007 */
3008 private void copyFolderIconToImage(FolderIcon fi) {
3009 final int width = fi.getMeasuredWidth();
3010 final int height = fi.getMeasuredHeight();
3011
3012 // Lazy load ImageView, Bitmap and Canvas
3013 if (mFolderIconImageView == null) {
3014 mFolderIconImageView = new ImageView(this);
3015 }
3016 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3017 mFolderIconBitmap.getHeight() != height) {
3018 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3019 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3020 }
3021
3022 DragLayer.LayoutParams lp;
3023 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3024 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3025 } else {
3026 lp = new DragLayer.LayoutParams(width, height);
3027 }
3028
Adam Cohen307fe232012-08-16 17:55:58 -07003029 // The layout from which the folder is being opened may be scaled, adjust the starting
3030 // view size by this scale factor.
3031 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003032 lp.customPosition = true;
3033 lp.x = mRectForFolderAnimation.left;
3034 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003035 lp.width = (int) (scale * width);
3036 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003037
3038 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3039 fi.draw(mFolderIconCanvas);
3040 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003041 if (fi.getFolder() != null) {
3042 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3043 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003044 }
Adam Cohen268c4752012-06-06 17:47:33 -07003045 // Just in case this image view is still in the drag layer from a previous animation,
3046 // we remove it and re-add it.
3047 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3048 mDragLayer.removeView(mFolderIconImageView);
3049 }
3050 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003051 if (fi.getFolder() != null) {
3052 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003053 }
Adam Cohen268c4752012-06-06 17:47:33 -07003054 }
3055
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003057 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003058 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3059 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3060 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3061
Adam Cohenc51934b2011-07-26 21:07:43 -07003062 FolderInfo info = (FolderInfo) fi.getTag();
3063 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3064 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003065 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3066 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003067 }
3068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3070 copyFolderIconToImage(fi);
3071 fi.setVisibility(View.INVISIBLE);
3072
Michael Jurka2ecf9952012-06-18 12:52:28 -07003073 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003074 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003075 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003076 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3077 }
3078 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003079 oa.start();
3080 }
3081
Adam Cohen268c4752012-06-06 17:47:33 -07003082 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003083 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003084 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3085 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3086 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3087
Adam Cohen268c4752012-06-06 17:47:33 -07003088 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003089
Adam Cohen268c4752012-06-06 17:47:33 -07003090 // We remove and re-draw the FolderIcon in-case it has changed
3091 mDragLayer.removeView(mFolderIconImageView);
3092 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003093 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003094 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003095 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003096 oa.addListener(new AnimatorListenerAdapter() {
3097 @Override
3098 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003099 if (cl != null) {
3100 cl.clearFolderLeaveBehind();
3101 // Remove the ImageView copy of the FolderIcon and make the original visible.
3102 mDragLayer.removeView(mFolderIconImageView);
3103 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003104 }
3105 }
3106 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003107 oa.start();
3108 }
3109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003111 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 * is animated relative to the specified View. If the View is null, no animation
3113 * is played.
3114 *
3115 * @param folderInfo The FolderInfo describing the folder to open.
3116 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003117 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003118 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003119 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120
Adam Cohena9cf38f2011-05-02 15:36:58 -07003121 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 // Just verify that the folder hasn't already been added to the DragLayer.
3124 // There was a one-off crash where the folder had a parent already.
3125 if (folder.getParent() == null) {
3126 mDragLayer.addView(folder);
3127 mDragController.addDropTarget((DropTarget) folder);
3128 } else {
3129 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3130 folder.getParent() + ").");
3131 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003132 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003133 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003134
3135 // Notify the accessibility manager that this folder "window" has appeared and occluded
3136 // the workspace items
3137 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3138 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003139 }
3140
3141 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003142 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003143 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003144 if (folder.isEditingName()) {
3145 folder.dismissEditingName();
3146 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003147 closeFolder(folder);
3148 }
3149 }
3150
3151 void closeFolder(Folder folder) {
3152 folder.getInfo().opened = false;
3153
3154 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3155 if (parent != null) {
3156 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3157 shrinkAndFadeInFolderIcon(fi);
3158 }
3159 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003160
3161 // Notify the accessibility manager that this folder "window" has disappeard and no
3162 // longer occludeds the workspace items
3163 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
3165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003166 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003167 if (!isDraggingEnabled()) return false;
3168 if (isWorkspaceLocked()) return false;
3169 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170
Adam Cohen1697b792013-09-17 19:08:21 -07003171 if (v instanceof Workspace) {
3172 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003173 if (mWorkspace.enterOverviewMode()) {
3174 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3175 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3176 return true;
3177 } else {
3178 return false;
3179 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003180 } else {
3181 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003182 }
Adam Cohen1697b792013-09-17 19:08:21 -07003183 }
3184
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003185 CellLayout.CellInfo longClickCellInfo = null;
3186 View itemUnderLongClick = null;
3187 if (v.getTag() instanceof ItemInfo) {
3188 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003189 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003190 itemUnderLongClick = longClickCellInfo.cell;
3191 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
3193
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 // The hotseat touch handling does not go through Workspace, and we always allow long press
3195 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003196 final boolean inHotseat = isHotseatLayout(v);
3197 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003199 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003200 // User long pressed on empty space
3201 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3202 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003203 if (mWorkspace.isInOverviewMode()) {
3204 mWorkspace.startReordering(v);
3205 } else {
3206 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003209 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3210 mHotseat.getOrderInHotseat(
3211 longClickCellInfo.cellX,
3212 longClickCellInfo.cellY));
3213 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003215 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 }
3217 }
3218 }
3219 return true;
3220 }
3221
Winson Chung3d503fb2011-07-13 17:25:49 -07003222 boolean isHotseatLayout(View layout) {
3223 return mHotseat != null && layout != null &&
3224 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3225 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003226
Winson Chung3d503fb2011-07-13 17:25:49 -07003227 /**
3228 * Returns the CellLayout of the specified container at the specified screen.
3229 */
Sunny Goyal92820592015-03-02 11:31:35 -08003230 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3232 if (mHotseat != null) {
3233 return mHotseat.getLayout();
3234 } else {
3235 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003236 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003238 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003239 }
3240 }
3241
Winson Chungb745afb2015-03-02 11:51:23 -08003242 /**
3243 * For overridden classes.
3244 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003245 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003246 return isAppsViewVisible();
3247 }
3248
3249 public boolean isAppsViewVisible() {
3250 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3251 }
3252
3253 public boolean isWidgetsViewVisible() {
3254 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003255 }
3256
Craig Mautner360310b2012-10-26 15:13:08 -07003257 private void setWorkspaceBackground(boolean workspace) {
3258 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003259 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003260 }
3261
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003262 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003263 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3264 int curflags = getWindow().getAttributes().flags
3265 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3266 if (wpflags != curflags) {
3267 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3268 }
Craig Mautner360310b2012-10-26 15:13:08 -07003269 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003270 }
3271
Michael Jurkae326f182011-11-21 14:05:46 -08003272 @Override
3273 public void onTrimMemory(int level) {
3274 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003275 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3276 // The widget preview db can result in holding onto over
3277 // 3MB of memory for caching which isn't necessary.
3278 SQLiteDatabase.releaseMemory();
3279
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003280 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003281 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003282 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003283 }
Michael Jurkae326f182011-11-21 14:05:46 -08003284 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003285 if (mLauncherCallbacks != null) {
3286 mLauncherCallbacks.onTrimMemory(level);
3287 }
Michael Jurkae326f182011-11-21 14:05:46 -08003288 }
3289
Winson Chungb745afb2015-03-02 11:51:23 -08003290 @Override
3291 public void onStateTransitionHideSearchBar() {
3292 // Hide the search bar
3293 if (mSearchDropTargetBar != null) {
3294 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3295 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003296 }
3297
Winson Chungb745afb2015-03-02 11:51:23 -08003298 protected void showWorkspace(boolean animated) {
3299 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003300 }
3301
Adam Cohened66b2b2012-01-23 17:28:51 -08003302 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003303 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003304 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003306 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3307 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003308
Winson Chungc7d2b602012-05-16 17:02:20 -07003309 // Show the search bar (only animate if we were showing the drop target bar in spring
3310 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003311 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003312 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003313 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003314
Michael Jurkab3e22d92011-10-31 15:58:33 -07003315 // Set focus to the AppsCustomize button
3316 if (mAllAppsButton != null) {
3317 mAllAppsButton.requestFocus();
3318 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003319 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003320
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003321 // Change the state *after* we've called all the transition code
3322 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003323
Winson Chung5fb63472011-02-02 17:03:37 -08003324 // Resume the auto-advance of widgets
3325 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003326 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003327
alanv1d4fde62012-10-17 13:15:47 -07003328 // Send an accessibility event to announce the context change
3329 getWindow().getDecorView()
3330 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003331
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003332 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003333 }
3334
Adam Cohened307df2013-10-02 09:37:31 -07003335 void showOverviewMode(boolean animated) {
3336 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003337 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3338 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003339 mState = State.WORKSPACE;
3340 onWorkspaceShown(animated);
3341 }
3342
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003343 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003344 }
3345
Winson Chungb745afb2015-03-02 11:51:23 -08003346 /**
3347 * Shows the apps view.
3348 */
3349 void showAppsView(boolean animated, boolean resetListToTop) {
3350 if (resetListToTop) {
3351 mAppsView.scrollToTop();
3352 }
3353 showAppsOrWidgets(animated, State.APPS);
3354 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355
Winson Chungb745afb2015-03-02 11:51:23 -08003356 /**
3357 * Shows the widgets view.
3358 */
3359 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003360 if (resetPageToZero) {
3361 mAppsCustomizeTabHost.reset();
3362 }
Winson Chungb745afb2015-03-02 11:51:23 -08003363 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003364 mAppsCustomizeTabHost.post(new Runnable() {
3365 @Override
3366 public void run() {
3367 // We post this in-case the all apps view isn't yet constructed.
3368 mAppsCustomizeTabHost.requestFocus();
3369 }
3370 });
Winson Chungb745afb2015-03-02 11:51:23 -08003371 }
3372
3373 /**
3374 * Sets up the transition to show the apps/widgets view.
3375 */
3376 private void showAppsOrWidgets(boolean animated, State toState) {
3377 if (mState != State.WORKSPACE) return;
3378 if (toState != State.APPS && toState != State.WIDGETS) return;
3379
3380 if (toState == State.APPS) {
3381 mStateTransitionAnimation.startAnimationToAllApps(animated);
3382 } else {
3383 mStateTransitionAnimation.startAnimationToWidgets(animated);
3384 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003385
Michael Jurkab3e22d92011-10-31 15:58:33 -07003386 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003387 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003388
3389 // Pause the auto-advance of widgets until we are out of AllApps
3390 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003391 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392 closeFolder();
3393
3394 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003395 getWindow().getDecorView()
3396 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003397 }
3398
Adam Cohen7777d962011-08-18 18:58:38 -07003399 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003400 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3401 mState == State.WIDGETS_SPRING_LOADED) {
3402 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003403 }
Winson Chungb745afb2015-03-02 11:51:23 -08003404
3405 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3406 true /* animated */, null /* onCompleteRunnable */);
3407 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003408 }
Adam Cohen7777d962011-08-18 18:58:38 -07003409
Adam Cohenad4e15c2013-10-17 16:21:35 -07003410 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003411 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003412 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003413
Winson Chunge7a03942011-08-05 15:05:12 -07003414 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003415 @Override
3416 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003417 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003418 // Before we show workspace, hide all apps again because
3419 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3420 // clean up our state transition functions
3421 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003422 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003423 } else {
3424 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003425 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003426 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003427 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003428 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429
Michael Jurkad3ef3062010-11-23 16:23:58 -08003430 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003431 if (mState == State.APPS_SPRING_LOADED) {
3432 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3433 mState = State.APPS;
3434 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3435 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3436 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 }
3438 // Otherwise, we are not in spring loaded mode, so don't do anything.
3439 }
3440
Michael Jurkab3e22d92011-10-31 15:58:33 -07003441 void lockAllApps() {
3442 // TODO
3443 }
3444
3445 void unlockAllApps() {
3446 // TODO
3447 }
3448
Sunny Goyal594d76d2014-11-06 10:12:54 -08003449 protected void disableVoiceButtonProxy(boolean disable) {
3450 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003451 }
3452
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003453 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003454 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3455 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003456 }
3457
Adam Cohen24ce0b32014-01-14 16:18:14 -08003458 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003459 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3460 if (searchProvider == null) {
3461 return null;
3462 }
3463
3464 Bundle opts = new Bundle();
3465 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003466 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003467
3468 SharedPreferences sp = getSharedPreferences(
3469 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3470 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003471 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003472 if (!searchProvider.provider.flattenToString().equals(
3473 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003474 || (widgetInfo == null)
3475 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003476 // A valid widget is not already bound.
3477 if (widgetId > -1) {
3478 mAppWidgetHost.deleteAppWidgetId(widgetId);
3479 widgetId = -1;
3480 }
3481
3482 // Try to bind a new widget
3483 widgetId = mAppWidgetHost.allocateAppWidgetId();
3484
3485 if (!AppWidgetManagerCompat.getInstance(this)
3486 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3487 mAppWidgetHost.deleteAppWidgetId(widgetId);
3488 widgetId = -1;
3489 }
3490
3491 sp.edit()
3492 .putInt(QSB_WIDGET_ID, widgetId)
3493 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3494 .commit();
3495 }
3496
3497 if (widgetId != -1) {
3498 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3499 mQsb.updateAppWidgetOptions(opts);
3500 mQsb.setPadding(0, 0, 0, 0);
3501 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003502 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003503 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003504 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003505 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003506 }
3507
Michael Jurkad7c28052012-04-27 15:43:36 -07003508 @Override
3509 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003510 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003511 final List<CharSequence> text = event.getText();
3512 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003513 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003514 if (mState == State.APPS) {
3515 text.add("Apps");
3516 } else if (mState == State.WIDGETS) {
3517 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003518 } else {
3519 text.add(getString(R.string.all_apps_home_button_label));
3520 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003521 return result;
3522 }
3523
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003524 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003525 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003526 */
Adam Cohen091440a2015-03-18 14:16:05 -07003527 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003528 @Override
3529 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003530 closeSystemDialogs();
3531 }
3532 }
3533
3534 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003535 * Receives notifications whenever the appwidgets are reset.
3536 */
3537 private class AppWidgetResetObserver extends ContentObserver {
3538 public AppWidgetResetObserver() {
3539 super(new Handler());
3540 }
3541
3542 @Override
3543 public void onChange(boolean selfChange) {
3544 onAppWidgetReset();
3545 }
3546 }
3547
3548 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 * If the activity is currently paused, signal that we need to run the passed Runnable
3550 * in onResume.
3551 *
3552 * This needs to be called from incoming places where resources might have been loaded
3553 * while we are paused. That is becaues the Configuration might be wrong
3554 * when we're not running, and if it comes back to what it was when we
3555 * were paused, we are not restarted.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 *
3559 * @return true if we are currently paused. The caller might be able to
3560 * skip some work in that case since we will come back again.
3561 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003562 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003563 if (mPaused) {
3564 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003565 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003566 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003567 }
3568 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003569 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003570 return true;
3571 } else {
3572 return false;
3573 }
3574 }
3575
Michael Jurkac402cd92013-05-20 15:49:32 +02003576 private boolean waitUntilResume(Runnable run) {
3577 return waitUntilResume(run, false);
3578 }
3579
Michael Jurka1e2f4652013-07-08 18:03:46 -07003580 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003581 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003582 }
3583
Michael Jurka7607c2f2013-04-03 14:33:19 -07003584 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003585 * If the activity is currently paused, signal that we need to re-run the loader
3586 * in onResume.
3587 *
3588 * This needs to be called from incoming places where resources might have been loaded
3589 * while we are paused. That is becaues the Configuration might be wrong
3590 * when we're not running, and if it comes back to what it was when we
3591 * were paused, we are not restarted.
3592 *
3593 * Implementation of the method from LauncherModel.Callbacks.
3594 *
3595 * @return true if we are currently paused. The caller might be able to
3596 * skip some work in that case since we will come back again.
3597 */
3598 public boolean setLoadOnResume() {
3599 if (mPaused) {
3600 Log.i(TAG, "setLoadOnResume");
3601 mOnResumeNeedsLoad = true;
3602 return true;
3603 } else {
3604 return false;
3605 }
3606 }
3607
3608 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003610 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003612 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003613 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003614 } else {
3615 return SCREEN_COUNT / 2;
3616 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003618
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 /**
3620 * Refreshes the shortcuts shown on the workspace.
3621 *
3622 * Implementation of the method from LauncherModel.Callbacks.
3623 */
3624 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003625 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003626
Michael Jurka7607c2f2013-04-03 14:33:19 -07003627 // If we're starting binding all over again, clear any bind calls we'd postponed in
3628 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3629 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003630 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003631
Winson Chungd64d1762013-08-20 14:37:16 -07003632 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003633 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003634 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003635
Michael Jurka05bf644e2011-11-30 20:28:53 -08003636 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003637 if (mHotseat != null) {
3638 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639 }
3640 }
3641
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 @Override
3643 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003644 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003645
Adam Cohen5084cba2013-09-03 12:01:16 -07003646 // If there are no screens, we need to have an empty screen
3647 if (orderedScreenIds.size() == 0) {
3648 mWorkspace.addExtraEmptyScreen();
3649 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003650
3651 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003652 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003653 if (hasCustomContentToLeft()) {
3654 mWorkspace.createCustomContentContainer();
3655 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003656 }
Winson Chung64359a52013-07-08 17:17:08 -07003657 }
3658
3659 @Override
3660 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003661 // Log to disk
3662 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3663 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3664 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003665 int count = orderedScreenIds.size();
3666 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003667 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003668 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003669 }
3670
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003671 @Override
3672 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3673 final long screenId, final int[] cell, final int spanX, final int spanY) {
3674 showWorkspace(true, new Runnable() {
3675
3676 @Override
3677 public void run() {
3678 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3679 addPendingItem(info, container, screenId, cell, spanX, spanY);
3680 }
3681 });
3682 }
3683
Adam Cohen39a06042013-07-19 14:30:12 -07003684 private boolean shouldShowWeightWatcher() {
3685 String spKey = LauncherAppState.getSharedPreferencesKey();
3686 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003687 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003688
3689 return show;
3690 }
3691
3692 private void toggleShowWeightWatcher() {
3693 String spKey = LauncherAppState.getSharedPreferencesKey();
3694 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3695 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3696
3697 show = !show;
3698
3699 SharedPreferences.Editor editor = sp.edit();
3700 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3701 editor.commit();
3702
3703 if (mWeightWatcher != null) {
3704 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3705 }
3706 }
3707
Winson Chungd64d1762013-08-20 14:37:16 -07003708 public void bindAppsAdded(final ArrayList<Long> newScreens,
3709 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003710 final ArrayList<ItemInfo> addAnimated,
3711 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003712 Runnable r = new Runnable() {
3713 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003714 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003715 }
3716 };
3717 if (waitUntilResume(r)) {
3718 return;
3719 }
3720
Winson Chungd64d1762013-08-20 14:37:16 -07003721 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003722 if (newScreens != null) {
3723 bindAddScreens(newScreens);
3724 }
Winson Chungd64d1762013-08-20 14:37:16 -07003725
3726 // We add the items without animation on non-visible pages, and with
3727 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003728 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003729 bindItems(addNotAnimated, 0,
3730 addNotAnimated.size(), false);
3731 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003732 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003733 bindItems(addAnimated, 0,
3734 addAnimated.size(), true);
3735 }
Winson Chungc58497e2013-09-03 17:48:37 -07003736
Winson Chung87412982013-10-03 18:34:14 -07003737 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003738 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003739
Winson Chungb745afb2015-03-02 11:51:23 -08003740 if (addedApps != null && mAppsView != null) {
3741 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003742 }
Winson Chungd64d1762013-08-20 14:37:16 -07003743 }
3744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003745 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 * Bind the items start-end from the list.
3747 *
3748 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003749 */
Winson Chung64359a52013-07-08 17:17:08 -07003750 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3751 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003752 Runnable r = new Runnable() {
3753 public void run() {
3754 bindItems(shortcuts, start, end, forceAnimateIcons);
3755 }
3756 };
3757 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003758 return;
3759 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003760
Winson Chungf0c6ae02012-03-21 16:10:31 -07003761 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003762 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3763 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003764 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003766 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003767 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003769
3770 // Short circuit if we are loading dock items for a configuration which has no dock
3771 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3772 mHotseat == null) {
3773 continue;
3774 }
3775
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 switch (item.itemType) {
3777 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3778 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003779 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003780 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003781
Winson Chung64359a52013-07-08 17:17:08 -07003782 /*
3783 * TODO: FIX collision case
3784 */
Winson Chungce376632013-07-11 16:12:41 -07003785 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3786 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3787 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003788 View v = cl.getChildAt(item.cellX, item.cellY);
3789 Object tag = v.getTag();
3790 String desc = "Collision while binding workspace item: " + item
3791 + ". Collides with " + tag;
3792 if (LauncherAppState.isDogfoodBuild()) {
3793 throw (new RuntimeException(desc));
3794 } else {
3795 Log.d(TAG, desc);
3796 }
Winson Chungce376632013-07-11 16:12:41 -07003797 }
Winson Chung64359a52013-07-08 17:17:08 -07003798 }
3799
Adam Cohendcd297f2013-06-18 13:13:40 -07003800 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3801 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003802 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003803 // Animate all the applications up now
3804 shortcut.setAlpha(0f);
3805 shortcut.setScaleX(0f);
3806 shortcut.setScaleY(0f);
3807 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003808 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003811 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003812 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003813 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003814 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003815 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3816 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003818 default:
3819 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003822
Winson Chung997a9232013-07-24 15:33:46 -07003823 if (animateIcons) {
3824 // Animate to the correct page
3825 if (newShortcutsScreenId > -1) {
3826 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003827 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003828 final Runnable startBounceAnimRunnable = new Runnable() {
3829 public void run() {
3830 anim.playTogether(bounceAnims);
3831 anim.start();
3832 }
3833 };
Winson Chung997a9232013-07-24 15:33:46 -07003834 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003835 // We post the animation slightly delayed to prevent slowdowns
3836 // when we are loading right after we return to launcher.
3837 mWorkspace.postDelayed(new Runnable() {
3838 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003839 if (mWorkspace != null) {
3840 mWorkspace.snapToPage(newScreenIndex);
3841 mWorkspace.postDelayed(startBounceAnimRunnable,
3842 NEW_APPS_ANIMATION_DELAY);
3843 }
Winson Chung94d67682013-09-25 16:29:40 -07003844 }
3845 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003846 } else {
3847 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003848 }
3849 }
Winson Chung64359a52013-07-08 17:17:08 -07003850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003852 }
3853
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 /**
3855 * Implementation of the method from LauncherModel.Callbacks.
3856 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003857 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003858 Runnable r = new Runnable() {
3859 public void run() {
3860 bindFolders(folders);
3861 }
3862 };
3863 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003864 return;
3865 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003866 sFolders.clear();
3867 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869
3870 /**
3871 * Add the views for a widget to the workspace.
3872 *
3873 * Implementation of the method from LauncherModel.Callbacks.
3874 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003875 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003876 Runnable r = new Runnable() {
3877 public void run() {
3878 bindAppWidget(item);
3879 }
3880 };
3881 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 return;
3883 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003884
Daniel Sandler843e8602010-06-07 14:59:01 -04003885 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3886 if (DEBUG_WIDGETS) {
3887 Log.d(TAG, "bindAppWidget: " + item);
3888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 final Workspace workspace = mWorkspace;
3890
Adam Cohen59400422014-03-05 18:07:04 -08003891 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003892 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003893
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003894 if (!mIsSafeModeEnabled
3895 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3896 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003897 if (appWidgetInfo == null) {
3898 if (DEBUG_WIDGETS) {
3899 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3900 + " belongs to component " + item.providerName
3901 + ", as the povider is null");
3902 }
3903 LauncherModel.deleteItemFromDatabase(this, item);
3904 return;
3905 }
3906 // Note: This assumes that the id remap broadcast is received before this step.
3907 // If that is not the case, the id remap will be ignored and user may see the
3908 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003909 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003910 pendingInfo.spanX = item.spanX;
3911 pendingInfo.spanY = item.spanY;
3912 pendingInfo.minSpanX = item.minSpanX;
3913 pendingInfo.minSpanY = item.minSpanY;
3914 Bundle options =
3915 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3916
Sunny Goyalff572272014-07-23 13:58:07 -07003917 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003918 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3919 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003920
3921 // TODO consider showing a permission dialog when the widget is clicked.
3922 if (!success) {
3923 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3924 if (DEBUG_WIDGETS) {
3925 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3926 + " belongs to component " + item.providerName
3927 + ", as the launcher is unable to bing a new widget id");
3928 }
3929 LauncherModel.deleteItemFromDatabase(this, item);
3930 return;
3931 }
3932
3933 item.appWidgetId = newWidgetId;
3934
3935 // If the widget has a configure activity, it is still needs to set it up, otherwise
3936 // the widget is ready to go.
3937 item.restoreStatus = (appWidgetInfo.configure == null)
3938 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3939 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3940
3941 LauncherModel.updateItemInDatabase(this, item);
3942 }
3943
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003944 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003945 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003946 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003947 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3948 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003949 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003950
Sunny Goyal651077b2014-06-30 14:15:31 -07003951 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3952 } else {
3953 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003954 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3955 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003956 view.updateIcon(mIconCache);
3957 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003958 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003959 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003963 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964
Adam Cohendcd297f2013-06-18 13:13:40 -07003965 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003967 if (!item.isCustomWidget()) {
3968 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3969 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003970
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971 workspace.requestLayout();
3972
Daniel Sandler843e8602010-06-07 14:59:01 -04003973 if (DEBUG_WIDGETS) {
3974 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3975 + (SystemClock.uptimeMillis()-start) + "ms");
3976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 }
3978
Sunny Goyalff572272014-07-23 13:58:07 -07003979 /**
3980 * Restores a pending widget.
3981 *
3982 * @param appWidgetId The app widget id
3983 * @param cellInfo The position on screen where to create the widget.
3984 */
3985 private void completeRestoreAppWidget(final int appWidgetId) {
3986 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3987 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3988 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3989 return;
3990 }
3991
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003992 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003993 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3994
3995 mWorkspace.reinflateWidgetsIfNecessary();
3996 LauncherModel.updateItemInDatabase(this, info);
3997 }
3998
Adam Cohen1462de32012-07-24 22:34:36 -07003999 public void onPageBoundSynchronously(int page) {
4000 mSynchronouslyBoundPages.add(page);
4001 }
4002
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 /**
4004 * Callback saying that there aren't any more items to bind.
4005 *
4006 * Implementation of the method from LauncherModel.Callbacks.
4007 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004008 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004009 Runnable r = new Runnable() {
4010 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004011 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004012 }
4013 };
4014 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004015 return;
4016 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004017 if (mSavedState != null) {
4018 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004019 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004020 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004021 mSavedState = null;
4022 }
4023
Adam Cohen1462de32012-07-24 22:34:36 -07004024 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004025
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004026 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004027 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004028
4029 // If we received the result of any pending adds while the loader was running (e.g. the
4030 // widget configuration forced an orientation change), process them now.
4031 if (sPendingAddItem != null) {
4032 final long screenId = completeAdd(sPendingAddItem);
4033
4034 // TODO: this moves the user to the page where the pending item was added. Ideally,
4035 // the screen would be guaranteed to exist after bind, and the page would be set through
4036 // the workspace restore process.
4037 mWorkspace.post(new Runnable() {
4038 @Override
4039 public void run() {
4040 mWorkspace.snapToScreenId(screenId);
4041 }
4042 });
4043 sPendingAddItem = null;
4044 }
4045
Sunny Goyale755d462014-07-22 13:48:29 -07004046 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004047
4048 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004049 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004050 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004051 }
4052
Adam Cohen3ed316a2014-07-23 18:21:20 -07004053 private void sendLoadingCompleteBroadcastIfNecessary() {
4054 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4055 String permission =
4056 getResources().getString(R.string.receive_first_load_broadcast_permission);
4057 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4058 sendBroadcast(intent, permission);
4059 SharedPreferences.Editor editor = mSharedPrefs.edit();
4060 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4061 editor.apply();
4062 }
4063 }
4064
Winson Chunga0b7e862013-09-05 16:03:15 -07004065 public boolean isAllAppsButtonRank(int rank) {
4066 if (mHotseat != null) {
4067 return mHotseat.isAllAppsButtonRank(rank);
4068 }
4069 return false;
4070 }
4071
Winson Chunga2413752012-04-03 14:22:34 -07004072 private boolean canRunNewAppsAnimation() {
4073 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4074 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4075 }
4076
Winson Chungc9168342013-06-26 14:54:55 -07004077 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4078 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4079 PropertyValuesHolder.ofFloat("alpha", 1f),
4080 PropertyValuesHolder.ofFloat("scaleX", 1f),
4081 PropertyValuesHolder.ofFloat("scaleY", 1f));
4082 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4083 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4084 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4085 return bounceAnim;
4086 }
4087
Adam Cohen27772732013-11-11 14:00:56 +00004088 public boolean useVerticalBarLayout() {
4089 return LauncherAppState.getInstance().getDynamicGrid().
4090 getDeviceProfile().isVerticalBarLayout();
4091 }
4092
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004093 protected Rect getSearchBarBounds() {
4094 return LauncherAppState.getInstance().getDynamicGrid().
4095 getDeviceProfile().getSearchBarBounds();
4096 }
4097
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004098 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004099 if (mSearchDropTargetBar == null) {
4100 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004101 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004102 if (mQsb != null) {
4103 mSearchDropTargetBar.removeView(mQsb);
4104 mQsb = null;
4105 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004106 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004107 }
4108
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004109 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 * Add the icons for all apps.
4111 *
4112 * Implementation of the method from LauncherModel.Callbacks.
4113 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004114 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004115 if (mAppsView != null) {
4116 mAppsView.setApps(apps);
4117 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004118 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004119 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Song70a48be2015-03-11 16:36:52 -07004120 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chung785d2eb2011-04-14 16:08:02 -07004121 }
Adam Cohen9211d422014-10-07 18:14:53 -07004122 if (mLauncherCallbacks != null) {
4123 mLauncherCallbacks.bindAllApplications(apps);
4124 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 }
4126
4127 /**
4128 * A package was updated.
4129 *
4130 * Implementation of the method from LauncherModel.Callbacks.
4131 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004132 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004133 Runnable r = new Runnable() {
4134 public void run() {
4135 bindAppsUpdated(apps);
4136 }
4137 };
4138 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004139 return;
4140 }
4141
Winson Chungb745afb2015-03-02 11:51:23 -08004142 if (mAppsView != null) {
4143 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004145 }
4146
Sunny Goyal4390ace2014-10-13 11:33:11 -07004147 @Override
4148 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4149 Runnable r = new Runnable() {
4150 public void run() {
4151 bindWidgetsRestored(widgets);
4152 }
4153 };
4154 if (waitUntilResume(r)) {
4155 return;
4156 }
4157 mWorkspace.widgetsRestored(widgets);
4158 }
4159
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004161 * Some shortcuts were updated in the background.
4162 *
4163 * Implementation of the method from LauncherModel.Callbacks.
4164 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004165 @Override
4166 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4167 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004168 Runnable r = new Runnable() {
4169 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004170 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004171 }
4172 };
4173 if (waitUntilResume(r)) {
4174 return;
4175 }
4176
Sunny Goyal4390ace2014-10-13 11:33:11 -07004177 if (!updated.isEmpty()) {
4178 mWorkspace.updateShortcuts(updated);
4179 }
4180
4181 if (!removed.isEmpty()) {
4182 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4183 for (ShortcutInfo si : removed) {
4184 removedComponents.add(si.getTargetComponent());
4185 }
4186 mWorkspace.removeItemsByComponentName(removedComponents, user);
4187 // Notify the drag controller
4188 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004189 }
4190 }
4191
4192 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004193 * Update the state of a package, typically related to install state.
4194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Sunny Goyale755d462014-07-22 13:48:29 -07004197 @Override
4198 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004199 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004200 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004201 }
4202 }
4203
4204 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004205 * Update the label and icon of all the icons in a package
4206 *
4207 * Implementation of the method from LauncherModel.Callbacks.
4208 */
4209 @Override
4210 public void updatePackageBadge(String packageName) {
4211 if (mWorkspace != null) {
4212 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4213 }
4214 }
4215
4216 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004217 * A package was uninstalled. We take both the super set of packageNames
4218 * in addition to specific applications to remove, the reason being that
4219 * this can be called when a package is updated as well. In that scenario,
4220 * we only remove specific components from the workspace, where as
4221 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004223 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 * Implementation of the method from LauncherModel.Callbacks.
4225 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004226 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004227 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004228 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004229 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004230 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004231 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004232 }
Winson Chungd64d1762013-08-20 14:37:16 -07004233 };
4234 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004235 return;
4236 }
4237
Sunny Goyal1a745e82014-10-02 15:58:31 -07004238 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004239 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4240 for (AppInfo info : appInfos) {
4241 removedComponents.add(info.componentName);
4242 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004243 if (!packageNames.isEmpty()) {
4244 mWorkspace.removeItemsByPackageName(packageNames, user);
4245 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004246 if (!removedComponents.isEmpty()) {
4247 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004248 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004249 // Notify the drag controller
4250 mDragController.onAppsRemoved(packageNames, removedComponents);
4251
Sunny Goyal1a745e82014-10-02 15:58:31 -07004252 } else {
4253 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004254 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004255
Winson Chungdf95eb12013-10-16 14:57:07 -07004256 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004257 if (mAppsView != null) {
4258 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004259 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 }
Joe Onoratobe386092009-11-17 17:32:16 -08004261
4262 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004263 * A number of packages were updated.
4264 */
Adam Cohen091440a2015-03-18 14:16:05 -07004265 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004266 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004267 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004268 bindPackagesUpdated(mWidgetsAndShortcuts);
4269 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004270 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004271 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004272 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4273 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4274 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004275 return;
4276 }
4277
Winson Chung64359a52013-07-08 17:17:08 -07004278 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004279 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004280 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004281 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004282 }
4283
Winson Chung400438b2011-01-16 17:53:48 -08004284 private int mapConfigurationOriActivityInfoOri(int configOri) {
4285 final Display d = getWindowManager().getDefaultDisplay();
4286 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4287 switch (d.getRotation()) {
4288 case Surface.ROTATION_0:
4289 case Surface.ROTATION_180:
4290 // We are currently in the same basic orientation as the natural orientation
4291 naturalOri = configOri;
4292 break;
4293 case Surface.ROTATION_90:
4294 case Surface.ROTATION_270:
4295 // We are currently in the other basic orientation to the natural orientation
4296 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4297 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4298 break;
4299 }
4300
4301 int[] oriMap = {
4302 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4303 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4304 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4305 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4306 };
4307 // Since the map starts at portrait, we need to offset if this device's natural orientation
4308 // is landscape.
4309 int indexOffset = 0;
4310 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4311 indexOffset = 1;
4312 }
4313 return oriMap[(d.getRotation() + indexOffset) % 4];
4314 }
Adam Cohen446e9402011-09-15 18:21:21 -07004315
Winson Chung4b919f82012-05-01 10:44:08 -07004316 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004317 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004318 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4319 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4320 .getConfiguration().orientation));
4321 } else {
4322 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4323 }
Winson Chung4b919f82012-05-01 10:44:08 -07004324 }
4325 }
4326 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004327 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004328 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004329 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004330 } else {
4331 mHandler.postDelayed(new Runnable() {
4332 public void run() {
4333 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4334 }
4335 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004336 }
Winson Chung4b919f82012-05-01 10:44:08 -07004337 }
Winson Chung400438b2011-01-16 17:53:48 -08004338 }
4339
Winson Chunge43a1e72014-01-15 10:33:02 -08004340 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004341 if (mLauncherCallbacks != null) {
4342 return mLauncherCallbacks.isLauncherPreinstalled();
4343 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004344 PackageManager pm = getPackageManager();
4345 try {
4346 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4347 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4348 return true;
4349 } else {
4350 return false;
4351 }
4352 } catch (NameNotFoundException e) {
4353 e.printStackTrace();
4354 return false;
4355 }
4356 }
4357
Adam Cohenea90f832014-05-21 15:03:34 -07004358 /**
4359 * This method indicates whether or not we should suggest default wallpaper dimensions
4360 * when our wallpaper cropper was not yet used to set a wallpaper.
4361 */
4362 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004363 if (mLauncherCallbacks != null) {
4364 return mLauncherCallbacks.overrideWallpaperDimensions();
4365 }
Adam Cohenea90f832014-05-21 15:03:34 -07004366 return true;
4367 }
4368
Adam Cohen432609a2014-03-13 17:03:22 -07004369 /**
4370 * To be overridden by subclasses to indicate that there is an activity to launch
4371 * before showing the standard launcher experience.
4372 */
4373 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004374 if (mLauncherCallbacks != null) {
4375 return mLauncherCallbacks.hasFirstRunActivity();
4376 }
Adam Cohen432609a2014-03-13 17:03:22 -07004377 return false;
4378 }
4379
4380 /**
4381 * To be overridden by subclasses to launch any first run activity
4382 */
4383 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004384 if (mLauncherCallbacks != null) {
4385 return mLauncherCallbacks.getFirstRunActivity();
4386 }
Adam Cohen432609a2014-03-13 17:03:22 -07004387 return null;
4388 }
4389
Winson Chunga6945242014-01-08 14:04:34 -08004390 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004391 return !ActivityManager.isRunningInTestHarness() &&
4392 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004393 }
4394
Adam Cohen4a9423d2014-03-28 14:22:31 -07004395 protected boolean hasRunFirstRunActivity() {
4396 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4397 }
4398
Adam Cohen432609a2014-03-13 17:03:22 -07004399 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004400 if (shouldRunFirstRunActivity() &&
4401 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004402 Intent firstRunIntent = getFirstRunActivity();
4403 if (firstRunIntent != null) {
4404 startActivity(firstRunIntent);
4405 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004406 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004407 }
4408 }
Adam Cohen432609a2014-03-13 17:03:22 -07004409 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004410 }
4411
4412 private void markFirstRunActivityShown() {
4413 SharedPreferences.Editor editor = mSharedPrefs.edit();
4414 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4415 editor.apply();
4416 }
4417
Adam Cohen432609a2014-03-13 17:03:22 -07004418 /**
4419 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4420 * screen that must be displayed and dismissed.
4421 */
4422 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004423 if (mLauncherCallbacks != null) {
4424 return mLauncherCallbacks.hasDismissableIntroScreen();
4425 }
Adam Cohen432609a2014-03-13 17:03:22 -07004426 return false;
4427 }
4428
4429 /**
4430 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4431 */
4432 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004433 if (mLauncherCallbacks != null) {
4434 return mLauncherCallbacks.getIntroScreen();
4435 }
Adam Cohen432609a2014-03-13 17:03:22 -07004436 return null;
4437 }
4438
4439 /**
4440 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4441 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4442 */
4443 private boolean shouldShowIntroScreen() {
4444 return hasDismissableIntroScreen() &&
4445 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4446 }
4447
4448 protected void showIntroScreen() {
4449 View introScreen = getIntroScreen();
4450 changeWallpaperVisiblity(false);
4451 if (introScreen != null) {
4452 mDragLayer.showOverlayView(introScreen);
4453 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004454 if (mLauncherOverlayContainer != null) {
4455 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4456 }
Adam Cohen432609a2014-03-13 17:03:22 -07004457 }
4458
4459 public void dismissIntroScreen() {
4460 markIntroScreenDismissed();
4461 if (showFirstRunActivity()) {
4462 // We delay hiding the intro view until the first run activity is showing. This
4463 // avoids a blip.
4464 mWorkspace.postDelayed(new Runnable() {
4465 @Override
4466 public void run() {
4467 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004468 if (mLauncherOverlayContainer != null) {
4469 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4470 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004471 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004472 }
4473 }, ACTIVITY_START_DELAY);
4474 } else {
4475 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004476 if (mLauncherOverlayContainer != null) {
4477 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4478 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004479 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004480 }
4481 changeWallpaperVisiblity(true);
4482 }
4483
4484 private void markIntroScreenDismissed() {
4485 SharedPreferences.Editor editor = mSharedPrefs.edit();
4486 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4487 editor.apply();
4488 }
4489
Adam Cohen091440a2015-03-18 14:16:05 -07004490 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004491 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4492 // on the device, then we always show the first run cling experience (or if there is no
4493 // launcher2). Otherwise, we prompt the user upon started for migration
4494 LauncherClings launcherClings = new LauncherClings(this);
4495 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4496 if (mModel.canMigrateFromOldLauncherDb(this)) {
4497 launcherClings.showMigrationCling();
4498 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004499 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004500 }
4501 }
4502 }
4503
Winson Chunga6945242014-01-08 14:04:34 -08004504 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004505 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4506 if (mHotseat != null) mHotseat.setAlpha(1f);
4507 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4508 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004509 }
4510
4511 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004512 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4513 if (mHotseat != null) mHotseat.setAlpha(0f);
4514 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4515 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004516 }
4517
Mathew Inwood72fbec12013-11-19 15:45:07 +00004518 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004519 // Called from search suggestion, not supported in other profiles.
4520 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4521 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4522 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4523 myUser);
4524 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004525 return null;
4526 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004527 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004528 }
4529
4530 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4531 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004532 // Called from search suggestion, not supported in other profiles.
4533 return createShortcutDragInfo(shortcutIntent, caption, icon,
4534 UserHandleCompat.myUserHandle());
4535 }
4536
4537 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4538 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004539 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004540 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004541 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004542 }
4543
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004544 protected void moveWorkspaceToDefaultScreen() {
4545 mWorkspace.moveToDefaultScreen(false);
4546 }
4547
Mathew Inwood72fbec12013-11-19 15:45:07 +00004548 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4549 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004550 mWorkspace.onExternalDragStartedWithItem(dragView);
4551 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004552 }
4553
Anjali Koppalf5d29132014-02-28 13:33:27 -08004554 @Override
4555 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004556 if (mLauncherCallbacks != null) {
4557 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4558 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004559 }
4560
Winson Chung80baf5a2010-08-09 16:03:15 -07004561 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004562 * Prints out out state for debugging.
4563 */
4564 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004565 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004566 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004567 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4568 Log.d(TAG, "mRestoring=" + mRestoring);
4569 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4570 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004571 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004572 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004573
Winson Chung785d2eb2011-04-14 16:08:02 -07004574 if (mAppsCustomizeContent != null) {
4575 mAppsCustomizeContent.dumpState();
4576 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004577 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004578 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004579
4580 @Override
4581 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4582 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004583 synchronized (sDumpLogs) {
4584 writer.println(" ");
4585 writer.println("Debug logs: ");
4586 for (int i = 0; i < sDumpLogs.size(); i++) {
4587 writer.println(" " + sDumpLogs.get(i));
4588 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004589 }
Adam Cohen9211d422014-10-07 18:14:53 -07004590 if (mLauncherCallbacks != null) {
4591 mLauncherCallbacks.dump(prefix, fd, writer, args);
4592 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004593 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004594
4595 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004596 if (DEBUG_DUMP_LOG) {
4597 synchronized (sDumpLogs) {
4598 Log.d(TAG, "");
4599 Log.d(TAG, "*********************");
4600 Log.d(TAG, "Launcher debug logs: ");
4601 for (int i = 0; i < sDumpLogs.size(); i++) {
4602 Log.d(TAG, " " + sDumpLogs.get(i));
4603 }
4604 Log.d(TAG, "*********************");
4605 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004606 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004607 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004608 }
4609
4610 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004611 addDumpLog(tag, log, null, debugLog);
4612 }
4613
4614 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004616 if (e != null) {
4617 Log.d(tag, log, e);
4618 } else {
4619 Log.d(tag, log);
4620 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004621 }
Winson Chungede41292013-09-19 16:27:36 -07004622 if (DEBUG_DUMP_LOG) {
4623 sDateStamp.setTime(System.currentTimeMillis());
4624 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004625 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4626 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004627 }
Winson Chungede41292013-09-19 16:27:36 -07004628 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004629 }
4630
Adam Cohen59400422014-03-05 18:07:04 -08004631 public static CustomAppWidget getCustomAppWidget(String name) {
4632 return sCustomAppWidgets.get(name);
4633 }
4634
4635 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4636 return sCustomAppWidgets;
4637 }
4638
Winson Chungede41292013-09-19 16:27:36 -07004639 public void dumpLogsToLocalData() {
4640 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004641 new AsyncTask<Void, Void, Void>() {
4642 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004643 boolean success = false;
4644 sDateStamp.setTime(sRunStart);
4645 String FILENAME = sDateStamp.getMonth() + "-"
4646 + sDateStamp.getDay() + "_"
4647 + sDateStamp.getHours() + "-"
4648 + sDateStamp.getMinutes() + "_"
4649 + sDateStamp.getSeconds() + ".txt";
4650
4651 FileOutputStream fos = null;
4652 File outFile = null;
4653 try {
4654 outFile = new File(getFilesDir(), FILENAME);
4655 outFile.createNewFile();
4656 fos = new FileOutputStream(outFile);
4657 } catch (Exception e) {
4658 e.printStackTrace();
4659 }
4660 if (fos != null) {
4661 PrintWriter writer = new PrintWriter(fos);
4662
4663 writer.println(" ");
4664 writer.println("Debug logs: ");
4665 synchronized (sDumpLogs) {
4666 for (int i = 0; i < sDumpLogs.size(); i++) {
4667 writer.println(" " + sDumpLogs.get(i));
4668 }
4669 }
4670 writer.close();
4671 }
4672 try {
4673 if (fos != null) {
4674 fos.close();
4675 success = true;
4676 }
4677 } catch (IOException e) {
4678 e.printStackTrace();
4679 }
Michael Jurka43467462013-11-14 12:03:58 +01004680 return null;
Winson Chungede41292013-09-19 16:27:36 -07004681 }
Michael Jurka43467462013-11-14 12:03:58 +01004682 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004683 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004685}
Michael Jurka2763be32011-02-24 11:19:57 -08004686
Michael Jurkaabded662011-03-04 12:06:57 -08004687interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004688 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004689 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004690 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004691 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004692 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004693}
Dan Sandlerd5024042014-01-09 15:01:33 -05004694
4695interface DebugIntents {
4696 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4697 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004698}