blob: 4c5bba9e368176575995e144ddc332c11a2c76b1 [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;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080045import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Display;
72import android.view.Gravity;
73import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070083import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070085import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
90import android.widget.FrameLayout;
91import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700100import com.android.launcher3.compat.PackageInstallerCompat;
101import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700105
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.DataInputStream;
107import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800114import java.lang.reflect.InvocationTargetException;
115import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700118import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700121import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700122import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000123import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125/**
126 * Default launcher application.
127 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100128public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700129 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800130 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
131 LauncherStateTransitionAnimation.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800132 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700133 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Daniel Sandlerf061f822013-06-27 13:59:36 -0400135 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400136 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700137 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700139 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700140
Dan Sandlerd5024042014-01-09 15:01:33 -0500141 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700149 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700150
Mathew Inwood876a8462013-06-14 14:12:41 +0100151 /**
152 * IntentStarter uses request codes starting with this. This must be greater than all activity
153 * request codes used internally.
154 */
155 protected static final int REQUEST_LAST = 100;
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
158
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Michael Jurka0a457bf2012-11-19 14:05:05 -0800161 // To turn on these properties, type
162 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung2672ff92012-05-04 16:22:30 -0700165 // The Intent extra that defines whether to ignore the launch animation
166 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400167 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
170 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700171 // Type: int
172 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
175 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: boolean
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
183 // Type: long
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: int[]
194 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Adam Cohen3ed316a2014-07-23 18:21:20 -0700199 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
200 static final String ACTION_FIRST_LOAD_COMPLETE =
201 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
202
Adam Cohen39a06042013-07-19 14:30:12 -0700203 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700204 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700205
Sunny Goyal594d76d2014-11-06 10:12:54 -0800206 private static final String QSB_WIDGET_ID = "qsb_widget_id";
207 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
208
Winson Chunga6945242014-01-08 14:04:34 -0800209 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
210
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800212 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk State mState = State.WORKSPACE;
214 @Thunk AnimatorSet mStateAnimation;
215 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217 private boolean mIsSafeModeEnabled;
218
Adam Cohenc2d6e892014-10-16 09:49:24 -0700219 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
220 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700221 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000228 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
229 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
230
Winson Chunga2413752012-04-03 14:22:34 -0700231 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700232 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
233 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700235
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800236 private final BroadcastReceiver mCloseSystemDialogsReceiver
237 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private LayoutInflater mInflater;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700243 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800244 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800246 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700247 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalffe83f12014-08-14 17:39:34 -0700249 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800253 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800254 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255
Michael Jurka0280c3b2010-09-17 15:00:07 -0700256 private int[] mTmpAddItemCellCoordinates = new int[2];
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private FolderInfo mFolderInfo;
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Adam Cohen091440a2015-03-18 14:16:05 -0700266 @Thunk AppsContainerView mAppsView;
267 @Thunk AppsCustomizeTabHost mAppsCustomizeTabHost;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700268 private AppsCustomizePagedView mAppsCustomizeContent;
269 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800270 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
274 // scroll issues (because the workspace may not have been measured yet) and extra work.
275 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
276 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
278 private SpannableStringBuilder mDefaultKeySsb = null;
279
Adam Cohen091440a2015-03-18 14:16:05 -0700280 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800282 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private boolean mRestoring;
284 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700285 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
Michael Jurka1e2f4652013-07-08 18:03:46 -0700287 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700288 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private Bundle mSavedInstanceState;
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700294 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700296 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
307 private final int mAdvanceInterval = 20000;
308 private final int mAdvanceStagger = 250;
309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 new HashMap<View, AppWidgetProviderInfo>();
313
Winson Chung400438b2011-01-16 17:53:48 -0800314 // Determines how long to wait after a rotation before restoring the screen orientation to
315 // match the sensor state.
316 private final int mRestoreScreenOrientationDelay = 500;
317
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700319
Adam Cohen1462de32012-07-24 22:34:36 -0700320 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700322
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700323 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700324 static Date sDateStamp = new Date();
325 static DateFormat sDateFormat =
326 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
327 static long sRunStart = System.currentTimeMillis();
328 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329
Winson Chung46353de2012-02-16 14:05:10 -0800330 // We only want to get the SharedPreferences once since it does an FS stat each time we get
331 // it from the context.
332 private SharedPreferences mSharedPrefs;
333
Winson Chungf0c6ae02012-03-21 16:10:31 -0700334 // Holds the page that we need to animate to, and the icon views that we need to animate up
335 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700336 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700337 private Bitmap mFolderIconBitmap;
338 private Canvas mFolderIconCanvas;
339 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700340
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 private BubbleTextView mWaitingForResume;
342
Adam Cohen59400422014-03-05 18:07:04 -0800343 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
344 new HashMap<String, CustomAppWidget>();
345
346 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
347 static {
348 if (ENABLE_CUSTOM_WIDGET_TEST) {
349 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
350 }
351 }
352
Chet Haasea8f996d2015-02-17 12:56:04 -0800353 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
354 private static Method sClipRevealMethod = null;
355 static {
356 Class<?> activityOptionsClass = ActivityOptions.class;
357 try {
358 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
359 View.class, int.class, int.class, int.class, int.class);
360 } catch (Exception e) {
361 // Earlier version
362 }
363 }
364
Adam Cohen091440a2015-03-18 14:16:05 -0700365 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700366 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700367 if (mWorkspace != null) {
368 mWorkspace.buildPageHardwareLayers();
369 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700370 }
371 };
372
Adam Cohendb364c32014-05-20 14:23:40 -0700373 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374
Adam Cohen091440a2015-03-18 14:16:05 -0700375 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 int requestCode;
377 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700378 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int cellX;
381 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700382 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 }
384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 private Stats mStats;
386
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700387 FocusIndicatorView mFocusHandler;
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700414 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700417 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700418
419 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700421 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700425 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400426 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800428 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
431
Sunny Goyalffe83f12014-08-14 17:39:34 -0700432 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
435 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700437 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
438 // this also ensures that any synchronous binding below doesn't re-trigger another
439 // LauncherModel load.
440 mPaused = false;
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200443 android.os.Debug.startMethodTracing(
444 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
447 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100451 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800453 registerContentObservers();
454
Joe Onorato7c312c12009-08-13 21:36:53 -0700455 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mSavedState = savedInstanceState;
458 restoreState(mSavedState);
459
460 if (PROFILE_STARTUP) {
461 android.os.Debug.stopMethodTracing();
462 }
463
464 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700465 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 // If the user leaves launcher, then we should just load items asynchronously when
467 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500468 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 } else {
470 // We only load the page synchronously if the user rotates (or triggers a
471 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800472 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
481 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Adam Cohenf9426d52012-06-04 17:26:21 -0700483 // On large interfaces, we want the screen to auto-rotate based on the current orientation
484 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700485
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 if (mLauncherCallbacks.hasLauncherOverlay()) {
489 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700490 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
491 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
492 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 mWorkspace.setLauncherOverlay(mLauncherOverlay);
494 }
Adam Cohen9211d422014-10-07 18:14:53 -0700495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 showFirstRunClings();
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
504
505 private LauncherCallbacks mLauncherCallbacks;
506
507 public void onPostCreate(Bundle savedInstanceState) {
508 super.onPostCreate(savedInstanceState);
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onPostCreate(savedInstanceState);
511 }
512 }
513
514 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
515 mLauncherCallbacks = callbacks;
516 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700517 }
518
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700519 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700520 public void onLauncherProviderChange() {
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onLauncherProviderChange();
523 }
524 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700527 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700528 if (mLauncherCallbacks != null) {
529 return mLauncherCallbacks.hasCustomContentToLeft();
530 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700531 return false;
532 }
533
Dave Hawkeya8881582013-09-17 15:55:33 -0600534 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500535 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 * {@link #addToCustomContentPage}. This will only be invoked if
537 * {@link #hasCustomContentToLeft()} is {@code true}.
538 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500539 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700540 if (mLauncherCallbacks != null) {
541 mLauncherCallbacks.populateCustomContentContainer();
542 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 }
544
545 /**
546 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
547 * ensure the custom content page is added or removed if necessary.
548 */
549 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600550 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 // Not bound yet, wait for bindScreens to be called.
552 return;
553 }
554
555 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
556 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500557 mWorkspace.createCustomContentContainer();
558 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600559 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
560 mWorkspace.removeCustomContentPage();
561 }
562 }
563
Adam Cohen091440a2015-03-18 14:16:05 -0700564 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700565 if (sLocaleConfiguration == null) {
566 new AsyncTask<Void, Void, LocaleConfiguration>() {
567 @Override
568 protected LocaleConfiguration doInBackground(Void... unused) {
569 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
570 readConfiguration(Launcher.this, localeConfiguration);
571 return localeConfiguration;
572 }
573
574 @Override
575 protected void onPostExecute(LocaleConfiguration result) {
576 sLocaleConfiguration = result;
577 checkForLocaleChange(); // recursive, but now with a locale configuration
578 }
579 }.execute();
580 return;
581 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700582
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 final Configuration configuration = getResources().getConfiguration();
584
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 final String locale = configuration.locale.toString();
587
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700588 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final int mcc = configuration.mcc;
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final int mnc = configuration.mnc;
593
Romain Guy84f296c2009-11-04 15:00:44 -0800594 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595
Romain Guy84f296c2009-11-04 15:00:44 -0800596 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 sLocaleConfiguration.locale = locale;
598 sLocaleConfiguration.mcc = mcc;
599 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700600
Joe Onorato0589f0f2010-02-08 13:44:00 -0800601 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602
603 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100604 new AsyncTask<Void, Void, Void>() {
605 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100607 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608 }
Michael Jurka43467462013-11-14 12:03:58 +0100609 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700610 }
611 }
612
Adam Cohen091440a2015-03-18 14:16:05 -0700613 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700614 public String locale;
615 public int mcc = -1;
616 public int mnc = -1;
617 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700618
Adam Cohen091440a2015-03-18 14:16:05 -0700619 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700620 DataInputStream in = null;
621 try {
Helena Josol28db2802014-10-09 17:04:09 +0100622 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700623 configuration.locale = in.readUTF();
624 configuration.mcc = in.readInt();
625 configuration.mnc = in.readInt();
626 } catch (FileNotFoundException e) {
627 // Ignore
628 } catch (IOException e) {
629 // Ignore
630 } finally {
631 if (in != null) {
632 try {
633 in.close();
634 } catch (IOException e) {
635 // Ignore
636 }
637 }
638 }
639 }
640
Adam Cohen091440a2015-03-18 14:16:05 -0700641 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700642 DataOutputStream out = null;
643 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100644 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100645 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700646 out.writeUTF(configuration.locale);
647 out.writeInt(configuration.mcc);
648 out.writeInt(configuration.mnc);
649 out.flush();
650 } catch (FileNotFoundException e) {
651 // Ignore
652 } catch (IOException e) {
653 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100654 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700655 } finally {
656 if (out != null) {
657 try {
658 out.close();
659 } catch (IOException e) {
660 // Ignore
661 }
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664 }
665
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666 public Stats getStats() {
667 return mStats;
668 }
669
Bjorn Bringertc459e522013-06-07 19:36:01 +0100670 public LayoutInflater getInflater() {
671 return mInflater;
672 }
673
Winson Chung36a62fe2012-05-06 18:04:42 -0700674 boolean isDraggingEnabled() {
675 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
676 // that is subsequently removed from the workspace in startBinding().
677 return !mModel.isLoadingWorkspace();
678 }
679
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800680 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000681 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100682 if (Build.VERSION.SDK_INT >= 17) {
683 return View.generateViewId();
684 } else {
685 // View.generateViewId() is not available. The following fallback logic is a copy
686 // of its implementation.
687 for (;;) {
688 final int result = sNextGeneratedId.get();
689 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
690 int newValue = result + 1;
691 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
692 if (sNextGeneratedId.compareAndSet(result, newValue)) {
693 return result;
694 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000695 }
696 }
697 }
698
699 public int getViewIdForItem(ItemInfo info) {
700 // This cast is safe given the > 2B range for int.
701 int itemId = (int) info.id;
702 if (mItemIdToViewId.containsKey(itemId)) {
703 return mItemIdToViewId.get(itemId);
704 }
705 int viewId = generateViewId();
706 mItemIdToViewId.put(itemId, viewId);
707 return viewId;
708 }
709
710 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700711 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
712 * a configuration step, this allows the proper animations to run after other transitions.
713 */
Adam Cohendb364c32014-05-20 14:23:40 -0700714 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700715 long screenId = args.screenId;
716 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
717 // When the screen id represents an actual screen (as opposed to a rank) we make sure
718 // that the drop page actually exists.
719 screenId = ensurePendingDropLayoutExists(args.screenId);
720 }
Adam Cohendb364c32014-05-20 14:23:40 -0700721
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800723 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700724 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700726 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800727 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700728 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700729 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700730 case REQUEST_RECONFIGURE_APPWIDGET:
731 completeRestoreAppWidget(args.appWidgetId);
732 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800733 }
Michael Jurka27614d22012-04-02 06:40:22 -0700734 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
735 // if you turned the screen off and then back while in All Apps, Launcher would not
736 // return to the workspace. Clearing mAddInfo.container here fixes this issue
737 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700738 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 }
740
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800741 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700743 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700744 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700747
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 Runnable exitSpringLoaded = new Runnable() {
749 @Override
750 public void run() {
751 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
752 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
753 }
754 };
755
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
759 if (resultCode == RESULT_CANCELED) {
760 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700761 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800764 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 }
767 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200768 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
769 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
770 mWorkspace.exitOverviewMode(false);
771 }
772 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200774
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
776 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700777
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 // We have special handling for widgets
780 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800781 final int appWidgetId;
782 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
783 : -1;
784 if (widgetId < 0) {
785 appWidgetId = pendingAddWidgetId;
786 } else {
787 appWidgetId = widgetId;
788 }
789
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800791 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
793 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 result = RESULT_CANCELED;
795 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 @Override
798 public void run() {
799 exitSpringLoadedDragModeDelayed(false, 0, null);
800 }
801 };
Adam Cohendb364c32014-05-20 14:23:40 -0700802 if (workspaceLocked) {
803 // No need to remove the empty screen if we're mid-binding, as the
804 // the bind will not add the empty screen.
805 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
806 } else {
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
809 }
Winson Chung5aaab772012-04-27 15:24:02 -0700810 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700811 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700812 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
813 // When the screen id represents an actual screen (as opposed to a rank)
814 // we make sure that the drop page actually exists.
815 mPendingAddInfo.screenId =
816 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
817 }
Adam Cohendb364c32014-05-20 14:23:40 -0700818 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
819
820 dropLayout.setDropPending(true);
821 final Runnable onComplete = new Runnable() {
822 @Override
823 public void run() {
824 completeTwoStageWidgetDrop(resultCode, appWidgetId);
825 dropLayout.setDropPending(false);
826 }
827 };
828 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
830 } else {
831 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
832 mPendingAddInfo);
833 sPendingAddItem = args;
834 }
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 return;
837 }
838
Sunny Goyalff572272014-07-23 13:58:07 -0700839 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
840 if (resultCode == RESULT_OK) {
841 // Update the widget view.
842 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
843 pendingAddWidgetId, mPendingAddInfo);
844 if (workspaceLocked) {
845 sPendingAddItem = args;
846 } else {
847 completeAdd(args);
848 }
849 }
850 // Leave the widget in the pending state if the user canceled the configure.
851 return;
852 }
853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 // The pattern used here is that a user PICKs a specific application,
855 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
858 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700859 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700860 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
861 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800862 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700863 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800864 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700866 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohen00074722013-10-11 17:46:09 -0700869 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700870 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800874
875 }
876
877 @Override
878 protected void onActivityResult(
879 final int requestCode, final int resultCode, final Intent data) {
880 handleActivityResult(requestCode, resultCode, data);
881 if (mLauncherCallbacks != null) {
882 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
883 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 }
885
Adam Cohendb364c32014-05-20 14:23:40 -0700886 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
887 appWidgetId, ItemInfo info) {
888 PendingAddArguments args = new PendingAddArguments();
889 args.requestCode = requestCode;
890 args.intent = data;
891 args.container = info.container;
892 args.screenId = info.screenId;
893 args.cellX = info.cellX;
894 args.cellY = info.cellY;
895 args.appWidgetId = appWidgetId;
896 return args;
897 }
898
899 /**
900 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
901 *
902 * @param screenId the screen id to check
903 * @return the new screen, or screenId if it exists
904 */
905 private long ensurePendingDropLayoutExists(long screenId) {
906 CellLayout dropLayout =
907 (CellLayout) mWorkspace.getScreenWithId(screenId);
908 if (dropLayout == null) {
909 // it's possible that the add screen was removed because it was
910 // empty and a re-bind occurred
911 mWorkspace.addExtraEmptyScreen();
912 return mWorkspace.commitExtraEmptyScreen();
913 } else {
914 return screenId;
915 }
916 }
917
Adam Cohen091440a2015-03-18 14:16:05 -0700918 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700919 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 Runnable onCompleteRunnable = null;
922 int animationType = 0;
923
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 if (resultCode == RESULT_OK) {
926 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
927 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 mPendingAddWidgetInfo);
929 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 onCompleteRunnable = new Runnable() {
931 @Override
932 public void run() {
933 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
936 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 }
938 };
939 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 if (mDragLayer.getAnimatedView() != null) {
944 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
945 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
946 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700947 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 // The animated view may be null in the case of a rotation during widget configuration
949 onCompleteRunnable.run();
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 protected void onStop() {
955 super.onStop();
956 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700957
958 if (mLauncherCallbacks != null) {
959 mLauncherCallbacks.onStop();
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
964 protected void onStart() {
965 super.onStart();
966 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStart();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200975 long startTime = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400978 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.preOnResume();
983 }
984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700986
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 // Restore the previous launcher state
Winson Chungb745afb2015-03-02 11:51:23 -0800988 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800990 } else if (mOnResumeState == State.APPS) {
991 showAppsView(false /* animated */, false /* resetListToTop */);
992 } else if (mOnResumeState == State.WIDGETS) {
993 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 }
995 mOnResumeState = State.NONE;
996
Craig Mautner360310b2012-10-26 15:13:08 -0700997 // Background was set to gradient in onPause(), restore to black if in all apps.
998 setWorkspaceBackground(mState == State.WORKSPACE);
999
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001000 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001002 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001003 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001005 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001007 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001008 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1009 // execute them here
1010 long startTimeCallbacks = 0;
1011 if (DEBUG_RESUME_TIME) {
1012 startTimeCallbacks = System.currentTimeMillis();
1013 }
1014
1015 if (mAppsCustomizeContent != null) {
1016 mAppsCustomizeContent.setBulkBind(true);
1017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1019 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001020 }
1021 if (mAppsCustomizeContent != null) {
1022 mAppsCustomizeContent.setBulkBind(false);
1023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 if (DEBUG_RESUME_TIME) {
1026 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1027 (System.currentTimeMillis() - startTimeCallbacks));
1028 }
Michael Jurka447bf842013-05-15 14:52:15 +02001029 }
Michael Jurka54554252013-08-01 12:52:23 +02001030 if (mOnResumeCallbacks.size() > 0) {
1031 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1032 mOnResumeCallbacks.get(i).run();
1033 }
1034 mOnResumeCallbacks.clear();
1035 }
Winson Chunge4e50662012-01-23 14:45:13 -08001036
1037 // Reset the pressed state of icons that were locked in the press state while activities
1038 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001039 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001040 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001041 mWaitingForResume.setStayPressed(false);
1042 }
Winson Chung82963d52013-10-09 11:20:57 -07001043
Adam Cohen06dff352012-06-01 17:17:08 -07001044 // It is possible that widgets can receive updates while launcher is not in the foreground.
1045 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1046 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1047 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001048 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001049
Winson Chung780fe592013-09-26 14:48:44 -07001050 // Process any items that were added while Launcher was away.
1051 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1052
Michael Jurka447bf842013-05-15 14:52:15 +02001053 if (DEBUG_RESUME_TIME) {
1054 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1055 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001056
1057 if (mWorkspace.getCustomContentCallbacks() != null) {
1058 // If we are resuming and the custom content is the current page, we call onShow().
1059 // It is also poassible that onShow will instead be called slightly after first layout
1060 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1061 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001062 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001063 }
1064 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001065 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001066 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001067
1068 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001069
1070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onResume();
1072 }
Adam Cohen06dff352012-06-01 17:17:08 -07001073 }
1074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001077 // Ensure that items added to Launcher are queued until Launcher returns
1078 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001079 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001080
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001082 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001083 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001084 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001085
1086 // We call onHide() aggressively. The custom content callbacks should be able to
1087 // debounce excess onHide calls.
1088 if (mWorkspace.getCustomContentCallbacks() != null) {
1089 mWorkspace.getCustomContentCallbacks().onHide();
1090 }
Romain Guycbb89e42009-06-08 15:52:54 -07001091
Adam Cohen9211d422014-10-07 18:14:53 -07001092 if (mLauncherCallbacks != null) {
1093 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
1096
1097 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001098 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1099 // by a onResume or by scrolling otherwise.
1100 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001101
1102 // Custom content is completely hidden
1103 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001104
1105 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1106 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001107
1108 // Indicates whether the user is allowed to scroll away from the custom content.
1109 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001110 }
1111
Adam Cohenc2d6e892014-10-16 09:49:24 -07001112 public interface LauncherOverlay {
1113
1114 /**
1115 * Touch interaction leading to overscroll has begun
1116 */
1117 public void onScrollInteractionBegin();
1118
1119 /**
1120 * Touch interaction related to overscroll has ended
1121 */
1122 public void onScrollInteractionEnd();
1123
1124 /**
1125 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1126 * screen (or in the case of RTL, the rightmost screen).
1127 */
1128 public void onScrollChange(int progress, boolean rtl);
1129
1130 /**
1131 * Screen has stopped scrolling
1132 */
1133 public void onScrollSettled();
1134
1135 /**
1136 * This method can be called by the Launcher in order to force the LauncherOverlay
1137 * to exit fully immersive mode.
1138 */
1139 public void forceExitFullImmersion();
1140 }
1141
1142 public interface LauncherOverlayCallbacks {
1143 /**
1144 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1145 * however it doesn't modify any state within the launcher.
1146 */
1147 public boolean canEnterFullImmersion();
1148
1149 /**
1150 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1151 * eg. by occupying the full screen and handling all touch events.
1152 *
1153 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1154 * case, Launcher will modify any necessary state and assumes the overlay is
1155 * handling all interaction. If false, the LauncherOverlay should cancel any
1156 *
1157 */
1158 public boolean enterFullImmersion();
1159
1160 /**
1161 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1162 * full control over UI and state.
1163 */
1164 public void exitFullImmersion();
1165 }
1166
1167 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1168
1169 @Override
1170 public boolean canEnterFullImmersion() {
1171 return mState == State.WORKSPACE;
1172 }
1173
1174 @Override
1175 public boolean enterFullImmersion() {
1176 if (mState == State.WORKSPACE) {
1177 // When fully immersed, disregard any touches which fall through.
1178 mDragLayer.setBlockTouch(true);
1179 return true;
1180 }
1181 return false;
1182 }
1183
1184 @Override
1185 public void exitFullImmersion() {
1186 mDragLayer.setBlockTouch(false);
1187 }
1188 }
1189
Jorim Jaggid017f882014-01-14 17:08:48 -08001190 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001191 if (mLauncherCallbacks != null) {
1192 return mLauncherCallbacks.hasSettings();
1193 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001194 return false;
1195 }
1196
Adam Cohenbffe7452013-07-22 18:21:45 -07001197
Adam Cohen9211d422014-10-07 18:14:53 -07001198 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001199 CustomContentCallbacks callbacks, String description) {
1200 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001201 }
1202
Adam Cohenedb40762013-07-18 16:45:45 -07001203 // The custom content needs to offset its content to account for the QSB
1204 public int getTopOffsetForCustomContent() {
1205 return mWorkspace.getPaddingTop();
1206 }
1207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 @Override
1209 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001211 if (mModel.isCurrentCallbacks(this)) {
1212 mModel.stopLoader();
1213 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 if (mAppsCustomizeContent != null) {
1215 mAppsCustomizeContent.surrender();
1216 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001217 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001218 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 @Override
1222 public void onWindowFocusChanged(boolean hasFocus) {
1223 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001224 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001225
1226 if (mLauncherCallbacks != null) {
1227 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 private boolean acceptFilter() {
1232 final InputMethodManager inputManager = (InputMethodManager)
1233 getSystemService(Context.INPUT_METHOD_SERVICE);
1234 return !inputManager.isFullscreenMode();
1235 }
1236
1237 @Override
1238 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001239 final int uniChar = event.getUnicodeChar();
1240 final boolean handled = super.onKeyDown(keyCode, event);
1241 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1242 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1244 keyCode, event);
1245 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001246 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001247 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 // showSearchDialog()
1249 // If there are multiple keystrokes before the search dialog takes focus,
1250 // onSearchRequested() will be called for every keystroke,
1251 // but it is idempotent, so it's fine.
1252 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254 }
1255
Joe Onorato8a9625e2010-01-28 15:55:35 -08001256 // Eat the long press event so the keyboard doesn't come up.
1257 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1258 return true;
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 return handled;
1262 }
1263
Karl Rosaen138a0412009-04-23 19:00:21 -07001264 private String getTypedText() {
1265 return mDefaultKeySsb.toString();
1266 }
1267
1268 private void clearTypedText() {
1269 mDefaultKeySsb.clear();
1270 mDefaultKeySsb.clearSpans();
1271 Selection.setSelection(mDefaultKeySsb, 0);
1272 }
1273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001275 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1276 * State
1277 */
1278 private static State intToState(int stateOrdinal) {
1279 State state = State.WORKSPACE;
1280 final State[] stateValues = State.values();
1281 for (int i = 0; i < stateValues.length; i++) {
1282 if (stateValues[i].ordinal() == stateOrdinal) {
1283 state = stateValues[i];
1284 break;
1285 }
1286 }
1287 return state;
1288 }
1289
1290 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 * Restores the previous state, if it exists.
1292 *
1293 * @param savedState The previous state.
1294 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001295 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 private void restoreState(Bundle savedState) {
1297 if (savedState == null) {
1298 return;
1299 }
1300
Michael Jurka883f55b2010-10-21 15:47:14 -07001301 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001302 if (state == State.APPS || state == State.WIDGETS) {
1303 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001304 }
1305
Adam Cohen21cd0022013-10-09 18:57:02 -07001306 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1307 PagedView.INVALID_RESTORE_PAGE);
1308 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001309 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001313 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1316 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001317 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001318 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1319 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001320 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1321 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1322 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001323 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001324 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 mRestoring = true;
1326 }
1327
1328 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1329 if (renameFolder) {
1330 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001331 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 mRestoring = true;
1333 }
Winson Chunga12a2502010-12-20 14:41:35 -08001334
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 // Restore the AppsCustomize tab
1336 if (mAppsCustomizeTabHost != null) {
1337 String curTab = savedState.getString("apps_customize_currentTab");
1338 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001339 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001341 mAppsCustomizeContent.loadAssociatedPages(
1342 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 }
1344
Winson Chung5afbf7b2011-07-25 11:53:08 -07001345 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1346 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001348 mItemIdToViewId = (HashMap<Integer, Integer>)
1349 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
1352 /**
1353 * Finds all the views we need and configure them properly.
1354 */
1355 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001356 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001357
Craig Mautner360310b2012-10-26 15:13:08 -07001358 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001359 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001360 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1361 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001362 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001363 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001364
John Spurlock77e1f472013-09-11 10:09:51 -04001365 mLauncherView.setSystemUiVisibility(
1366 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001367 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Winson Chung4c98d922011-05-31 16:50:48 -07001369 // Setup the drag layer
1370 mDragLayer.setup(this, dragController);
1371
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 // Setup the hotseat
1373 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1374 if (mHotseat != null) {
1375 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001376 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 }
1378
Jorim Jaggid017f882014-01-14 17:08:48 -08001379 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001380 View widgetButton = findViewById(R.id.widget_button);
1381 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 @Override
1383 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001384 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001385 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001386 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001387 }
1388 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001389 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1390
1391 View wallpaperButton = findViewById(R.id.wallpaper_button);
1392 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 @Override
1394 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001395 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001396 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001397 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 }
1399 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001400 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1401
1402 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001403 if (hasSettings()) {
1404 settingsButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View arg0) {
1407 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001408 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001410 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001411 });
1412 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1413 } else {
1414 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001415 }
1416
Adam Cohendbdff6b2013-09-18 19:09:15 -07001417 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418
Winson Chung4c98d922011-05-31 16:50:48 -07001419 // Setup the workspace
1420 mWorkspace.setHapticFeedbackEnabled(false);
1421 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001422 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001423 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001424
Winson Chungf0ea4d32011-06-06 14:27:16 -07001425 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001426 mSearchDropTargetBar = (SearchDropTargetBar)
1427 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001428
Winson Chungb745afb2015-03-02 11:51:23 -08001429 // Setup Apps
1430 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1431
Winson Chungf0ea4d32011-06-06 14:27:16 -07001432 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001433 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 mAppsCustomizeContent = (AppsCustomizePagedView)
1435 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1436 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001437
Winson Chung3d503fb2011-07-13 17:25:49 -07001438 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001439 dragController.setDragScoller(mWorkspace);
1440 dragController.setScrollView(mDragLayer);
1441 dragController.setMoveTarget(mWorkspace);
1442 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001443 if (mSearchDropTargetBar != null) {
1444 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001445 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001446 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001447
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001448 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001449 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001450 mWeightWatcher = new WeightWatcher(this);
1451 mWeightWatcher.setAlpha(0.5f);
1452 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 new FrameLayout.LayoutParams(
1454 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001455 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001456 Gravity.BOTTOM)
1457 );
Adam Cohen39a06042013-07-19 14:30:12 -07001458
1459 boolean show = shouldShowWeightWatcher();
1460 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 * Sets the all apps button. This method is called from {@link Hotseat}.
1466 */
1467 public void setAllAppsButton(View allAppsButton) {
1468 mAllAppsButton = allAppsButton;
1469 }
1470
1471 public View getAllAppsButton() {
1472 return mAllAppsButton;
1473 }
1474
1475 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * Creates a view representing a shortcut.
1477 *
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from R.layout.application.
1481 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001482 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001484 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 /**
1488 * Creates a view representing a shortcut inflated from the specified resource.
1489 *
1490 * @param layoutResId The id of the XML layout used to create the shortcut.
1491 * @param parent The group the shortcut belongs to.
1492 * @param info The data structure describing the shortcut.
1493 *
1494 * @return A View inflated from layoutResId.
1495 */
Adam Cohen59400422014-03-05 18:07:04 -08001496 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001497 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001498 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001500 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return favorite;
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Add a shortcut to the workspace.
1506 *
1507 * @param data The intent describing the shortcut.
1508 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 int cellY) {
1512 int[] cellXY = mTmpAddItemCellCoordinates;
1513 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001515
Michael Jurkad3ef3062010-11-23 16:23:58 -08001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517
Sunny Goyal2350bc92014-10-14 16:42:54 -07001518 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001519 if (info == null) {
1520 return;
1521 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 final View view = createShortcut(info);
1523
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001541 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 foundCellSpan = (result != null);
1545 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 }
1548
1549 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001550 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 return;
1552 }
1553
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555
1556 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560 }
1561
Adam Cohen2f093b62012-04-30 18:59:53 -07001562 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1563 int minHeight) {
1564 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001565 // We want to account for the extra amount of padding that we are adding to the widget
1566 // to ensure that it gets the full amount of space that it has requested
1567 int requiredWidth = minWidth + padding.left + padding.right;
1568 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001569 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1573 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1577 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001578 }
1579
Adam Cohen2f093b62012-04-30 18:59:53 -07001580 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1581 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001582 }
1583
Adam Cohen2f093b62012-04-30 18:59:53 -07001584 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1585 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001586 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001587 }
1588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001590 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001592 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 */
Adam Cohen091440a2015-03-18 14:16:05 -07001594 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001595 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1596
1597 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001598 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001599 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1600 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 }
Romain Guycbb89e42009-06-08 15:52:54 -07001602
Adam Cohen59400422014-03-05 18:07:04 -08001603 if (appWidgetInfo.isCustomWidget) {
1604 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001605 }
1606
Adam Cohen59400422014-03-05 18:07:04 -08001607 LauncherAppWidgetInfo launcherInfo;
1608 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1609 launcherInfo.spanX = info.spanX;
1610 launcherInfo.spanY = info.spanY;
1611 launcherInfo.minSpanX = info.minSpanX;
1612 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001613 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001616 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
1618 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001619 if (hostView == null) {
1620 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001621 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1622 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 } else {
1624 // The AppWidgetHostView has already been inflated and instantiated
1625 launcherInfo.hostView = hostView;
1626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001628 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001629 launcherInfo.notifyWidgetSizeChanged(this);
1630
Adam Cohen59400422014-03-05 18:07:04 -08001631 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1632 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001633
1634 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001636 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Adam Cohended9f8d2010-11-03 13:25:16 -07001639 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1640 @Override
1641 public void onReceive(Context context, Intent intent) {
1642 final String action = intent.getAction();
1643 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1644 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001645 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001646 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001647
Winson Chungc100e8e2011-08-09 16:02:43 -07001648 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001649 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001650 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1651 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001652 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001653 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1655 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001657 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1658 mModel.resetLoadedState(false, true);
1659 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1660 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1661 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1662 mModel.resetLoadedState(false, true);
1663 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1664 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1665 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001666 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1667 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1668 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 }
1670 }
1671 };
1672
1673 @Override
1674 public void onAttachedToWindow() {
1675 super.onAttachedToWindow();
1676
1677 // Listen for broadcasts related to user-presence
1678 final IntentFilter filter = new IntentFilter();
1679 filter.addAction(Intent.ACTION_SCREEN_OFF);
1680 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001681 // For handling managed profiles
1682 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1683 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001684 if (ENABLE_DEBUG_INTENTS) {
1685 filter.addAction(DebugIntents.DELETE_DATABASE);
1686 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001690 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001691 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 mVisible = true;
1693 }
1694
Adam Cohen0b395352014-06-09 22:54:36 +00001695 /**
1696 * Sets up transparent navigation and status bars in LMP.
1697 * This method is a no-op for other platform versions.
1698 */
Sunny Goyald0091012015-01-20 15:55:34 -08001699 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001700 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001701 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001702 Window window = getWindow();
1703 window.getAttributes().systemUiVisibility |=
1704 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1705 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1706 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1707 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1708 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1709 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1710 window.setStatusBarColor(Color.TRANSPARENT);
1711 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001712 }
1713 }
1714
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 @Override
1716 public void onDetachedFromWindow() {
1717 super.onDetachedFromWindow();
1718 mVisible = false;
1719
Adam Cohend113e0c2010-11-11 10:48:05 -08001720 if (mAttached) {
1721 unregisterReceiver(mReceiver);
1722 mAttached = false;
1723 }
Winson Chungb745afb2015-03-02 11:51:23 -08001724 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 }
1726
1727 public void onWindowVisibilityChanged(int visibility) {
1728 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001729 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001730 // The following code used to be in onResume, but it turns out onResume is called when
1731 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1732 // is a more appropriate event to handle
1733 if (mVisible) {
1734 mAppsCustomizeTabHost.onWindowVisible();
1735 if (!mWorkspaceLoading) {
1736 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001737 // We want to let Launcher draw itself at least once before we force it to build
1738 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001739 // apps is nice and speedy.
1740 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001741 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001742 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 if (mStarted) return;
1744 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001745 // We delay the layer building a bit in order to give
1746 // other message processing a time to run. In particular
1747 // this avoids a delay in hiding the IME if it was
1748 // currently shown, because doing that may involve
1749 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001750 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001751 final ViewTreeObserver.OnDrawListener listener = this;
1752 mWorkspace.post(new Runnable() {
1753 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001754 if (mWorkspace != null &&
1755 mWorkspace.getViewTreeObserver() != null) {
1756 mWorkspace.getViewTreeObserver().
1757 removeOnDrawListener(listener);
1758 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001759 }
1760 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001761 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 }
1763 });
1764 }
1765 clearTypedText();
1766 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768
Adam Cohen091440a2015-03-18 14:16:05 -07001769 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 mHandler.removeMessages(ADVANCE_MSG);
1771 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1772 mHandler.sendMessageDelayed(msg, delay);
1773 mAutoAdvanceSentTime = System.currentTimeMillis();
1774 }
1775
Adam Cohen091440a2015-03-18 14:16:05 -07001776 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1778 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1779 mAutoAdvanceRunning = autoAdvanceRunning;
1780 if (autoAdvanceRunning) {
1781 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1782 sendAdvanceMessage(delay);
1783 } else {
1784 if (!mWidgetsToAdvance.isEmpty()) {
1785 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1786 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1787 }
1788 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001789 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 }
1791 }
1792 }
1793
1794 private final Handler mHandler = new Handler() {
1795 @Override
1796 public void handleMessage(Message msg) {
1797 if (msg.what == ADVANCE_MSG) {
1798 int i = 0;
1799 for (View key: mWidgetsToAdvance.keySet()) {
1800 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1801 final int delay = mAdvanceStagger * i;
1802 if (v instanceof Advanceable) {
1803 postDelayed(new Runnable() {
1804 public void run() {
1805 ((Advanceable) v).advance();
1806 }
1807 }, delay);
1808 }
1809 i++;
1810 }
1811 sendAdvanceMessage(mAdvanceInterval);
1812 }
1813 }
1814 };
1815
1816 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001817 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001818 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1819 if (v instanceof Advanceable) {
1820 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001821 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001822 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 }
1824 }
1825
1826 void removeWidgetToAutoAdvance(View hostView) {
1827 if (mWidgetsToAdvance.containsKey(hostView)) {
1828 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001829 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001830 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 }
1832
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001834 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 launcherInfo.hostView = null;
1836 }
1837
Winson Chung93eef082012-03-23 15:59:27 -07001838 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1839 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1840 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001841 }
1842
Winson Chunga6945242014-01-08 14:04:34 -08001843 public DragLayer getDragLayer() {
1844 return mDragLayer;
1845 }
1846
Winson Chungb745afb2015-03-02 11:51:23 -08001847 public AppsContainerView getAppsView() {
1848 return mAppsView;
1849 }
1850
1851 public AppsCustomizeTabHost getWidgetsView() {
1852 return mAppsCustomizeTabHost;
1853 }
1854
Winson Chunga6945242014-01-08 14:04:34 -08001855 public Workspace getWorkspace() {
1856 return mWorkspace;
1857 }
1858
1859 public Hotseat getHotseat() {
1860 return mHotseat;
1861 }
1862
Jorim Jaggid017f882014-01-14 17:08:48 -08001863 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001864 return mOverviewPanel;
1865 }
1866
1867 public SearchDropTargetBar getSearchBar() {
1868 return mSearchDropTargetBar;
1869 }
1870
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001871 public LauncherAppWidgetHost getAppWidgetHost() {
1872 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 }
Romain Guycbb89e42009-06-08 15:52:54 -07001874
Winson Chunga9abd0e2010-10-27 17:18:37 -07001875 public LauncherModel getModel() {
1876 return mModel;
1877 }
1878
Winson Chunga6945242014-01-08 14:04:34 -08001879 protected SharedPreferences getSharedPrefs() {
1880 return mSharedPrefs;
1881 }
1882
Bjorn Bringertc459e522013-06-07 19:36:01 +01001883 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001884 getWindow().closeAllPanels();
1885
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001886 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001887 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001888 }
1889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 @Override
1891 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001892 long startTime = 0;
1893 if (DEBUG_RESUME_TIME) {
1894 startTime = System.currentTimeMillis();
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 super.onNewIntent(intent);
1897
1898 // Close the menu
1899 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001900 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001902
Adam Cohened307df2013-10-02 09:37:31 -07001903 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1904 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1905 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001906
Adam Cohen6fecd412013-10-02 17:41:50 -07001907 if (mWorkspace == null) {
1908 // Can be cases where mWorkspace is null, this prevents a NPE
1909 return;
1910 }
1911 Folder openFolder = mWorkspace.getOpenFolder();
1912 // In all these cases, only animate if we're already on home
1913 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001914
1915 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1916 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001918 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001920 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001921
Adam Cohen6fecd412013-10-02 17:41:50 -07001922 closeFolder();
1923 exitSpringLoadedDragMode();
1924
1925 // If we are already on home, then just animate back to the workspace,
1926 // otherwise, just wait until onResume to set the state back to Workspace
1927 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001928 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 } else {
1930 mOnResumeState = State.WORKSPACE;
1931 }
1932
1933 final View v = getWindow().peekDecorView();
1934 if (v != null && v.getWindowToken() != null) {
1935 InputMethodManager imm = (InputMethodManager)getSystemService(
1936 INPUT_METHOD_SERVICE);
1937 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1938 }
1939
Winson Chungb745afb2015-03-02 11:51:23 -08001940 // Reset the apps view
1941 if (!alreadyOnHome && mAppsView != null) {
1942 mAppsView.scrollToTop();
1943 }
1944
Adam Cohen6fecd412013-10-02 17:41:50 -07001945 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001946 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 mAppsCustomizeTabHost.reset();
1948 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001949
Adam Cohen9211d422014-10-07 18:14:53 -07001950 if (mLauncherCallbacks != null) {
1951 mLauncherCallbacks.onHomeIntent();
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
Adam Cohened307df2013-10-02 09:37:31 -07001954
Michael Jurka447bf842013-05-15 14:52:15 +02001955 if (DEBUG_RESUME_TIME) {
1956 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onNewIntent(intent);
1961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962 }
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001965 public void onRestoreInstanceState(Bundle state) {
1966 super.onRestoreInstanceState(state);
1967 for (int page: mSynchronouslyBoundPages) {
1968 mWorkspace.restoreInstanceStateForChild(page);
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
1972 @Override
1973 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001974 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001975 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1976 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001977 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001978 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
Michael Jurka883f55b2010-10-21 15:47:14 -07001980 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001981 // We close any open folder since it will not be re-opened, and we need to make sure
1982 // this state is reflected.
1983 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
Adam Cohendcd297f2013-06-18 13:13:40 -07001985 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 mWaitingForResult) {
1987 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001989 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1990 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1993 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 if (mFolderInfo != null && mWaitingForResult) {
1998 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1999 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2000 }
Michael Jurka946ad472010-07-09 18:05:18 -07002001
Winson Chung785d2eb2011-04-14 16:08:02 -07002002 // Save the current AppsCustomize tab
2003 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002004 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2005 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002006 if (currentTabTag != null) {
2007 outState.putString("apps_customize_currentTab", currentTabTag);
2008 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002009 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2010 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002011 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002012 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onSaveInstanceState(outState);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
2019 @Override
2020 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Winson Chunge7a03942011-08-05 15:05:12 -07002023 // Remove all pending runnables
2024 mHandler.removeMessages(ADVANCE_MSG);
2025 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002026 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002027
Winson Chungcd2b0142011-06-08 16:02:26 -07002028 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002029 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002030
2031 // It's possible to receive onDestroy after a new Launcher activity has
2032 // been created. In this case, don't interfere with the new Launcher.
2033 if (mModel.isCurrentCallbacks(this)) {
2034 mModel.stopLoader();
2035 app.setLauncher(null);
2036 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002039 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002041 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002043 mAppWidgetHost = null;
2044
2045 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046
2047 TextKeyListener.getInstance().release();
2048
Winson Chung81b52252012-08-27 15:34:29 -07002049 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2050 // to prevent leaking Launcher activities on orientation change.
2051 if (mModel != null) {
2052 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2053 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002054
2055 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002056 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002057
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002058 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002060 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 mWorkspace = null;
2062 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002063
Michael Jurka2ecf9952012-06-18 12:52:28 -07002064 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002065
2066 if (mLauncherCallbacks != null) {
2067 mLauncherCallbacks.onDestroy();
2068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070
Adam Cohena9cf38f2011-05-02 15:36:58 -07002071 public DragController getDragController() {
2072 return mDragController;
2073 }
2074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 @Override
2076 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002077 if (requestCode >= 0) {
2078 setWaitingForResult(true);
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 super.startActivityForResult(intent, requestCode);
2081 }
2082
Winson Chung70d72102011-08-12 11:24:35 -07002083 /**
2084 * Indicates that we want global search for this activity by setting the globalSearch
2085 * argument for {@link #startSearch} to true.
2086 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002088 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002090
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002091 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Karl Rosaen138a0412009-04-23 19:00:21 -07002093 if (initialQuery == null) {
2094 // Use any text typed in the launcher as the initial query
2095 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 if (appSearchData == null) {
2098 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002099 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
Winson Chungadf0c182012-08-23 14:59:07 -07002101 Rect sourceBounds = new Rect();
2102 if (mSearchDropTargetBar != null) {
2103 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2104 }
Romain Guycbb89e42009-06-08 15:52:54 -07002105
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002107 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002108 if (clearTextImmediately) {
2109 clearTypedText();
2110 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002111 }
2112
Ian Parkinson047036e2014-09-01 15:40:53 +01002113 /**
2114 * Start a text search.
2115 *
2116 * @return {@code true} if the search will start immediately, so any further keypresses
2117 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2118 * to buffer keypresses.
2119 */
2120 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002122 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2123 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2124 sourceBounds);
2125 }
2126
Michael Jurkaa33411c2012-06-14 16:18:21 -07002127 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002130 }
2131
2132 /**
2133 * Starts the global search activity. This code is a copied from SearchManager
2134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002135 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002137 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2139 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2140 if (globalSearchActivity == null) {
2141 Log.w(TAG, "No global search activity found.");
2142 return;
2143 }
2144 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2145 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2146 intent.setComponent(globalSearchActivity);
2147 // Make sure that we have a Bundle to put source in
2148 if (appSearchData == null) {
2149 appSearchData = new Bundle();
2150 } else {
2151 appSearchData = new Bundle(appSearchData);
2152 }
Adam Cohen9211d422014-10-07 18:14:53 -07002153 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 if (!appSearchData.containsKey("source")) {
2155 appSearchData.putString("source", getPackageName());
2156 }
2157 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2158 if (!TextUtils.isEmpty(initialQuery)) {
2159 intent.putExtra(SearchManager.QUERY, initialQuery);
2160 }
2161 if (selectInitialQuery) {
2162 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2163 }
2164 intent.setSourceBounds(sourceBounds);
2165 try {
2166 startActivity(intent);
2167 } catch (ActivityNotFoundException ex) {
2168 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
2171
Yura4f93ec62014-02-04 14:15:21 +00002172 public boolean isOnCustomContent() {
2173 return mWorkspace.isOnOrMovingToCustomContent();
2174 }
2175
Winson Chung70d72102011-08-12 11:24:35 -07002176 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002177 public boolean onPrepareOptionsMenu(Menu menu) {
2178 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002179 if (!isOnCustomContent()) {
2180 // Close any open folders
2181 closeFolder();
2182 // Stop resizing any widgets
2183 mWorkspace.exitWidgetResizeMode();
2184 if (!mWorkspace.isInOverviewMode()) {
2185 // Show the overview mode
2186 showOverviewMode(true);
2187 } else {
2188 showWorkspace(true);
2189 }
Winson Chunge0298742014-01-17 12:03:00 -08002190 }
Adam Cohen9211d422014-10-07 18:14:53 -07002191 if (mLauncherCallbacks != null) {
2192 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2193 }
2194
Michael Jurkab94f3f82013-09-11 18:08:54 +02002195 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002198 @Override
2199 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002200 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002201 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002202 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002203 }
2204
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 public boolean isWorkspaceLocked() {
2206 return mWorkspaceLoading || mWaitingForResult;
2207 }
2208
Adam Cohen517a7f52014-03-01 12:12:59 -08002209 public boolean isWorkspaceLoading() {
2210 return mWorkspaceLoading;
2211 }
2212
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002213 private void setWorkspaceLoading(boolean value) {
2214 boolean isLocked = isWorkspaceLocked();
2215 mWorkspaceLoading = value;
2216 if (isLocked != isWorkspaceLocked()) {
2217 onWorkspaceLockedChanged();
2218 }
2219 }
2220
2221 private void setWaitingForResult(boolean value) {
2222 boolean isLocked = isWorkspaceLocked();
2223 mWaitingForResult = value;
2224 if (isLocked != isWorkspaceLocked()) {
2225 onWorkspaceLockedChanged();
2226 }
2227 }
2228
Adam Cohen9211d422014-10-07 18:14:53 -07002229 protected void onWorkspaceLockedChanged() {
2230 if (mLauncherCallbacks != null) {
2231 mLauncherCallbacks.onWorkspaceLockedChanged();
2232 }
2233 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002234
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2239 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002240 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002243
Sunny Goyale6b63a32015-01-16 16:14:29 -08002244 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002245 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002246 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2247 }
2248
Sunny Goyale6b63a32015-01-16 16:14:29 -08002249 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002250 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2251 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002252 if (appWidgetInfo.configure != null) {
2253 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002254 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002255
Bjorn Bringert7984c942009-12-09 15:38:25 +00002256 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002257 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2258 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002261 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002262 Runnable onComplete = new Runnable() {
2263 @Override
2264 public void run() {
2265 // Exit spring loaded mode if necessary after adding the widget
2266 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2267 null);
2268 }
2269 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002271 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002272 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
2274 }
Romain Guycbb89e42009-06-08 15:52:54 -07002275
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002276 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002277 // Close any folders that may be open.
2278 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002279 mWorkspace.moveToCustomContentScreen(animate);
2280 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002281
2282 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2283 int[] cell, int spanX, int spanY) {
2284 switch (info.itemType) {
2285 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2286 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2287 int span[] = new int[2];
2288 span[0] = spanX;
2289 span[1] = spanY;
2290 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2291 container, screenId, cell, span);
2292 break;
2293 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2294 processShortcutFromDrop(info.componentName, container, screenId, cell);
2295 break;
2296 default:
2297 throw new IllegalStateException("Unknown item type: " + info.itemType);
2298 }
2299 }
2300
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 /**
2302 * Process a shortcut drop.
2303 *
2304 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002308 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2309 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002310 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002313 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314
2315 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.cellX = cell[0];
2317 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002319
2320 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2321 createShortcutIntent.setComponent(componentName);
2322 processShortcut(createShortcutIntent);
2323 }
2324
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 /**
2326 * Process a widget drop.
2327 *
2328 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002332 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2333 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002337 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002338 mPendingAddInfo.minSpanX = info.minSpanX;
2339 mPendingAddInfo.minSpanY = info.minSpanY;
2340
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 mPendingAddInfo.cellX = cell[0];
2343 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002345 if (span != null) {
2346 mPendingAddInfo.spanX = span[0];
2347 mPendingAddInfo.spanY = span[1];
2348 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 AppWidgetHostView hostView = info.boundWidget;
2351 int appWidgetId;
2352 if (hostView != null) {
2353 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002354 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002355 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002356 // In this case, we either need to start an activity to get permission to bind
2357 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002358 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002359 Bundle options = info.bindOptions;
2360
Sunny Goyalffe83f12014-08-14 17:39:34 -07002361 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2362 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002363 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002364 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002365 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002366 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2368 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2369 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002370 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2371 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002372 // TODO: we need to make sure that this accounts for the options bundle.
2373 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002374 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2375 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002377 }
2378
Joe Onoratodeb98af2010-02-19 14:59:39 -08002379 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002380 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 }
2382
Winson Chung24ab2f12010-09-16 14:10:47 -07002383 void processWallpaper(Intent intent) {
2384 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2385 }
2386
Adam Cohendcd297f2013-06-18 13:13:40 -07002387 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002388 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002389 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 folderInfo.title = getText(R.string.folder_name);
2391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002395 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396
2397 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 FolderIcon newFolder =
2399 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002401 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002402 // Force measure the new folder icon
2403 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2404 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002405 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
Romain Guycbb89e42009-06-08 15:52:54 -07002407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002409 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 }
2411
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002412 /**
2413 * Registers various content observers. The current implementation registers
2414 * only a favorites observer to keep track of the favorites applications.
2415 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002416 private void registerContentObservers() {
2417 ContentResolver resolver = getContentResolver();
2418 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2419 true, mWidgetObserver);
2420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 @Override
2423 public boolean dispatchKeyEvent(KeyEvent event) {
2424 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2425 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002428 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002429 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002430 dumpState();
2431 return true;
2432 }
2433 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 }
2435 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2436 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 return true;
2439 }
2440 }
2441
2442 return super.dispatchKeyEvent(event);
2443 }
2444
Joe Onorato88ec0992009-11-19 13:16:06 -08002445 @Override
2446 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002447 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2448 return;
2449 }
2450
Winson Chung3d9490a2015-03-24 17:38:42 -07002451 LauncherAccessibilityDelegate delegate =
2452 LauncherAppState.getInstance().getAccessibilityDelegate();
2453 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002454 return;
2455 }
2456
Winson Chungb745afb2015-03-02 11:51:23 -08002457 if (isAppsViewVisible()) {
2458 showWorkspace(true);
2459 } else if (isWidgetsViewVisible()) {
2460 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002461 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002462 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002463 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002464 Folder openFolder = mWorkspace.getOpenFolder();
2465 if (openFolder.isEditingName()) {
2466 openFolder.dismissEditingName();
2467 } else {
2468 closeFolder();
2469 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002471 mWorkspace.exitWidgetResizeMode();
2472
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002473 // Back button is a no-op here, but give at least some feedback for the button press
2474 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002475 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002476 }
2477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002479 * Re-listen when widgets are reset.
2480 */
Adam Cohen091440a2015-03-18 14:16:05 -07002481 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002482 if (mAppWidgetHost != null) {
2483 mAppWidgetHost.startListening();
2484 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002485 }
2486
2487 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 * Launches the intent referred by the clicked shortcut.
2489 *
2490 * @param v The view representing the clicked shortcut.
2491 */
2492 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002493 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2494 // view has detached (it's possible for this to happen if the view is removed mid touch).
2495 if (v.getWindowToken() == null) {
2496 return;
2497 }
2498
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002499 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002500 return;
2501 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002502
Adam Cohen1697b792013-09-17 19:08:21 -07002503 if (v instanceof Workspace) {
2504 if (mWorkspace.isInOverviewMode()) {
2505 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002506 }
2507 return;
2508 }
2509
2510 if (v instanceof CellLayout) {
2511 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002512 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514 }
2515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002517 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002520 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002522 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002523 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002525 } else if (tag instanceof AppInfo) {
2526 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002527 } else if (tag instanceof LauncherAppWidgetInfo) {
2528 if (v instanceof PendingAppWidgetHostView) {
2529 onClickPendingWidget((PendingAppWidgetHostView) v);
2530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 }
2532 }
2533
Sunny Goyal508da152014-08-14 10:53:27 -07002534 public void onClickPagedViewIcon(View v) {
2535 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002536 if (mLauncherCallbacks != null) {
2537 mLauncherCallbacks.onClickPagedViewIcon(v);
2538 }
Sunny Goyal508da152014-08-14 10:53:27 -07002539 }
2540
Michael Jurka0e260592010-06-30 17:07:39 -07002541 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002542 return false;
2543 }
2544
Michael Jurkaaf442092010-06-10 17:01:57 -07002545 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002546 * Event handler for the app widget view which has not fully restored.
2547 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002549 if (mIsSafeModeEnabled) {
2550 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2551 return;
2552 }
2553
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002555 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002556 int widgetId = info.appWidgetId;
2557 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2558 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002559 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2560 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 mPendingAddInfo.copyFrom(info);
2562 mPendingAddWidgetId = widgetId;
2563
Sunny Goyalffe83f12014-08-14 17:39:34 -07002564 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2565 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 } else if (info.installProgress < 0) {
2568 // The install has not been queued
2569 final String packageName = info.providerName.getPackageName();
2570 showBrokenAppInstallDialog(packageName,
2571 new DialogInterface.OnClickListener() {
2572 public void onClick(DialogInterface dialog, int id) {
2573 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2574 }
2575 });
2576 } else {
2577 // Download has started.
2578 final String packageName = info.providerName.getPackageName();
2579 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002580 }
2581 }
2582
2583 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002584 * Event handler for the "grid" button that appears on the home screen, which
2585 * enters all apps mode.
2586 *
2587 * @param v The view that was clicked.
2588 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 protected void onClickAllAppsButton(View v) {
2590 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002591 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 showWorkspace(true);
2593 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002594 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 }
Adam Cohen9211d422014-10-07 18:14:53 -07002596 if (mLauncherCallbacks != null) {
2597 mLauncherCallbacks.onClickAllAppsButton(v);
2598 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
2600
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 private void showBrokenAppInstallDialog(final String packageName,
2602 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002603 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 .setTitle(R.string.abandoned_promises_title)
2605 .setMessage(R.string.abandoned_promise_explanation)
2606 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2607 .setNeutralButton(R.string.abandoned_clean_this,
2608 new DialogInterface.OnClickListener() {
2609 public void onClick(DialogInterface dialog, int id) {
2610 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2611 mWorkspace.removeAbandonedPromise(packageName, user);
2612 }
2613 })
2614 .create().show();
2615 return;
2616 }
2617
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 /**
2619 * Event handler for an app shortcut click.
2620 *
2621 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2622 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2625 Object tag = v.getTag();
2626 if (!(tag instanceof ShortcutInfo)) {
2627 throw new IllegalArgumentException("Input must be a Shortcut");
2628 }
2629
2630 // Open shortcut
2631 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632
2633 if (shortcut.isDisabled != 0) {
2634 int error = R.string.activity_not_available;
2635 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2636 error = R.string.safemode_shortcut_error;
2637 }
2638 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 final Intent intent = shortcut.intent;
2643
2644 // Check for special shortcuts
2645 if (intent.getComponent() != null) {
2646 final String shortcutClass = intent.getComponent().getClassName();
2647
2648 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2649 MemoryDumpActivity.startDump(this);
2650 return;
2651 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2652 toggleShowWeightWatcher();
2653 return;
2654 }
2655 }
2656
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002658 if ((v instanceof BubbleTextView)
2659 && shortcut.isPromise()
2660 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002661 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002662 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 new DialogInterface.OnClickListener() {
2664 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002665 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 return;
2669 }
2670
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002673
2674 if (mLauncherCallbacks != null) {
2675 mLauncherCallbacks.onClickAppShortcut(v);
2676 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 }
2678
Adam Cohen091440a2015-03-18 14:16:05 -07002679 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002681 final ShortcutInfo shortcut;
2682 final Intent intent;
2683 if (tag instanceof ShortcutInfo) {
2684 shortcut = (ShortcutInfo) tag;
2685 intent = shortcut.intent;
2686 int[] pos = new int[2];
2687 v.getLocationOnScreen(pos);
2688 intent.setSourceBounds(new Rect(pos[0], pos[1],
2689 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002690
Sunny Goyal508da152014-08-14 10:53:27 -07002691 } else if (tag instanceof AppInfo) {
2692 shortcut = null;
2693 intent = ((AppInfo) tag).intent;
2694 } else {
2695 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2696 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697
2698 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 mStats.recordLaunch(intent, shortcut);
2700
2701 if (success && v instanceof BubbleTextView) {
2702 mWaitingForResume = (BubbleTextView) v;
2703 mWaitingForResume.setStayPressed(true);
2704 }
2705 }
2706
2707 /**
2708 * Event handler for a folder icon click.
2709 *
2710 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2711 */
2712 protected void onClickFolderIcon(View v) {
2713 if (LOGD) Log.d(TAG, "onClickFolder");
2714 if (!(v instanceof FolderIcon)){
2715 throw new IllegalArgumentException("Input must be a FolderIcon");
2716 }
2717
2718 FolderIcon folderIcon = (FolderIcon) v;
2719 final FolderInfo info = folderIcon.getFolderInfo();
2720 Folder openFolder = mWorkspace.getFolderForTag(info);
2721
2722 // If the folder info reports that the associated folder is open, then verify that
2723 // it is actually opened. There have been a few instances where this gets out of sync.
2724 if (info.opened && openFolder == null) {
2725 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2726 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2727 info.opened = false;
2728 }
2729
2730 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2731 // Close any open folder
2732 closeFolder();
2733 // Open the requested folder
2734 openFolder(folderIcon);
2735 } else {
2736 // Find the open folder...
2737 int folderScreen;
2738 if (openFolder != null) {
2739 folderScreen = mWorkspace.getPageForView(openFolder);
2740 // .. and close it
2741 closeFolder(openFolder);
2742 if (folderScreen != mWorkspace.getCurrentPage()) {
2743 // Close any folder open on the current screen
2744 closeFolder();
2745 // Pull the folder onto this screen
2746 openFolder(folderIcon);
2747 }
2748 }
2749 }
Adam Cohen9211d422014-10-07 18:14:53 -07002750
2751 if (mLauncherCallbacks != null) {
2752 mLauncherCallbacks.onClickFolderIcon(v);
2753 }
Michael Jurka5130e402011-10-13 04:55:35 -07002754 }
2755
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002756 /**
2757 * Event handler for the (Add) Widgets button that appears after a long press
2758 * on the home screen.
2759 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002760 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002761 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002762 if (mIsSafeModeEnabled) {
2763 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2764 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002765 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onClickAddWidgetButton(view);
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002770 }
2771
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 /**
2773 * Event handler for the wallpaper picker button that appears after a long press
2774 * on the home screen.
2775 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002776 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002777 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002778 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2779 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002780
2781 if (mLauncherCallbacks != null) {
2782 mLauncherCallbacks.onClickWallpaperPicker(v);
2783 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002784 }
2785
2786 /**
2787 * Event handler for a click on the settings button that appears after a long press
2788 * on the home screen.
2789 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002790 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002792 if (mLauncherCallbacks != null) {
2793 mLauncherCallbacks.onClickSettingsButton(v);
2794 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002795 }
2796
Michael Jurka5130e402011-10-13 04:55:35 -07002797 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002798 // Provide the same haptic feedback that the system offers for virtual keys.
2799 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002800 }
2801
Adam Cohen61f560d2013-09-30 15:58:20 -07002802 public void performHapticFeedbackOnTouchDown(View v) {
2803 // Provide the same haptic feedback that the system offers for virtual keys.
2804 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2805 }
2806
2807 public View.OnTouchListener getHapticFeedbackTouchListener() {
2808 if (mHapticFeedbackTouchListener == null) {
2809 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2810 @Override
2811 public boolean onTouch(View v, MotionEvent event) {
2812 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2814 }
2815 return false;
2816 }
2817 };
2818 }
2819 return mHapticFeedbackTouchListener;
2820 }
2821
Adam Cohen9211d422014-10-07 18:14:53 -07002822 public void onDragStarted(View view) {
2823 if (isOnCustomContent()) {
2824 // Custom content screen doesn't participate in drag and drop. If on custom
2825 // content screen, move to default.
2826 moveWorkspaceToDefaultScreen();
2827 }
2828
2829 if (mLauncherCallbacks != null) {
2830 mLauncherCallbacks.onDragStarted(view);
2831 }
2832 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002833
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002834 /**
2835 * Called when the user stops interacting with the launcher.
2836 * This implies that the user is now on the homescreen and is not doing housekeeping.
2837 */
Adam Cohen9211d422014-10-07 18:14:53 -07002838 protected void onInteractionEnd() {
2839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onInteractionEnd();
2841 }
2842 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002843
2844 /**
2845 * Called when the user starts interacting with the launcher.
2846 * The possible interactions are:
2847 * - open all apps
2848 * - reorder an app shortcut, or a widget
2849 * - open the overview mode.
2850 * This is a good time to stop doing things that only make sense
2851 * when the user is on the homescreen and not doing housekeeping.
2852 */
Adam Cohen9211d422014-10-07 18:14:53 -07002853 protected void onInteractionBegin() {
2854 if (mLauncherCallbacks != null) {
2855 mLauncherCallbacks.onInteractionBegin();
2856 }
2857 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002858
Kenny Guyf07af7b2014-07-31 11:39:16 +01002859 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002860 try {
2861 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002862 launcherApps.showAppDetailsForProfile(componentName, user);
2863 } catch (SecurityException e) {
2864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2865 Log.e(TAG, "Launcher does not have permission to launch settings");
2866 } catch (ActivityNotFoundException e) {
2867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2868 Log.e(TAG, "Unable to launch settings");
2869 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002870 }
2871
Michael Jurka1e2f4652013-07-08 18:03:46 -07002872 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002873 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2874 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002875 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002876 // System applications cannot be installed. For now, show a toast explaining that.
2877 // We may give them the option of disabling apps this way.
2878 int messageId = R.string.uninstall_system_app_text;
2879 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002880 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002881 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002882 String packageName = componentName.getPackageName();
2883 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 Intent intent = new Intent(
2885 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002886 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2887 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002888 if (user != null) {
2889 user.addToIntent(intent, Intent.EXTRA_USER);
2890 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002893 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002894 }
2895
Michael Jurka86a720e2012-05-09 11:23:23 -07002896 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002897 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002899 // Only launch using the new animation if the shortcut has not opted out (this is a
2900 // private contract between launcher and may be ignored in the future).
2901 boolean useLaunchAnimation = (v != null) &&
2902 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002903 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2904 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002905
Kenny Guy1317e2d2014-05-08 18:52:50 +01002906 UserHandleCompat user = null;
2907 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2908 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2909 user = userManager.getUserForSerialNumber(serialNumber);
2910 }
2911
2912 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002913 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002914 ActivityOptions opts = null;
2915 if (sClipRevealMethod != null) {
2916 // TODO: call method directly when Launcher3 can depend on M APIs
2917 int left = 0, top = 0;
2918 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2919 if (v instanceof TextView) {
2920 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002921 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2922 if (icon != null) {
2923 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002924 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002925 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002926 width = bounds.width();
2927 height = bounds.height();
2928 }
2929 }
2930 try {
2931 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2932 left, top, width, height);
2933 } catch (IllegalAccessException e) {
2934 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2935 sClipRevealMethod = null;
2936 } catch (InvocationTargetException e) {
2937 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2938 sClipRevealMethod = null;
2939 }
2940 }
2941 if (opts == null) {
2942 opts = Utilities.isLmpOrAbove() ?
2943 ActivityOptions.makeCustomAnimation(this,
2944 R.anim.task_open_enter, R.anim.no_anim) :
2945 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2946 v.getMeasuredWidth(), v.getMeasuredHeight());
2947 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002948 optsBundle = opts.toBundle();
2949 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002950
2951 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2952 // Could be launching some bookkeeping activity
2953 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002954 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002955 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002956 launcherApps.startActivityForProfile(intent.getComponent(), user,
2957 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002958 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002959 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 } catch (SecurityException e) {
2961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002962 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002964 "or use the exported attribute for this activity. "
2965 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002969
Michael Jurka86a720e2012-05-09 11:23:23 -07002970 boolean startActivitySafely(View v, Intent intent, Object tag) {
2971 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002972 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2973 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2974 return false;
2975 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002976 try {
2977 success = startActivity(v, intent, tag);
2978 } catch (ActivityNotFoundException e) {
2979 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2980 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2981 }
2982 return success;
2983 }
2984
Adam Cohen268c4752012-06-06 17:47:33 -07002985 /**
2986 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2987 * in the DragLayer in the exact absolute location of the original FolderIcon.
2988 */
2989 private void copyFolderIconToImage(FolderIcon fi) {
2990 final int width = fi.getMeasuredWidth();
2991 final int height = fi.getMeasuredHeight();
2992
2993 // Lazy load ImageView, Bitmap and Canvas
2994 if (mFolderIconImageView == null) {
2995 mFolderIconImageView = new ImageView(this);
2996 }
2997 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2998 mFolderIconBitmap.getHeight() != height) {
2999 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3000 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3001 }
3002
3003 DragLayer.LayoutParams lp;
3004 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3005 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3006 } else {
3007 lp = new DragLayer.LayoutParams(width, height);
3008 }
3009
Adam Cohen307fe232012-08-16 17:55:58 -07003010 // The layout from which the folder is being opened may be scaled, adjust the starting
3011 // view size by this scale factor.
3012 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003013 lp.customPosition = true;
3014 lp.x = mRectForFolderAnimation.left;
3015 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003016 lp.width = (int) (scale * width);
3017 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003018
3019 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3020 fi.draw(mFolderIconCanvas);
3021 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 if (fi.getFolder() != null) {
3023 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3024 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003025 }
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // Just in case this image view is still in the drag layer from a previous animation,
3027 // we remove it and re-add it.
3028 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3029 mDragLayer.removeView(mFolderIconImageView);
3030 }
3031 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003032 if (fi.getFolder() != null) {
3033 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003034 }
Adam Cohen268c4752012-06-06 17:47:33 -07003035 }
3036
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003038 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3040 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3041 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3042
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 FolderInfo info = (FolderInfo) fi.getTag();
3044 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3045 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003046 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3047 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 }
3049
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3051 copyFolderIconToImage(fi);
3052 fi.setVisibility(View.INVISIBLE);
3053
Michael Jurka2ecf9952012-06-18 12:52:28 -07003054 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003055 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003056 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003057 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3058 }
3059 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 oa.start();
3061 }
3062
Adam Cohen268c4752012-06-06 17:47:33 -07003063 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003064 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003065 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3066 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3067 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003070
Adam Cohen268c4752012-06-06 17:47:33 -07003071 // We remove and re-draw the FolderIcon in-case it has changed
3072 mDragLayer.removeView(mFolderIconImageView);
3073 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003074 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003075 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003076 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003077 oa.addListener(new AnimatorListenerAdapter() {
3078 @Override
3079 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003080 if (cl != null) {
3081 cl.clearFolderLeaveBehind();
3082 // Remove the ImageView copy of the FolderIcon and make the original visible.
3083 mDragLayer.removeView(mFolderIconImageView);
3084 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003085 }
3086 }
3087 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003088 oa.start();
3089 }
3090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003092 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 * is animated relative to the specified View. If the View is null, no animation
3094 * is played.
3095 *
3096 * @param folderInfo The FolderInfo describing the folder to open.
3097 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003098 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003099 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003100 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Adam Cohen4554ee12011-08-03 16:13:21 -07003104 // Just verify that the folder hasn't already been added to the DragLayer.
3105 // There was a one-off crash where the folder had a parent already.
3106 if (folder.getParent() == null) {
3107 mDragLayer.addView(folder);
3108 mDragController.addDropTarget((DropTarget) folder);
3109 } else {
3110 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3111 folder.getParent() + ").");
3112 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003114 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003115
3116 // Notify the accessibility manager that this folder "window" has appeared and occluded
3117 // the workspace items
3118 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3119 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003120 }
3121
3122 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003123 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003125 if (folder.isEditingName()) {
3126 folder.dismissEditingName();
3127 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 closeFolder(folder);
3129 }
3130 }
3131
3132 void closeFolder(Folder folder) {
3133 folder.getInfo().opened = false;
3134
3135 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3136 if (parent != null) {
3137 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3138 shrinkAndFadeInFolderIcon(fi);
3139 }
3140 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003141
3142 // Notify the accessibility manager that this folder "window" has disappeard and no
3143 // longer occludeds the workspace items
3144 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003148 if (!isDraggingEnabled()) return false;
3149 if (isWorkspaceLocked()) return false;
3150 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151
Adam Cohen1697b792013-09-17 19:08:21 -07003152 if (v instanceof Workspace) {
3153 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003154 if (mWorkspace.enterOverviewMode()) {
3155 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3156 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3157 return true;
3158 } else {
3159 return false;
3160 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003161 } else {
3162 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003163 }
Adam Cohen1697b792013-09-17 19:08:21 -07003164 }
3165
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003166 CellLayout.CellInfo longClickCellInfo = null;
3167 View itemUnderLongClick = null;
3168 if (v.getTag() instanceof ItemInfo) {
3169 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003170 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003171 itemUnderLongClick = longClickCellInfo.cell;
3172 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 }
3174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 // The hotseat touch handling does not go through Workspace, and we always allow long press
3176 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003177 final boolean inHotseat = isHotseatLayout(v);
3178 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003180 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003181 // User long pressed on empty space
3182 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3183 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003184 if (mWorkspace.isInOverviewMode()) {
3185 mWorkspace.startReordering(v);
3186 } else {
3187 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003190 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3191 mHotseat.getOrderInHotseat(
3192 longClickCellInfo.cellX,
3193 longClickCellInfo.cellY));
3194 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003196 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 }
3198 }
3199 }
3200 return true;
3201 }
3202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 boolean isHotseatLayout(View layout) {
3204 return mHotseat != null && layout != null &&
3205 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3206 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 /**
3209 * Returns the CellLayout of the specified container at the specified screen.
3210 */
Sunny Goyal92820592015-03-02 11:31:35 -08003211 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3213 if (mHotseat != null) {
3214 return mHotseat.getLayout();
3215 } else {
3216 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003217 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003219 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003220 }
3221 }
3222
Winson Chungb745afb2015-03-02 11:51:23 -08003223 /**
3224 * For overridden classes.
3225 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003226 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003227 return isAppsViewVisible();
3228 }
3229
3230 public boolean isAppsViewVisible() {
3231 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3232 }
3233
3234 public boolean isWidgetsViewVisible() {
3235 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003236 }
3237
Craig Mautner360310b2012-10-26 15:13:08 -07003238 private void setWorkspaceBackground(boolean workspace) {
3239 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003240 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003241 }
3242
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003243 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003244 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3245 int curflags = getWindow().getAttributes().flags
3246 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3247 if (wpflags != curflags) {
3248 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3249 }
Craig Mautner360310b2012-10-26 15:13:08 -07003250 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003251 }
3252
Michael Jurkae326f182011-11-21 14:05:46 -08003253 @Override
3254 public void onTrimMemory(int level) {
3255 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003256 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3257 // The widget preview db can result in holding onto over
3258 // 3MB of memory for caching which isn't necessary.
3259 SQLiteDatabase.releaseMemory();
3260
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003261 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003262 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003263 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003264 }
Michael Jurkae326f182011-11-21 14:05:46 -08003265 }
3266 }
3267
Winson Chungb745afb2015-03-02 11:51:23 -08003268 @Override
3269 public void onStateTransitionHideSearchBar() {
3270 // Hide the search bar
3271 if (mSearchDropTargetBar != null) {
3272 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3273 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003274 }
3275
Winson Chungb745afb2015-03-02 11:51:23 -08003276 protected void showWorkspace(boolean animated) {
3277 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003278 }
3279
Adam Cohened66b2b2012-01-23 17:28:51 -08003280 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003281 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003282 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003283 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003284 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3285 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003286
Winson Chungc7d2b602012-05-16 17:02:20 -07003287 // Show the search bar (only animate if we were showing the drop target bar in spring
3288 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003289 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003290 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003291 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003292
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293 // Set focus to the AppsCustomize button
3294 if (mAllAppsButton != null) {
3295 mAllAppsButton.requestFocus();
3296 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003297 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003298
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003299 // Change the state *after* we've called all the transition code
3300 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003301
Winson Chung5fb63472011-02-02 17:03:37 -08003302 // Resume the auto-advance of widgets
3303 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003304 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003305
alanv1d4fde62012-10-17 13:15:47 -07003306 // Send an accessibility event to announce the context change
3307 getWindow().getDecorView()
3308 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003309
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003310 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003311 }
3312
Adam Cohened307df2013-10-02 09:37:31 -07003313 void showOverviewMode(boolean animated) {
3314 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003315 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3316 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003317 mState = State.WORKSPACE;
3318 onWorkspaceShown(animated);
3319 }
3320
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003321 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003322 }
3323
Winson Chungb745afb2015-03-02 11:51:23 -08003324 /**
3325 * Shows the apps view.
3326 */
3327 void showAppsView(boolean animated, boolean resetListToTop) {
3328 if (resetListToTop) {
3329 mAppsView.scrollToTop();
3330 }
3331 showAppsOrWidgets(animated, State.APPS);
3332 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003333
Winson Chungb745afb2015-03-02 11:51:23 -08003334 /**
3335 * Shows the widgets view.
3336 */
3337 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003338 if (resetPageToZero) {
3339 mAppsCustomizeTabHost.reset();
3340 }
Winson Chungb745afb2015-03-02 11:51:23 -08003341 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003342 mAppsCustomizeTabHost.post(new Runnable() {
3343 @Override
3344 public void run() {
3345 // We post this in-case the all apps view isn't yet constructed.
3346 mAppsCustomizeTabHost.requestFocus();
3347 }
3348 });
Winson Chungb745afb2015-03-02 11:51:23 -08003349 }
3350
3351 /**
3352 * Sets up the transition to show the apps/widgets view.
3353 */
3354 private void showAppsOrWidgets(boolean animated, State toState) {
3355 if (mState != State.WORKSPACE) return;
3356 if (toState != State.APPS && toState != State.WIDGETS) return;
3357
3358 if (toState == State.APPS) {
3359 mStateTransitionAnimation.startAnimationToAllApps(animated);
3360 } else {
3361 mStateTransitionAnimation.startAnimationToWidgets(animated);
3362 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363
Michael Jurkab3e22d92011-10-31 15:58:33 -07003364 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003365 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003366
3367 // Pause the auto-advance of widgets until we are out of AllApps
3368 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003369 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 closeFolder();
3371
3372 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003373 getWindow().getDecorView()
3374 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375 }
3376
Adam Cohen7777d962011-08-18 18:58:38 -07003377 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003378 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3379 mState == State.WIDGETS_SPRING_LOADED) {
3380 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003381 }
Winson Chungb745afb2015-03-02 11:51:23 -08003382
3383 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3384 true /* animated */, null /* onCompleteRunnable */);
3385 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003386 }
Adam Cohen7777d962011-08-18 18:58:38 -07003387
Adam Cohenad4e15c2013-10-17 16:21:35 -07003388 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003389 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003390 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003391
Winson Chunge7a03942011-08-05 15:05:12 -07003392 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003393 @Override
3394 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003395 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003396 // Before we show workspace, hide all apps again because
3397 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3398 // clean up our state transition functions
3399 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003400 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003401 } else {
3402 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003403 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003404 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003405 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003406 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407
Michael Jurkad3ef3062010-11-23 16:23:58 -08003408 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003409 if (mState == State.APPS_SPRING_LOADED) {
3410 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3411 mState = State.APPS;
3412 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3413 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3414 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003415 }
3416 // Otherwise, we are not in spring loaded mode, so don't do anything.
3417 }
3418
Michael Jurkab3e22d92011-10-31 15:58:33 -07003419 void lockAllApps() {
3420 // TODO
3421 }
3422
3423 void unlockAllApps() {
3424 // TODO
3425 }
3426
Sunny Goyal594d76d2014-11-06 10:12:54 -08003427 protected void disableVoiceButtonProxy(boolean disable) {
3428 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003429 }
3430
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003431 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003432 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3433 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003434 }
3435
Adam Cohen24ce0b32014-01-14 16:18:14 -08003436 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003437 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3438 if (searchProvider == null) {
3439 return null;
3440 }
3441
3442 Bundle opts = new Bundle();
3443 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003444 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003445
3446 SharedPreferences sp = getSharedPreferences(
3447 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3448 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003449 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003450 if (!searchProvider.provider.flattenToString().equals(
3451 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003452 || (widgetInfo == null)
3453 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003454 // A valid widget is not already bound.
3455 if (widgetId > -1) {
3456 mAppWidgetHost.deleteAppWidgetId(widgetId);
3457 widgetId = -1;
3458 }
3459
3460 // Try to bind a new widget
3461 widgetId = mAppWidgetHost.allocateAppWidgetId();
3462
3463 if (!AppWidgetManagerCompat.getInstance(this)
3464 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3465 mAppWidgetHost.deleteAppWidgetId(widgetId);
3466 widgetId = -1;
3467 }
3468
3469 sp.edit()
3470 .putInt(QSB_WIDGET_ID, widgetId)
3471 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3472 .commit();
3473 }
3474
3475 if (widgetId != -1) {
3476 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3477 mQsb.updateAppWidgetOptions(opts);
3478 mQsb.setPadding(0, 0, 0, 0);
3479 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003480 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003481 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003482 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003483 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003484 }
3485
Michael Jurkad7c28052012-04-27 15:43:36 -07003486 @Override
3487 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003488 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003489 final List<CharSequence> text = event.getText();
3490 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003491 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003492 if (mState == State.APPS) {
3493 text.add("Apps");
3494 } else if (mState == State.WIDGETS) {
3495 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003496 } else {
3497 text.add(getString(R.string.all_apps_home_button_label));
3498 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003499 return result;
3500 }
3501
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003502 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003503 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003504 */
Adam Cohen091440a2015-03-18 14:16:05 -07003505 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003506 @Override
3507 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003508 closeSystemDialogs();
3509 }
3510 }
3511
3512 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003513 * Receives notifications whenever the appwidgets are reset.
3514 */
3515 private class AppWidgetResetObserver extends ContentObserver {
3516 public AppWidgetResetObserver() {
3517 super(new Handler());
3518 }
3519
3520 @Override
3521 public void onChange(boolean selfChange) {
3522 onAppWidgetReset();
3523 }
3524 }
3525
3526 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 * If the activity is currently paused, signal that we need to run the passed Runnable
3528 * in onResume.
3529 *
3530 * This needs to be called from incoming places where resources might have been loaded
3531 * while we are paused. That is becaues the Configuration might be wrong
3532 * when we're not running, and if it comes back to what it was when we
3533 * were paused, we are not restarted.
3534 *
3535 * Implementation of the method from LauncherModel.Callbacks.
3536 *
3537 * @return true if we are currently paused. The caller might be able to
3538 * skip some work in that case since we will come back again.
3539 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003540 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003541 if (mPaused) {
3542 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003543 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003544 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003545 }
3546 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003547 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003548 return true;
3549 } else {
3550 return false;
3551 }
3552 }
3553
Michael Jurkac402cd92013-05-20 15:49:32 +02003554 private boolean waitUntilResume(Runnable run) {
3555 return waitUntilResume(run, false);
3556 }
3557
Michael Jurka1e2f4652013-07-08 18:03:46 -07003558 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003559 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003560 }
3561
Michael Jurka7607c2f2013-04-03 14:33:19 -07003562 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003563 * If the activity is currently paused, signal that we need to re-run the loader
3564 * in onResume.
3565 *
3566 * This needs to be called from incoming places where resources might have been loaded
3567 * while we are paused. That is becaues the Configuration might be wrong
3568 * when we're not running, and if it comes back to what it was when we
3569 * were paused, we are not restarted.
3570 *
3571 * Implementation of the method from LauncherModel.Callbacks.
3572 *
3573 * @return true if we are currently paused. The caller might be able to
3574 * skip some work in that case since we will come back again.
3575 */
3576 public boolean setLoadOnResume() {
3577 if (mPaused) {
3578 Log.i(TAG, "setLoadOnResume");
3579 mOnResumeNeedsLoad = true;
3580 return true;
3581 } else {
3582 return false;
3583 }
3584 }
3585
3586 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003587 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003590 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003591 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003592 } else {
3593 return SCREEN_COUNT / 2;
3594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003596
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 /**
3598 * Refreshes the shortcuts shown on the workspace.
3599 *
3600 * Implementation of the method from LauncherModel.Callbacks.
3601 */
3602 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003603 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003604
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 // If we're starting binding all over again, clear any bind calls we'd postponed in
3606 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3607 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003608 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003609
Winson Chungd64d1762013-08-20 14:37:16 -07003610 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003611 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003612 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003613
Michael Jurka05bf6442011-11-30 20:28:53 -08003614 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003615 if (mHotseat != null) {
3616 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003617 }
3618 }
3619
Adam Cohendcd297f2013-06-18 13:13:40 -07003620 @Override
3621 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003622 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003623
Adam Cohen5084cba2013-09-03 12:01:16 -07003624 // If there are no screens, we need to have an empty screen
3625 if (orderedScreenIds.size() == 0) {
3626 mWorkspace.addExtraEmptyScreen();
3627 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003628
3629 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003630 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003631 if (hasCustomContentToLeft()) {
3632 mWorkspace.createCustomContentContainer();
3633 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003634 }
Winson Chung64359a52013-07-08 17:17:08 -07003635 }
3636
3637 @Override
3638 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003639 // Log to disk
3640 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3641 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3642 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003643 int count = orderedScreenIds.size();
3644 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003645 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003646 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003647 }
3648
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003649 @Override
3650 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3651 final long screenId, final int[] cell, final int spanX, final int spanY) {
3652 showWorkspace(true, new Runnable() {
3653
3654 @Override
3655 public void run() {
3656 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3657 addPendingItem(info, container, screenId, cell, spanX, spanY);
3658 }
3659 });
3660 }
3661
Adam Cohen39a06042013-07-19 14:30:12 -07003662 private boolean shouldShowWeightWatcher() {
3663 String spKey = LauncherAppState.getSharedPreferencesKey();
3664 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003665 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003666
3667 return show;
3668 }
3669
3670 private void toggleShowWeightWatcher() {
3671 String spKey = LauncherAppState.getSharedPreferencesKey();
3672 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3673 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3674
3675 show = !show;
3676
3677 SharedPreferences.Editor editor = sp.edit();
3678 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3679 editor.commit();
3680
3681 if (mWeightWatcher != null) {
3682 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3683 }
3684 }
3685
Winson Chungd64d1762013-08-20 14:37:16 -07003686 public void bindAppsAdded(final ArrayList<Long> newScreens,
3687 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003688 final ArrayList<ItemInfo> addAnimated,
3689 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003690 Runnable r = new Runnable() {
3691 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003692 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003693 }
3694 };
3695 if (waitUntilResume(r)) {
3696 return;
3697 }
3698
Winson Chungd64d1762013-08-20 14:37:16 -07003699 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003700 if (newScreens != null) {
3701 bindAddScreens(newScreens);
3702 }
Winson Chungd64d1762013-08-20 14:37:16 -07003703
3704 // We add the items without animation on non-visible pages, and with
3705 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003706 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003707 bindItems(addNotAnimated, 0,
3708 addNotAnimated.size(), false);
3709 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003710 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 bindItems(addAnimated, 0,
3712 addAnimated.size(), true);
3713 }
Winson Chungc58497e2013-09-03 17:48:37 -07003714
Winson Chung87412982013-10-03 18:34:14 -07003715 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003716 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003717
Winson Chungb745afb2015-03-02 11:51:23 -08003718 if (addedApps != null && mAppsView != null) {
3719 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003720 }
Winson Chungd64d1762013-08-20 14:37:16 -07003721 }
3722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 * Bind the items start-end from the list.
3725 *
3726 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727 */
Winson Chung64359a52013-07-08 17:17:08 -07003728 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3729 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003730 Runnable r = new Runnable() {
3731 public void run() {
3732 bindItems(shortcuts, start, end, forceAnimateIcons);
3733 }
3734 };
3735 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003736 return;
3737 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003738
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003740 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3741 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003742 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003743 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003744 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003745 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003746 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003747
3748 // Short circuit if we are loading dock items for a configuration which has no dock
3749 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3750 mHotseat == null) {
3751 continue;
3752 }
3753
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 switch (item.itemType) {
3755 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3756 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003757 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003758 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003759
Winson Chung64359a52013-07-08 17:17:08 -07003760 /*
3761 * TODO: FIX collision case
3762 */
Winson Chungce376632013-07-11 16:12:41 -07003763 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3764 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3765 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003766 View v = cl.getChildAt(item.cellX, item.cellY);
3767 Object tag = v.getTag();
3768 String desc = "Collision while binding workspace item: " + item
3769 + ". Collides with " + tag;
3770 if (LauncherAppState.isDogfoodBuild()) {
3771 throw (new RuntimeException(desc));
3772 } else {
3773 Log.d(TAG, desc);
3774 }
Winson Chungce376632013-07-11 16:12:41 -07003775 }
Winson Chung64359a52013-07-08 17:17:08 -07003776 }
3777
Adam Cohendcd297f2013-06-18 13:13:40 -07003778 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3779 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003780 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003781 // Animate all the applications up now
3782 shortcut.setAlpha(0f);
3783 shortcut.setScaleX(0f);
3784 shortcut.setScaleY(0f);
3785 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003786 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003789 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003790 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003791 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003792 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003793 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3794 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003796 default:
3797 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003800
Winson Chung997a9232013-07-24 15:33:46 -07003801 if (animateIcons) {
3802 // Animate to the correct page
3803 if (newShortcutsScreenId > -1) {
3804 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003805 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003806 final Runnable startBounceAnimRunnable = new Runnable() {
3807 public void run() {
3808 anim.playTogether(bounceAnims);
3809 anim.start();
3810 }
3811 };
Winson Chung997a9232013-07-24 15:33:46 -07003812 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003813 // We post the animation slightly delayed to prevent slowdowns
3814 // when we are loading right after we return to launcher.
3815 mWorkspace.postDelayed(new Runnable() {
3816 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003817 if (mWorkspace != null) {
3818 mWorkspace.snapToPage(newScreenIndex);
3819 mWorkspace.postDelayed(startBounceAnimRunnable,
3820 NEW_APPS_ANIMATION_DELAY);
3821 }
Winson Chung94d67682013-09-25 16:29:40 -07003822 }
3823 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003824 } else {
3825 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003826 }
3827 }
Winson Chung64359a52013-07-08 17:17:08 -07003828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003830 }
3831
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 /**
3833 * Implementation of the method from LauncherModel.Callbacks.
3834 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003835 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003836 Runnable r = new Runnable() {
3837 public void run() {
3838 bindFolders(folders);
3839 }
3840 };
3841 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003842 return;
3843 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003844 sFolders.clear();
3845 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003846 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847
3848 /**
3849 * Add the views for a widget to the workspace.
3850 *
3851 * Implementation of the method from LauncherModel.Callbacks.
3852 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003853 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003854 Runnable r = new Runnable() {
3855 public void run() {
3856 bindAppWidget(item);
3857 }
3858 };
3859 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003860 return;
3861 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003862
Daniel Sandler843e8602010-06-07 14:59:01 -04003863 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3864 if (DEBUG_WIDGETS) {
3865 Log.d(TAG, "bindAppWidget: " + item);
3866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 final Workspace workspace = mWorkspace;
3868
Adam Cohen59400422014-03-05 18:07:04 -08003869 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003870 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003871
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003872 if (!mIsSafeModeEnabled
3873 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3874 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003875 if (appWidgetInfo == null) {
3876 if (DEBUG_WIDGETS) {
3877 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3878 + " belongs to component " + item.providerName
3879 + ", as the povider is null");
3880 }
3881 LauncherModel.deleteItemFromDatabase(this, item);
3882 return;
3883 }
3884 // Note: This assumes that the id remap broadcast is received before this step.
3885 // If that is not the case, the id remap will be ignored and user may see the
3886 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003887 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003888 pendingInfo.spanX = item.spanX;
3889 pendingInfo.spanY = item.spanY;
3890 pendingInfo.minSpanX = item.minSpanX;
3891 pendingInfo.minSpanY = item.minSpanY;
3892 Bundle options =
3893 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3894
Sunny Goyalff572272014-07-23 13:58:07 -07003895 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003896 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3897 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003898
3899 // TODO consider showing a permission dialog when the widget is clicked.
3900 if (!success) {
3901 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3902 if (DEBUG_WIDGETS) {
3903 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3904 + " belongs to component " + item.providerName
3905 + ", as the launcher is unable to bing a new widget id");
3906 }
3907 LauncherModel.deleteItemFromDatabase(this, item);
3908 return;
3909 }
3910
3911 item.appWidgetId = newWidgetId;
3912
3913 // If the widget has a configure activity, it is still needs to set it up, otherwise
3914 // the widget is ready to go.
3915 item.restoreStatus = (appWidgetInfo.configure == null)
3916 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3917 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3918
3919 LauncherModel.updateItemInDatabase(this, item);
3920 }
3921
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003922 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003923 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003924 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003925 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3926 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003927 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003928
Sunny Goyal651077b2014-06-30 14:15:31 -07003929 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3930 } else {
3931 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003932 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3933 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003934 view.updateIcon(mIconCache);
3935 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003936 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003937 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003939
Joe Onorato9c1289c2009-08-17 11:03:03 -04003940 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003941 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942
Adam Cohendcd297f2013-06-18 13:13:40 -07003943 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003944 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003945 if (!item.isCustomWidget()) {
3946 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3947 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003948
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 workspace.requestLayout();
3950
Daniel Sandler843e8602010-06-07 14:59:01 -04003951 if (DEBUG_WIDGETS) {
3952 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3953 + (SystemClock.uptimeMillis()-start) + "ms");
3954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 }
3956
Sunny Goyalff572272014-07-23 13:58:07 -07003957 /**
3958 * Restores a pending widget.
3959 *
3960 * @param appWidgetId The app widget id
3961 * @param cellInfo The position on screen where to create the widget.
3962 */
3963 private void completeRestoreAppWidget(final int appWidgetId) {
3964 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3965 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3966 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3967 return;
3968 }
3969
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003970 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003971 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3972
3973 mWorkspace.reinflateWidgetsIfNecessary();
3974 LauncherModel.updateItemInDatabase(this, info);
3975 }
3976
Adam Cohen1462de32012-07-24 22:34:36 -07003977 public void onPageBoundSynchronously(int page) {
3978 mSynchronouslyBoundPages.add(page);
3979 }
3980
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 /**
3982 * Callback saying that there aren't any more items to bind.
3983 *
3984 * Implementation of the method from LauncherModel.Callbacks.
3985 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003986 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003987 Runnable r = new Runnable() {
3988 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003989 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003990 }
3991 };
3992 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003993 return;
3994 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003995 if (mSavedState != null) {
3996 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003997 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003999 mSavedState = null;
4000 }
4001
Adam Cohen1462de32012-07-24 22:34:36 -07004002 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004004 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004005 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004006
4007 // If we received the result of any pending adds while the loader was running (e.g. the
4008 // widget configuration forced an orientation change), process them now.
4009 if (sPendingAddItem != null) {
4010 final long screenId = completeAdd(sPendingAddItem);
4011
4012 // TODO: this moves the user to the page where the pending item was added. Ideally,
4013 // the screen would be guaranteed to exist after bind, and the page would be set through
4014 // the workspace restore process.
4015 mWorkspace.post(new Runnable() {
4016 @Override
4017 public void run() {
4018 mWorkspace.snapToScreenId(screenId);
4019 }
4020 });
4021 sPendingAddItem = null;
4022 }
4023
Sunny Goyale755d462014-07-22 13:48:29 -07004024 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004025
4026 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004027 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004028 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004029 }
4030
Adam Cohen3ed316a2014-07-23 18:21:20 -07004031 private void sendLoadingCompleteBroadcastIfNecessary() {
4032 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4033 String permission =
4034 getResources().getString(R.string.receive_first_load_broadcast_permission);
4035 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4036 sendBroadcast(intent, permission);
4037 SharedPreferences.Editor editor = mSharedPrefs.edit();
4038 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4039 editor.apply();
4040 }
4041 }
4042
Winson Chunga0b7e862013-09-05 16:03:15 -07004043 public boolean isAllAppsButtonRank(int rank) {
4044 if (mHotseat != null) {
4045 return mHotseat.isAllAppsButtonRank(rank);
4046 }
4047 return false;
4048 }
4049
Winson Chunga2413752012-04-03 14:22:34 -07004050 private boolean canRunNewAppsAnimation() {
4051 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4052 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4053 }
4054
Winson Chungc9168342013-06-26 14:54:55 -07004055 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4056 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4057 PropertyValuesHolder.ofFloat("alpha", 1f),
4058 PropertyValuesHolder.ofFloat("scaleX", 1f),
4059 PropertyValuesHolder.ofFloat("scaleY", 1f));
4060 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4061 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4062 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4063 return bounceAnim;
4064 }
4065
Adam Cohen27772732013-11-11 14:00:56 +00004066 public boolean useVerticalBarLayout() {
4067 return LauncherAppState.getInstance().getDynamicGrid().
4068 getDeviceProfile().isVerticalBarLayout();
4069 }
4070
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004071 protected Rect getSearchBarBounds() {
4072 return LauncherAppState.getInstance().getDynamicGrid().
4073 getDeviceProfile().getSearchBarBounds();
4074 }
4075
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004076 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004077 if (mSearchDropTargetBar == null) {
4078 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004079 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004080 if (mQsb != null) {
4081 mSearchDropTargetBar.removeView(mQsb);
4082 mQsb = null;
4083 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004084 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004085 }
4086
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004087 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 * Add the icons for all apps.
4089 *
4090 * Implementation of the method from LauncherModel.Callbacks.
4091 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004092 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004093 if (mAppsView != null) {
4094 mAppsView.setApps(apps);
4095 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004096 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004097 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Song70a48be2015-03-11 16:36:52 -07004098 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chung785d2eb2011-04-14 16:08:02 -07004099 }
Adam Cohen9211d422014-10-07 18:14:53 -07004100 if (mLauncherCallbacks != null) {
4101 mLauncherCallbacks.bindAllApplications(apps);
4102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 }
4104
4105 /**
4106 * A package was updated.
4107 *
4108 * Implementation of the method from LauncherModel.Callbacks.
4109 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004110 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004111 Runnable r = new Runnable() {
4112 public void run() {
4113 bindAppsUpdated(apps);
4114 }
4115 };
4116 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004117 return;
4118 }
4119
Winson Chungb745afb2015-03-02 11:51:23 -08004120 if (mAppsView != null) {
4121 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 }
4124
Sunny Goyal4390ace2014-10-13 11:33:11 -07004125 @Override
4126 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4127 Runnable r = new Runnable() {
4128 public void run() {
4129 bindWidgetsRestored(widgets);
4130 }
4131 };
4132 if (waitUntilResume(r)) {
4133 return;
4134 }
4135 mWorkspace.widgetsRestored(widgets);
4136 }
4137
Joe Onorato9c1289c2009-08-17 11:03:03 -04004138 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004139 * Some shortcuts were updated in the background.
4140 *
4141 * Implementation of the method from LauncherModel.Callbacks.
4142 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004143 @Override
4144 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4145 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004146 Runnable r = new Runnable() {
4147 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004148 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004149 }
4150 };
4151 if (waitUntilResume(r)) {
4152 return;
4153 }
4154
Sunny Goyal4390ace2014-10-13 11:33:11 -07004155 if (!updated.isEmpty()) {
4156 mWorkspace.updateShortcuts(updated);
4157 }
4158
4159 if (!removed.isEmpty()) {
4160 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4161 for (ShortcutInfo si : removed) {
4162 removedComponents.add(si.getTargetComponent());
4163 }
4164 mWorkspace.removeItemsByComponentName(removedComponents, user);
4165 // Notify the drag controller
4166 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004167 }
4168 }
4169
4170 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004171 * Update the state of a package, typically related to install state.
4172 *
4173 * Implementation of the method from LauncherModel.Callbacks.
4174 */
Sunny Goyale755d462014-07-22 13:48:29 -07004175 @Override
4176 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004177 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004178 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004179 }
4180 }
4181
4182 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004183 * Update the label and icon of all the icons in a package
4184 *
4185 * Implementation of the method from LauncherModel.Callbacks.
4186 */
4187 @Override
4188 public void updatePackageBadge(String packageName) {
4189 if (mWorkspace != null) {
4190 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4191 }
4192 }
4193
4194 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004195 * A package was uninstalled. We take both the super set of packageNames
4196 * in addition to specific applications to remove, the reason being that
4197 * this can be called when a package is updated as well. In that scenario,
4198 * we only remove specific components from the workspace, where as
4199 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004200 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004201 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 * Implementation of the method from LauncherModel.Callbacks.
4203 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004204 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004205 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004206 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004207 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004208 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004209 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004210 }
Winson Chungd64d1762013-08-20 14:37:16 -07004211 };
4212 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004213 return;
4214 }
4215
Sunny Goyal1a745e82014-10-02 15:58:31 -07004216 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004217 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4218 for (AppInfo info : appInfos) {
4219 removedComponents.add(info.componentName);
4220 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004221 if (!packageNames.isEmpty()) {
4222 mWorkspace.removeItemsByPackageName(packageNames, user);
4223 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004224 if (!removedComponents.isEmpty()) {
4225 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004226 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004227 // Notify the drag controller
4228 mDragController.onAppsRemoved(packageNames, removedComponents);
4229
Sunny Goyal1a745e82014-10-02 15:58:31 -07004230 } else {
4231 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004232 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004233
Winson Chungdf95eb12013-10-16 14:57:07 -07004234 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004235 if (mAppsView != null) {
4236 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 }
Joe Onoratobe386092009-11-17 17:32:16 -08004239
4240 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004241 * A number of packages were updated.
4242 */
Adam Cohen091440a2015-03-18 14:16:05 -07004243 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004244 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004245 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004246 bindPackagesUpdated(mWidgetsAndShortcuts);
4247 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004248 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004249 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004250 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4251 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4252 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004253 return;
4254 }
4255
Winson Chung64359a52013-07-08 17:17:08 -07004256 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004257 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004258 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004259 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004260 }
4261
Winson Chung400438b2011-01-16 17:53:48 -08004262 private int mapConfigurationOriActivityInfoOri(int configOri) {
4263 final Display d = getWindowManager().getDefaultDisplay();
4264 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4265 switch (d.getRotation()) {
4266 case Surface.ROTATION_0:
4267 case Surface.ROTATION_180:
4268 // We are currently in the same basic orientation as the natural orientation
4269 naturalOri = configOri;
4270 break;
4271 case Surface.ROTATION_90:
4272 case Surface.ROTATION_270:
4273 // We are currently in the other basic orientation to the natural orientation
4274 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4275 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4276 break;
4277 }
4278
4279 int[] oriMap = {
4280 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4281 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4282 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4283 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4284 };
4285 // Since the map starts at portrait, we need to offset if this device's natural orientation
4286 // is landscape.
4287 int indexOffset = 0;
4288 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4289 indexOffset = 1;
4290 }
4291 return oriMap[(d.getRotation() + indexOffset) % 4];
4292 }
Adam Cohen446e9402011-09-15 18:21:21 -07004293
Winson Chung4b919f82012-05-01 10:44:08 -07004294 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004295 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004296 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4297 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4298 .getConfiguration().orientation));
4299 } else {
4300 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4301 }
Winson Chung4b919f82012-05-01 10:44:08 -07004302 }
4303 }
4304 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004305 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004306 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004307 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004308 } else {
4309 mHandler.postDelayed(new Runnable() {
4310 public void run() {
4311 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4312 }
4313 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004314 }
Winson Chung4b919f82012-05-01 10:44:08 -07004315 }
Winson Chung400438b2011-01-16 17:53:48 -08004316 }
4317
Winson Chunge43a1e72014-01-15 10:33:02 -08004318 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004319 if (mLauncherCallbacks != null) {
4320 return mLauncherCallbacks.isLauncherPreinstalled();
4321 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004322 PackageManager pm = getPackageManager();
4323 try {
4324 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4325 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4326 return true;
4327 } else {
4328 return false;
4329 }
4330 } catch (NameNotFoundException e) {
4331 e.printStackTrace();
4332 return false;
4333 }
4334 }
4335
Adam Cohenea90f832014-05-21 15:03:34 -07004336 /**
4337 * This method indicates whether or not we should suggest default wallpaper dimensions
4338 * when our wallpaper cropper was not yet used to set a wallpaper.
4339 */
4340 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004341 if (mLauncherCallbacks != null) {
4342 return mLauncherCallbacks.overrideWallpaperDimensions();
4343 }
Adam Cohenea90f832014-05-21 15:03:34 -07004344 return true;
4345 }
4346
Adam Cohen432609a2014-03-13 17:03:22 -07004347 /**
4348 * To be overridden by subclasses to indicate that there is an activity to launch
4349 * before showing the standard launcher experience.
4350 */
4351 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004352 if (mLauncherCallbacks != null) {
4353 return mLauncherCallbacks.hasFirstRunActivity();
4354 }
Adam Cohen432609a2014-03-13 17:03:22 -07004355 return false;
4356 }
4357
4358 /**
4359 * To be overridden by subclasses to launch any first run activity
4360 */
4361 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004362 if (mLauncherCallbacks != null) {
4363 return mLauncherCallbacks.getFirstRunActivity();
4364 }
Adam Cohen432609a2014-03-13 17:03:22 -07004365 return null;
4366 }
4367
Winson Chunga6945242014-01-08 14:04:34 -08004368 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004369 return !ActivityManager.isRunningInTestHarness() &&
4370 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004371 }
4372
Adam Cohen4a9423d2014-03-28 14:22:31 -07004373 protected boolean hasRunFirstRunActivity() {
4374 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4375 }
4376
Adam Cohen432609a2014-03-13 17:03:22 -07004377 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004378 if (shouldRunFirstRunActivity() &&
4379 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004380 Intent firstRunIntent = getFirstRunActivity();
4381 if (firstRunIntent != null) {
4382 startActivity(firstRunIntent);
4383 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004384 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004385 }
4386 }
Adam Cohen432609a2014-03-13 17:03:22 -07004387 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004388 }
4389
4390 private void markFirstRunActivityShown() {
4391 SharedPreferences.Editor editor = mSharedPrefs.edit();
4392 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4393 editor.apply();
4394 }
4395
Adam Cohen432609a2014-03-13 17:03:22 -07004396 /**
4397 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4398 * screen that must be displayed and dismissed.
4399 */
4400 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004401 if (mLauncherCallbacks != null) {
4402 return mLauncherCallbacks.hasDismissableIntroScreen();
4403 }
Adam Cohen432609a2014-03-13 17:03:22 -07004404 return false;
4405 }
4406
4407 /**
4408 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4409 */
4410 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004411 if (mLauncherCallbacks != null) {
4412 return mLauncherCallbacks.getIntroScreen();
4413 }
Adam Cohen432609a2014-03-13 17:03:22 -07004414 return null;
4415 }
4416
4417 /**
4418 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4419 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4420 */
4421 private boolean shouldShowIntroScreen() {
4422 return hasDismissableIntroScreen() &&
4423 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4424 }
4425
4426 protected void showIntroScreen() {
4427 View introScreen = getIntroScreen();
4428 changeWallpaperVisiblity(false);
4429 if (introScreen != null) {
4430 mDragLayer.showOverlayView(introScreen);
4431 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004432 if (mLauncherOverlayContainer != null) {
4433 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4434 }
Adam Cohen432609a2014-03-13 17:03:22 -07004435 }
4436
4437 public void dismissIntroScreen() {
4438 markIntroScreenDismissed();
4439 if (showFirstRunActivity()) {
4440 // We delay hiding the intro view until the first run activity is showing. This
4441 // avoids a blip.
4442 mWorkspace.postDelayed(new Runnable() {
4443 @Override
4444 public void run() {
4445 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004446 if (mLauncherOverlayContainer != null) {
4447 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4448 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004449 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004450 }
4451 }, ACTIVITY_START_DELAY);
4452 } else {
4453 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004454 if (mLauncherOverlayContainer != null) {
4455 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4456 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004457 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004458 }
4459 changeWallpaperVisiblity(true);
4460 }
4461
4462 private void markIntroScreenDismissed() {
4463 SharedPreferences.Editor editor = mSharedPrefs.edit();
4464 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4465 editor.apply();
4466 }
4467
Adam Cohen091440a2015-03-18 14:16:05 -07004468 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004469 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4470 // on the device, then we always show the first run cling experience (or if there is no
4471 // launcher2). Otherwise, we prompt the user upon started for migration
4472 LauncherClings launcherClings = new LauncherClings(this);
4473 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4474 if (mModel.canMigrateFromOldLauncherDb(this)) {
4475 launcherClings.showMigrationCling();
4476 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004477 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004478 }
4479 }
4480 }
4481
Winson Chunga6945242014-01-08 14:04:34 -08004482 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004483 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4484 if (mHotseat != null) mHotseat.setAlpha(1f);
4485 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4486 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004487 }
4488
4489 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004490 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4491 if (mHotseat != null) mHotseat.setAlpha(0f);
4492 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4493 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004494 }
4495
Mathew Inwood72fbec12013-11-19 15:45:07 +00004496 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004497 // Called from search suggestion, not supported in other profiles.
4498 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4499 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4500 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4501 myUser);
4502 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004503 return null;
4504 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004505 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004506 }
4507
4508 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4509 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004510 // Called from search suggestion, not supported in other profiles.
4511 return createShortcutDragInfo(shortcutIntent, caption, icon,
4512 UserHandleCompat.myUserHandle());
4513 }
4514
4515 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4516 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004517 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004518 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004519 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004520 }
4521
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004522 protected void moveWorkspaceToDefaultScreen() {
4523 mWorkspace.moveToDefaultScreen(false);
4524 }
4525
Mathew Inwood72fbec12013-11-19 15:45:07 +00004526 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4527 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004528 mWorkspace.onExternalDragStartedWithItem(dragView);
4529 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004530 }
4531
Anjali Koppalf5d29132014-02-28 13:33:27 -08004532 @Override
4533 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004534 if (mLauncherCallbacks != null) {
4535 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4536 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004537 }
4538
Winson Chung80baf5a2010-08-09 16:03:15 -07004539 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004540 * Prints out out state for debugging.
4541 */
4542 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004543 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004544 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004545 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4546 Log.d(TAG, "mRestoring=" + mRestoring);
4547 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4548 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004549 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004550 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004551
Winson Chung785d2eb2011-04-14 16:08:02 -07004552 if (mAppsCustomizeContent != null) {
4553 mAppsCustomizeContent.dumpState();
4554 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004555 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004556 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004557
4558 @Override
4559 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4560 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004561 synchronized (sDumpLogs) {
4562 writer.println(" ");
4563 writer.println("Debug logs: ");
4564 for (int i = 0; i < sDumpLogs.size(); i++) {
4565 writer.println(" " + sDumpLogs.get(i));
4566 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004567 }
Adam Cohen9211d422014-10-07 18:14:53 -07004568 if (mLauncherCallbacks != null) {
4569 mLauncherCallbacks.dump(prefix, fd, writer, args);
4570 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004571 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004572
4573 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004574 if (DEBUG_DUMP_LOG) {
4575 synchronized (sDumpLogs) {
4576 Log.d(TAG, "");
4577 Log.d(TAG, "*********************");
4578 Log.d(TAG, "Launcher debug logs: ");
4579 for (int i = 0; i < sDumpLogs.size(); i++) {
4580 Log.d(TAG, " " + sDumpLogs.get(i));
4581 }
4582 Log.d(TAG, "*********************");
4583 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004584 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004585 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004586 }
4587
4588 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004589 addDumpLog(tag, log, null, debugLog);
4590 }
4591
4592 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004593 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004594 if (e != null) {
4595 Log.d(tag, log, e);
4596 } else {
4597 Log.d(tag, log);
4598 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004599 }
Winson Chungede41292013-09-19 16:27:36 -07004600 if (DEBUG_DUMP_LOG) {
4601 sDateStamp.setTime(System.currentTimeMillis());
4602 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004603 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4604 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004605 }
Winson Chungede41292013-09-19 16:27:36 -07004606 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 }
4608
Adam Cohen59400422014-03-05 18:07:04 -08004609 public static CustomAppWidget getCustomAppWidget(String name) {
4610 return sCustomAppWidgets.get(name);
4611 }
4612
4613 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4614 return sCustomAppWidgets;
4615 }
4616
Winson Chungede41292013-09-19 16:27:36 -07004617 public void dumpLogsToLocalData() {
4618 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004619 new AsyncTask<Void, Void, Void>() {
4620 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004621 boolean success = false;
4622 sDateStamp.setTime(sRunStart);
4623 String FILENAME = sDateStamp.getMonth() + "-"
4624 + sDateStamp.getDay() + "_"
4625 + sDateStamp.getHours() + "-"
4626 + sDateStamp.getMinutes() + "_"
4627 + sDateStamp.getSeconds() + ".txt";
4628
4629 FileOutputStream fos = null;
4630 File outFile = null;
4631 try {
4632 outFile = new File(getFilesDir(), FILENAME);
4633 outFile.createNewFile();
4634 fos = new FileOutputStream(outFile);
4635 } catch (Exception e) {
4636 e.printStackTrace();
4637 }
4638 if (fos != null) {
4639 PrintWriter writer = new PrintWriter(fos);
4640
4641 writer.println(" ");
4642 writer.println("Debug logs: ");
4643 synchronized (sDumpLogs) {
4644 for (int i = 0; i < sDumpLogs.size(); i++) {
4645 writer.println(" " + sDumpLogs.get(i));
4646 }
4647 }
4648 writer.close();
4649 }
4650 try {
4651 if (fos != null) {
4652 fos.close();
4653 success = true;
4654 }
4655 } catch (IOException e) {
4656 e.printStackTrace();
4657 }
Michael Jurka43467462013-11-14 12:03:58 +01004658 return null;
Winson Chungede41292013-09-19 16:27:36 -07004659 }
Michael Jurka43467462013-11-14 12:03:58 +01004660 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004661 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004663}
Michael Jurka2763be32011-02-24 11:19:57 -08004664
Michael Jurkaabded662011-03-04 12:06:57 -08004665interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004666 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004667 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004668 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004669 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004670 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004671}
Dan Sandlerd5024042014-01-09 15:01:33 -05004672
4673interface DebugIntents {
4674 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4675 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004676}