blob: 58b085480d5b2caaa112035b23398c934b803a8a [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080098import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070099import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700100
Sunny Goyal651077b2014-06-30 14:15:31 -0700101import com.android.launcher3.DropTarget.DragObject;
102import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700106import com.android.launcher3.compat.PackageInstallerCompat;
107import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100108import com.android.launcher3.compat.UserHandleCompat;
109import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700110
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.DataInputStream;
112import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800119import java.lang.reflect.Field;
120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800170 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: boolean
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
190 // Type: long
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: int[]
201 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Adam Cohen432609a2014-03-13 17:03:22 -0700203 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
205
Adam Cohen3ed316a2014-07-23 18:21:20 -0700206 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
207 static final String ACTION_FIRST_LOAD_COMPLETE =
208 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
209
Adam Cohen39a06042013-07-19 14:30:12 -0700210 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700211 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700212
Sunny Goyal594d76d2014-11-06 10:12:54 -0800213 private static final String QSB_WIDGET_ID = "qsb_widget_id";
214 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
215
Winson Chunga6945242014-01-08 14:04:34 -0800216 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
217
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700219 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700220 private State mState = State.WORKSPACE;
221 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000234 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
235 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
236
Winson Chunga2413752012-04-03 14:22:34 -0700237 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700238 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
239 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700240 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700241 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700242
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800243 private final BroadcastReceiver mCloseSystemDialogsReceiver
244 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800245 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private LayoutInflater mInflater;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700250 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800251 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800252 private DragLayer mDragLayer;
253 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700254 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Sunny Goyalffe83f12014-08-14 17:39:34 -0700256 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Michael Jurkac9d95c52011-08-29 14:03:34 -0700259 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800260 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800261 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700262
Michael Jurka0280c3b2010-09-17 15:00:07 -0700263 private int[] mTmpAddItemCellCoordinates = new int[2];
264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private FolderInfo mFolderInfo;
266
Winson Chung3d503fb2011-07-13 17:25:49 -0700267 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800268 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700269
Michael Jurka838a4ca2011-02-07 13:33:06 -0800270 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700271
Winson Chungc51db6a2011-10-05 11:44:49 -0700272 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700273 private AppsCustomizeTabHost mAppsCustomizeTabHost;
274 private AppsCustomizePagedView mAppsCustomizeContent;
275 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800276 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
280 // scroll issues (because the workspace may not have been measured yet) and extra work.
281 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
282 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
284 private SpannableStringBuilder mDefaultKeySsb = null;
285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private boolean mWorkspaceLoading = true;
287
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800288 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private boolean mRestoring;
290 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700291 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
Michael Jurka1e2f4652013-07-08 18:03:46 -0700293 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700294 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private Bundle mSavedInstanceState;
297
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mUserPresent = true;
301 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700302 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305 private static LocaleConfiguration sLocaleConfiguration = null;
306
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700308
Adam Cohen61f560d2013-09-30 15:58:20 -0700309 private View.OnTouchListener mHapticFeedbackTouchListener;
310
Adam Cohen80e6beb2015-02-13 16:14:33 -0800311 public static final int BUILD_LAYER = 0;
312 public static final int BUILD_AND_SET_LAYER = 1;
313
Adam Cohended9f8d2010-11-03 13:25:16 -0700314 // Related to the auto-advancing of widgets
315 private final int ADVANCE_MSG = 1;
316 private final int mAdvanceInterval = 20000;
317 private final int mAdvanceStagger = 250;
318 private long mAutoAdvanceSentTime;
319 private long mAutoAdvanceTimeLeft = -1;
320 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
321 new HashMap<View, AppWidgetProviderInfo>();
322
Winson Chung400438b2011-01-16 17:53:48 -0800323 // Determines how long to wait after a rotation before restoring the screen orientation to
324 // match the sensor state.
325 private final int mRestoreScreenOrientationDelay = 500;
326
Craig Mautner360310b2012-10-26 15:13:08 -0700327 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 // Holds the page that we need to animate to, and the icon views that we need to animate up
344 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700345 private ImageView mFolderIconImageView;
346 private Bitmap mFolderIconBitmap;
347 private Canvas mFolderIconCanvas;
348 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349
Michael Jurkaddd62e92011-02-16 17:49:14 -0800350 private BubbleTextView mWaitingForResume;
351
Adam Cohen59400422014-03-05 18:07:04 -0800352 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
353 new HashMap<String, CustomAppWidget>();
354
355 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
356 static {
357 if (ENABLE_CUSTOM_WIDGET_TEST) {
358 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
359 }
360 }
361
Chet Haasea8f996d2015-02-17 12:56:04 -0800362 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
363 private static Method sClipRevealMethod = null;
364 static {
365 Class<?> activityOptionsClass = ActivityOptions.class;
366 try {
367 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
368 View.class, int.class, int.class, int.class, int.class);
369 } catch (Exception e) {
370 // Earlier version
371 }
372 }
373
Michael Jurkac1f5d262011-09-30 19:32:27 -0700374 private Runnable mBuildLayersRunnable = new Runnable() {
375 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700376 if (mWorkspace != null) {
377 mWorkspace.buildPageHardwareLayers();
378 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700379 }
380 };
381
Adam Cohendb364c32014-05-20 14:23:40 -0700382 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383
384 private static class PendingAddArguments {
385 int requestCode;
386 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700387 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700388 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 int cellX;
390 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700391 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 }
393
Daniel Sandlerff02d492013-08-05 02:12:05 -0400394 private Stats mStats;
395
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700396 FocusIndicatorView mFocusHandler;
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 @Override
399 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700400 if (DEBUG_STRICT_MODE) {
401 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
402 .detectDiskReads()
403 .detectDiskWrites()
404 .detectNetwork() // or .detectAll() for all detectable problems
405 .penaltyLog()
406 .build());
407 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
408 .detectLeakedSqlLiteObjects()
409 .detectLeakedClosableObjects()
410 .penaltyLog()
411 .penaltyDeath()
412 .build());
413 }
414
Adam Cohen9211d422014-10-07 18:14:53 -0700415 if (mLauncherCallbacks != null) {
416 mLauncherCallbacks.preOnCreate();
417 }
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400420
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400421 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400422 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700423 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700424
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700426 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700427
428 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400429 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700430 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700431 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mModel = app.setLauncher(this);
433 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700434 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400435 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Daniel Sandlerff02d492013-08-05 02:12:05 -0400438 mStats = new Stats(this);
439
Sunny Goyalffe83f12014-08-14 17:39:34 -0700440 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700441
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
443 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700445 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
446 // this also ensures that any synchronous binding below doesn't re-trigger another
447 // LauncherModel load.
448 mPaused = false;
449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200451 android.os.Debug.startMethodTracing(
452 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100459 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800461 registerContentObservers();
462
Joe Onorato7c312c12009-08-13 21:36:53 -0700463 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700464
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 mSavedState = savedInstanceState;
466 restoreState(mSavedState);
467
468 if (PROFILE_STARTUP) {
469 android.os.Debug.stopMethodTracing();
470 }
471
472 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700473 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 // If the user leaves launcher, then we should just load items asynchronously when
475 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500476 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 } else {
478 // We only load the page synchronously if the user rotates (or triggers a
479 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800480 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 }
483
484 // For handling default keys
485 mDefaultKeySsb = new SpannableStringBuilder();
486 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800487
488 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
489 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800490
Adam Cohenf9426d52012-06-04 17:26:21 -0700491 // On large interfaces, we want the screen to auto-rotate based on the current orientation
492 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700493
Adam Cohen9211d422014-10-07 18:14:53 -0700494 if (mLauncherCallbacks != null) {
495 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700496 if (mLauncherCallbacks.hasLauncherOverlay()) {
497 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700498 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
499 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
500 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700501 mWorkspace.setLauncherOverlay(mLauncherOverlay);
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700504
505 if (shouldShowIntroScreen()) {
506 showIntroScreen();
507 } else {
508 showFirstRunActivity();
509 showFirstRunClings();
510 }
Adam Cohen9211d422014-10-07 18:14:53 -0700511 }
512
513 private LauncherCallbacks mLauncherCallbacks;
514
515 public void onPostCreate(Bundle savedInstanceState) {
516 super.onPostCreate(savedInstanceState);
517 if (mLauncherCallbacks != null) {
518 mLauncherCallbacks.onPostCreate(savedInstanceState);
519 }
520 }
521
522 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
523 mLauncherCallbacks = callbacks;
524 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700525 }
526
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700527 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700528 public void onLauncherProviderChange() {
529 if (mLauncherCallbacks != null) {
530 mLauncherCallbacks.onLauncherProviderChange();
531 }
532 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700533
Adam Cohen9211d422014-10-07 18:14:53 -0700534 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700535 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700536 if (mLauncherCallbacks != null) {
537 return mLauncherCallbacks.hasCustomContentToLeft();
538 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700539 return false;
540 }
541
Dave Hawkeya8881582013-09-17 15:55:33 -0600542 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500543 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 * {@link #addToCustomContentPage}. This will only be invoked if
545 * {@link #hasCustomContentToLeft()} is {@code true}.
546 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500547 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700548 if (mLauncherCallbacks != null) {
549 mLauncherCallbacks.populateCustomContentContainer();
550 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 }
552
553 /**
554 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
555 * ensure the custom content page is added or removed if necessary.
556 */
557 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600558 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600559 // Not bound yet, wait for bindScreens to be called.
560 return;
561 }
562
563 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
564 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500565 mWorkspace.createCustomContentContainer();
566 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
568 mWorkspace.removeCustomContentPage();
569 }
570 }
571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700573 if (sLocaleConfiguration == null) {
574 new AsyncTask<Void, Void, LocaleConfiguration>() {
575 @Override
576 protected LocaleConfiguration doInBackground(Void... unused) {
577 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
578 readConfiguration(Launcher.this, localeConfiguration);
579 return localeConfiguration;
580 }
581
582 @Override
583 protected void onPostExecute(LocaleConfiguration result) {
584 sLocaleConfiguration = result;
585 checkForLocaleChange(); // recursive, but now with a locale configuration
586 }
587 }.execute();
588 return;
589 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700590
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 final Configuration configuration = getResources().getConfiguration();
592
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700593 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 final String locale = configuration.locale.toString();
595
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final int mcc = configuration.mcc;
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final int mnc = configuration.mnc;
601
Romain Guy84f296c2009-11-04 15:00:44 -0800602 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603
Romain Guy84f296c2009-11-04 15:00:44 -0800604 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 sLocaleConfiguration.locale = locale;
606 sLocaleConfiguration.mcc = mcc;
607 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700608
Joe Onorato0589f0f2010-02-08 13:44:00 -0800609 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610
611 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100612 new AsyncTask<Void, Void, Void>() {
613 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100615 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 }
Michael Jurka43467462013-11-14 12:03:58 +0100617 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700618 }
619 }
620
621 private static class LocaleConfiguration {
622 public String locale;
623 public int mcc = -1;
624 public int mnc = -1;
625 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700626
Romain Guy98d01652009-06-30 16:21:04 -0700627 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
628 DataInputStream in = null;
629 try {
Helena Josol28db2802014-10-09 17:04:09 +0100630 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700631 configuration.locale = in.readUTF();
632 configuration.mcc = in.readInt();
633 configuration.mnc = in.readInt();
634 } catch (FileNotFoundException e) {
635 // Ignore
636 } catch (IOException e) {
637 // Ignore
638 } finally {
639 if (in != null) {
640 try {
641 in.close();
642 } catch (IOException e) {
643 // Ignore
644 }
645 }
646 }
647 }
648
649 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
650 DataOutputStream out = null;
651 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100652 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100653 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700654 out.writeUTF(configuration.locale);
655 out.writeInt(configuration.mcc);
656 out.writeInt(configuration.mnc);
657 out.flush();
658 } catch (FileNotFoundException e) {
659 // Ignore
660 } catch (IOException e) {
661 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100662 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700663 } finally {
664 if (out != null) {
665 try {
666 out.close();
667 } catch (IOException e) {
668 // Ignore
669 }
670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672 }
673
Anton Hanssona2f665f2013-09-26 12:18:32 +0100674 public Stats getStats() {
675 return mStats;
676 }
677
Bjorn Bringertc459e522013-06-07 19:36:01 +0100678 public LayoutInflater getInflater() {
679 return mInflater;
680 }
681
Winson Chung36a62fe2012-05-06 18:04:42 -0700682 boolean isDraggingEnabled() {
683 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
684 // that is subsequently removed from the workspace in startBinding().
685 return !mModel.isLoadingWorkspace();
686 }
687
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800688 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000689 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100690 if (Build.VERSION.SDK_INT >= 17) {
691 return View.generateViewId();
692 } else {
693 // View.generateViewId() is not available. The following fallback logic is a copy
694 // of its implementation.
695 for (;;) {
696 final int result = sNextGeneratedId.get();
697 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
698 int newValue = result + 1;
699 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
700 if (sNextGeneratedId.compareAndSet(result, newValue)) {
701 return result;
702 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000703 }
704 }
705 }
706
707 public int getViewIdForItem(ItemInfo info) {
708 // This cast is safe given the > 2B range for int.
709 int itemId = (int) info.id;
710 if (mItemIdToViewId.containsKey(itemId)) {
711 return mItemIdToViewId.get(itemId);
712 }
713 int viewId = generateViewId();
714 mItemIdToViewId.put(itemId, viewId);
715 return viewId;
716 }
717
718 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700719 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
720 * a configuration step, this allows the proper animations to run after other transitions.
721 */
Adam Cohendb364c32014-05-20 14:23:40 -0700722 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700723 long screenId = args.screenId;
724 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
725 // When the screen id represents an actual screen (as opposed to a rank) we make sure
726 // that the drop page actually exists.
727 screenId = ensurePendingDropLayoutExists(args.screenId);
728 }
Adam Cohendb364c32014-05-20 14:23:40 -0700729
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700732 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700734 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700736 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700737 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700738 case REQUEST_RECONFIGURE_APPWIDGET:
739 completeRestoreAppWidget(args.appWidgetId);
740 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 }
Michael Jurka27614d22012-04-02 06:40:22 -0700742 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
743 // if you turned the screen off and then back while in All Apps, Launcher would not
744 // return to the workspace. Clearing mAddInfo.container here fixes this issue
745 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700746 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 }
748
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800749 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700751 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700752 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700753 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800754 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700755
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 Runnable exitSpringLoaded = new Runnable() {
757 @Override
758 public void run() {
759 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
760 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
761 }
762 };
763
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
767 if (resultCode == RESULT_CANCELED) {
768 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700769 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800772 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 }
775 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200776 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
777 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
778 mWorkspace.exitOverviewMode(false);
779 }
780 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200782
Adam Cohened66b2b2012-01-23 17:28:51 -0800783 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
784 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700785
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 // We have special handling for widgets
788 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800789 final int appWidgetId;
790 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
791 : -1;
792 if (widgetId < 0) {
793 appWidgetId = pendingAddWidgetId;
794 } else {
795 appWidgetId = widgetId;
796 }
797
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800799 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700800 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
801 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 result = RESULT_CANCELED;
803 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700804 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 @Override
806 public void run() {
807 exitSpringLoadedDragModeDelayed(false, 0, null);
808 }
809 };
Adam Cohendb364c32014-05-20 14:23:40 -0700810 if (workspaceLocked) {
811 // No need to remove the empty screen if we're mid-binding, as the
812 // the bind will not add the empty screen.
813 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
814 } else {
815 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
816 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
817 }
Winson Chung5aaab772012-04-27 15:24:02 -0700818 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700819 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700820 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
821 // When the screen id represents an actual screen (as opposed to a rank)
822 // we make sure that the drop page actually exists.
823 mPendingAddInfo.screenId =
824 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
825 }
Adam Cohendb364c32014-05-20 14:23:40 -0700826 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
827
828 dropLayout.setDropPending(true);
829 final Runnable onComplete = new Runnable() {
830 @Override
831 public void run() {
832 completeTwoStageWidgetDrop(resultCode, appWidgetId);
833 dropLayout.setDropPending(false);
834 }
835 };
836 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
838 } else {
839 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
840 mPendingAddInfo);
841 sPendingAddItem = args;
842 }
Winson Chung5aaab772012-04-27 15:24:02 -0700843 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 return;
845 }
846
Sunny Goyalff572272014-07-23 13:58:07 -0700847 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
848 if (resultCode == RESULT_OK) {
849 // Update the widget view.
850 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
851 pendingAddWidgetId, mPendingAddInfo);
852 if (workspaceLocked) {
853 sPendingAddItem = args;
854 } else {
855 completeAdd(args);
856 }
857 }
858 // Leave the widget in the pending state if the user canceled the configure.
859 return;
860 }
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 // The pattern used here is that a user PICKs a specific application,
863 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
866 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700867 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700868 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
869 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800870 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700871 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800872 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700873 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700874 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
875 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
Adam Cohen00074722013-10-11 17:46:09 -0700877 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700878 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700879 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800882
883 }
884
885 @Override
886 protected void onActivityResult(
887 final int requestCode, final int resultCode, final Intent data) {
888 handleActivityResult(requestCode, resultCode, data);
889 if (mLauncherCallbacks != null) {
890 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
891 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
893
Adam Cohendb364c32014-05-20 14:23:40 -0700894 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
895 appWidgetId, ItemInfo info) {
896 PendingAddArguments args = new PendingAddArguments();
897 args.requestCode = requestCode;
898 args.intent = data;
899 args.container = info.container;
900 args.screenId = info.screenId;
901 args.cellX = info.cellX;
902 args.cellY = info.cellY;
903 args.appWidgetId = appWidgetId;
904 return args;
905 }
906
907 /**
908 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
909 *
910 * @param screenId the screen id to check
911 * @return the new screen, or screenId if it exists
912 */
913 private long ensurePendingDropLayoutExists(long screenId) {
914 CellLayout dropLayout =
915 (CellLayout) mWorkspace.getScreenWithId(screenId);
916 if (dropLayout == null) {
917 // it's possible that the add screen was removed because it was
918 // empty and a re-bind occurred
919 mWorkspace.addExtraEmptyScreen();
920 return mWorkspace.commitExtraEmptyScreen();
921 } else {
922 return screenId;
923 }
924 }
925
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700927 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700928 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 Runnable onCompleteRunnable = null;
930 int animationType = 0;
931
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 if (resultCode == RESULT_OK) {
934 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
935 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 mPendingAddWidgetInfo);
937 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 onCompleteRunnable = new Runnable() {
939 @Override
940 public void run() {
941 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700943 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
944 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 }
946 };
947 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800948 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700951 if (mDragLayer.getAnimatedView() != null) {
952 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
953 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
954 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700955 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700956 // The animated view may be null in the case of a rotation during widget configuration
957 onCompleteRunnable.run();
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960
961 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 protected void onStop() {
963 super.onStop();
964 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700965
966 if (mLauncherCallbacks != null) {
967 mLauncherCallbacks.onStop();
968 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700969 }
970
971 @Override
972 protected void onStart() {
973 super.onStart();
974 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700975
976 if (mLauncherCallbacks != null) {
977 mLauncherCallbacks.onStart();
978 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700979 }
980
981 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200983 long startTime = 0;
984 if (DEBUG_RESUME_TIME) {
985 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400986 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200987 }
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.preOnResume();
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700994
Winson Chung4a2afa32012-07-19 14:53:05 -0700995 // Restore the previous launcher state
996 if (mOnResumeState == State.WORKSPACE) {
997 showWorkspace(false);
998 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800999 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 }
1001 mOnResumeState = State.NONE;
1002
Craig Mautner360310b2012-10-26 15:13:08 -07001003 // Background was set to gradient in onPause(), restore to black if in all apps.
1004 setWorkspaceBackground(mState == State.WORKSPACE);
1005
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001006 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001007 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001008 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001009 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001011 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001014 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1015 // execute them here
1016 long startTimeCallbacks = 0;
1017 if (DEBUG_RESUME_TIME) {
1018 startTimeCallbacks = System.currentTimeMillis();
1019 }
1020
1021 if (mAppsCustomizeContent != null) {
1022 mAppsCustomizeContent.setBulkBind(true);
1023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1025 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001026 }
1027 if (mAppsCustomizeContent != null) {
1028 mAppsCustomizeContent.setBulkBind(false);
1029 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001030 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001031 if (DEBUG_RESUME_TIME) {
1032 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1033 (System.currentTimeMillis() - startTimeCallbacks));
1034 }
Michael Jurka447bf842013-05-15 14:52:15 +02001035 }
Michael Jurka54554252013-08-01 12:52:23 +02001036 if (mOnResumeCallbacks.size() > 0) {
1037 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1038 mOnResumeCallbacks.get(i).run();
1039 }
1040 mOnResumeCallbacks.clear();
1041 }
Winson Chunge4e50662012-01-23 14:45:13 -08001042
1043 // Reset the pressed state of icons that were locked in the press state while activities
1044 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001045 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001046 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001047 mWaitingForResume.setStayPressed(false);
1048 }
Winson Chung82963d52013-10-09 11:20:57 -07001049
Adam Cohen06dff352012-06-01 17:17:08 -07001050 // It is possible that widgets can receive updates while launcher is not in the foreground.
1051 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1052 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1053 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001054 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001055
Winson Chung780fe592013-09-26 14:48:44 -07001056 // Process any items that were added while Launcher was away.
1057 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1058
Michael Jurka447bf842013-05-15 14:52:15 +02001059 if (DEBUG_RESUME_TIME) {
1060 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1061 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001062
1063 if (mWorkspace.getCustomContentCallbacks() != null) {
1064 // If we are resuming and the custom content is the current page, we call onShow().
1065 // It is also poassible that onShow will instead be called slightly after first layout
1066 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1067 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001068 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001069 }
1070 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001071 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001072 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001073
1074 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001075
1076 if (mLauncherCallbacks != null) {
1077 mLauncherCallbacks.onResume();
1078 }
Adam Cohen06dff352012-06-01 17:17:08 -07001079 }
1080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001083 // Ensure that items added to Launcher are queued until Launcher returns
1084 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001085 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001086
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001088 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001089 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001090 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001091
1092 // We call onHide() aggressively. The custom content callbacks should be able to
1093 // debounce excess onHide calls.
1094 if (mWorkspace.getCustomContentCallbacks() != null) {
1095 mWorkspace.getCustomContentCallbacks().onHide();
1096 }
Romain Guycbb89e42009-06-08 15:52:54 -07001097
Adam Cohen9211d422014-10-07 18:14:53 -07001098 if (mLauncherCallbacks != null) {
1099 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001100 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001101 }
1102
1103 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001104 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1105 // by a onResume or by scrolling otherwise.
1106 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001107
1108 // Custom content is completely hidden
1109 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001110
1111 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1112 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001113
1114 // Indicates whether the user is allowed to scroll away from the custom content.
1115 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001116 }
1117
Adam Cohenc2d6e892014-10-16 09:49:24 -07001118 public interface LauncherOverlay {
1119
1120 /**
1121 * Touch interaction leading to overscroll has begun
1122 */
1123 public void onScrollInteractionBegin();
1124
1125 /**
1126 * Touch interaction related to overscroll has ended
1127 */
1128 public void onScrollInteractionEnd();
1129
1130 /**
1131 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1132 * screen (or in the case of RTL, the rightmost screen).
1133 */
1134 public void onScrollChange(int progress, boolean rtl);
1135
1136 /**
1137 * Screen has stopped scrolling
1138 */
1139 public void onScrollSettled();
1140
1141 /**
1142 * This method can be called by the Launcher in order to force the LauncherOverlay
1143 * to exit fully immersive mode.
1144 */
1145 public void forceExitFullImmersion();
1146 }
1147
1148 public interface LauncherOverlayCallbacks {
1149 /**
1150 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1151 * however it doesn't modify any state within the launcher.
1152 */
1153 public boolean canEnterFullImmersion();
1154
1155 /**
1156 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1157 * eg. by occupying the full screen and handling all touch events.
1158 *
1159 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1160 * case, Launcher will modify any necessary state and assumes the overlay is
1161 * handling all interaction. If false, the LauncherOverlay should cancel any
1162 *
1163 */
1164 public boolean enterFullImmersion();
1165
1166 /**
1167 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1168 * full control over UI and state.
1169 */
1170 public void exitFullImmersion();
1171 }
1172
1173 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1174
1175 @Override
1176 public boolean canEnterFullImmersion() {
1177 return mState == State.WORKSPACE;
1178 }
1179
1180 @Override
1181 public boolean enterFullImmersion() {
1182 if (mState == State.WORKSPACE) {
1183 // When fully immersed, disregard any touches which fall through.
1184 mDragLayer.setBlockTouch(true);
1185 return true;
1186 }
1187 return false;
1188 }
1189
1190 @Override
1191 public void exitFullImmersion() {
1192 mDragLayer.setBlockTouch(false);
1193 }
1194 }
1195
Jorim Jaggid017f882014-01-14 17:08:48 -08001196 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001197 if (mLauncherCallbacks != null) {
1198 return mLauncherCallbacks.hasSettings();
1199 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001200 return false;
1201 }
1202
Adam Cohenbffe7452013-07-22 18:21:45 -07001203
Adam Cohen9211d422014-10-07 18:14:53 -07001204 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001205 CustomContentCallbacks callbacks, String description) {
1206 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001207 }
1208
Adam Cohenedb40762013-07-18 16:45:45 -07001209 // The custom content needs to offset its content to account for the QSB
1210 public int getTopOffsetForCustomContent() {
1211 return mWorkspace.getPaddingTop();
1212 }
1213
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001214 @Override
1215 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001216 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001217 if (mModel.isCurrentCallbacks(this)) {
1218 mModel.stopLoader();
1219 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 if (mAppsCustomizeContent != null) {
1221 mAppsCustomizeContent.surrender();
1222 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001223 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001224 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001225
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001227 @Override
1228 public void onWindowFocusChanged(boolean hasFocus) {
1229 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001230 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001231
1232 if (mLauncherCallbacks != null) {
1233 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1234 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001235 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 private boolean acceptFilter() {
1238 final InputMethodManager inputManager = (InputMethodManager)
1239 getSystemService(Context.INPUT_METHOD_SERVICE);
1240 return !inputManager.isFullscreenMode();
1241 }
1242
1243 @Override
1244 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001245 final int uniChar = event.getUnicodeChar();
1246 final boolean handled = super.onKeyDown(keyCode, event);
1247 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1248 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1250 keyCode, event);
1251 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001252 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001253 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 // showSearchDialog()
1255 // If there are multiple keystrokes before the search dialog takes focus,
1256 // onSearchRequested() will be called for every keystroke,
1257 // but it is idempotent, so it's fine.
1258 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260 }
1261
Joe Onorato8a9625e2010-01-28 15:55:35 -08001262 // Eat the long press event so the keyboard doesn't come up.
1263 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1264 return true;
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 return handled;
1268 }
1269
Karl Rosaen138a0412009-04-23 19:00:21 -07001270 private String getTypedText() {
1271 return mDefaultKeySsb.toString();
1272 }
1273
1274 private void clearTypedText() {
1275 mDefaultKeySsb.clear();
1276 mDefaultKeySsb.clearSpans();
1277 Selection.setSelection(mDefaultKeySsb, 0);
1278 }
1279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001281 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1282 * State
1283 */
1284 private static State intToState(int stateOrdinal) {
1285 State state = State.WORKSPACE;
1286 final State[] stateValues = State.values();
1287 for (int i = 0; i < stateValues.length; i++) {
1288 if (stateValues[i].ordinal() == stateOrdinal) {
1289 state = stateValues[i];
1290 break;
1291 }
1292 }
1293 return state;
1294 }
1295
1296 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 * Restores the previous state, if it exists.
1298 *
1299 * @param savedState The previous state.
1300 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001301 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 private void restoreState(Bundle savedState) {
1303 if (savedState == null) {
1304 return;
1305 }
1306
Michael Jurka883f55b2010-10-21 15:47:14 -07001307 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001308 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001309 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001310 }
1311
Adam Cohen21cd0022013-10-09 18:57:02 -07001312 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1313 PagedView.INVALID_RESTORE_PAGE);
1314 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001315 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
Winson Chung3d503fb2011-07-13 17:25:49 -07001318 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001319 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320
Winson Chung3d503fb2011-07-13 17:25:49 -07001321 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1322 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001323 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001324 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1325 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001326 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1327 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1328 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001329 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001330 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 mRestoring = true;
1332 }
1333
1334 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1335 if (renameFolder) {
1336 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001337 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 mRestoring = true;
1339 }
Winson Chunga12a2502010-12-20 14:41:35 -08001340
Winson Chung785d2eb2011-04-14 16:08:02 -07001341 // Restore the AppsCustomize tab
1342 if (mAppsCustomizeTabHost != null) {
1343 String curTab = savedState.getString("apps_customize_currentTab");
1344 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001345 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001347 mAppsCustomizeContent.loadAssociatedPages(
1348 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001349 }
1350
Winson Chung5afbf7b2011-07-25 11:53:08 -07001351 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1352 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001353 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001354 mItemIdToViewId = (HashMap<Integer, Integer>)
1355 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
1357
1358 /**
1359 * Finds all the views we need and configure them properly.
1360 */
1361 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001362 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001363
Craig Mautner360310b2012-10-26 15:13:08 -07001364 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001365 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001366 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1367 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001368 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001369 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001370
John Spurlock77e1f472013-09-11 10:09:51 -04001371 mLauncherView.setSystemUiVisibility(
1372 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001373 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374
Winson Chung4c98d922011-05-31 16:50:48 -07001375 // Setup the drag layer
1376 mDragLayer.setup(this, dragController);
1377
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 // Setup the hotseat
1379 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1380 if (mHotseat != null) {
1381 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001382 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001383 }
1384
Jorim Jaggid017f882014-01-14 17:08:48 -08001385 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001386 View widgetButton = findViewById(R.id.widget_button);
1387 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 @Override
1389 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001390 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001391 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001392 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 }
1394 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001395 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1396
1397 View wallpaperButton = findViewById(R.id.wallpaper_button);
1398 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001399 @Override
1400 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001401 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001402 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001403 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001404 }
1405 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001406 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1407
1408 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 if (hasSettings()) {
1410 settingsButton.setOnClickListener(new OnClickListener() {
1411 @Override
1412 public void onClick(View arg0) {
1413 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001414 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001415 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001416 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001417 });
1418 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1419 } else {
1420 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001421 }
1422
Adam Cohendbdff6b2013-09-18 19:09:15 -07001423 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424
Winson Chung4c98d922011-05-31 16:50:48 -07001425 // Setup the workspace
1426 mWorkspace.setHapticFeedbackEnabled(false);
1427 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001428 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001429 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001430
Winson Chungf0ea4d32011-06-06 14:27:16 -07001431 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001432 mSearchDropTargetBar = (SearchDropTargetBar)
1433 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001434
Winson Chungf0ea4d32011-06-06 14:27:16 -07001435 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001436 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001437 mAppsCustomizeContent = (AppsCustomizePagedView)
1438 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1439 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001440
Winson Chung3d503fb2011-07-13 17:25:49 -07001441 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001442 dragController.setDragScoller(mWorkspace);
1443 dragController.setScrollView(mDragLayer);
1444 dragController.setMoveTarget(mWorkspace);
1445 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001446 if (mSearchDropTargetBar != null) {
1447 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001448 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001449 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001450
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001451 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001452 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001453 mWeightWatcher = new WeightWatcher(this);
1454 mWeightWatcher.setAlpha(0.5f);
1455 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001456 new FrameLayout.LayoutParams(
1457 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001458 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 Gravity.BOTTOM)
1460 );
Adam Cohen39a06042013-07-19 14:30:12 -07001461
1462 boolean show = shouldShowWeightWatcher();
1463 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466
1467 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 * Sets the all apps button. This method is called from {@link Hotseat}.
1469 */
1470 public void setAllAppsButton(View allAppsButton) {
1471 mAllAppsButton = allAppsButton;
1472 }
1473
1474 public View getAllAppsButton() {
1475 return mAllAppsButton;
1476 }
1477
1478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Creates a view representing a shortcut.
1480 *
1481 * @param info The data structure describing the shortcut.
1482 *
1483 * @return A View inflated from R.layout.application.
1484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001487 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
1493 * @param layoutResId The id of the XML layout used to create the shortcut.
1494 * @param parent The group the shortcut belongs to.
1495 * @param info The data structure describing the shortcut.
1496 *
1497 * @return A View inflated from layoutResId.
1498 */
Adam Cohen59400422014-03-05 18:07:04 -08001499 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001500 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001501 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
1511 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
1516 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520
Sunny Goyal2350bc92014-10-14 16:42:54 -07001521 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001522 if (info == null) {
1523 return;
1524 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 final View view = createShortcut(info);
1526
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 // First we check if we already know the exact location where we want to add this item.
1528 if (cellX >= 0 && cellY >= 0) {
1529 cellXY[0] = cellX;
1530 cellXY[1] = cellY;
1531 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001532
1533 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 true, null,null)) {
1536 return;
1537 }
1538 DragObject dragObject = new DragObject();
1539 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1541 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 return;
1543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 foundCellSpan = (result != null);
1548 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 }
1551
1552 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001553 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 return;
1555 }
1556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
1564
Adam Cohen2f093b62012-04-30 18:59:53 -07001565 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1566 int minHeight) {
1567 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001568 // We want to account for the extra amount of padding that we are adding to the widget
1569 // to ensure that it gets the full amount of space that it has requested
1570 int requiredWidth = minWidth + padding.left + padding.right;
1571 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001572 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1576 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001577 }
1578
Adam Cohen2f093b62012-04-30 18:59:53 -07001579 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1580 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001581 }
1582
Adam Cohen2f093b62012-04-30 18:59:53 -07001583 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1584 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001585 }
1586
Adam Cohen2f093b62012-04-30 18:59:53 -07001587 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1588 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001589 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001593 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001595 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 */
Adam Cohen59400422014-03-05 18:07:04 -08001597 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1598 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1599
1600 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001602 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1603 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Adam Cohen59400422014-03-05 18:07:04 -08001606 if (appWidgetInfo.isCustomWidget) {
1607 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001608 }
1609
Adam Cohen59400422014-03-05 18:07:04 -08001610 LauncherAppWidgetInfo launcherInfo;
1611 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1612 launcherInfo.spanX = info.spanX;
1613 launcherInfo.spanY = info.spanY;
1614 launcherInfo.minSpanX = info.minSpanX;
1615 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001616 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001619 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620
1621 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 if (hostView == null) {
1623 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001624 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1625 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 } else {
1627 // The AppWidgetHostView has already been inflated and instantiated
1628 launcherInfo.hostView = hostView;
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001631 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001632 launcherInfo.notifyWidgetSizeChanged(this);
1633
Adam Cohen59400422014-03-05 18:07:04 -08001634 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1635 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001636
1637 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001639 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1643 @Override
1644 public void onReceive(Context context, Intent intent) {
1645 final String action = intent.getAction();
1646 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1647 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001648 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001650
Winson Chungc100e8e2011-08-09 16:02:43 -07001651 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001652 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001653 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001654 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1657 mUserPresent = true;
1658 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001659 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1660 mModel.resetLoadedState(false, true);
1661 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1662 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1663 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1664 mModel.resetLoadedState(false, true);
1665 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1666 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1667 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001668 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1669 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1670 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672 }
1673 };
1674
1675 @Override
1676 public void onAttachedToWindow() {
1677 super.onAttachedToWindow();
1678
1679 // Listen for broadcasts related to user-presence
1680 final IntentFilter filter = new IntentFilter();
1681 filter.addAction(Intent.ACTION_SCREEN_OFF);
1682 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001683 // For handling managed profiles
1684 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1685 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001686 if (ENABLE_DEBUG_INTENTS) {
1687 filter.addAction(DebugIntents.DELETE_DATABASE);
1688 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1689 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001691 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001692 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001693 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 mVisible = true;
1695 }
1696
Adam Cohen0b395352014-06-09 22:54:36 +00001697 /**
1698 * Sets up transparent navigation and status bars in LMP.
1699 * This method is a no-op for other platform versions.
1700 */
Sunny Goyald0091012015-01-20 15:55:34 -08001701 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001702 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001703 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001704 Window window = getWindow();
1705 window.getAttributes().systemUiVisibility |=
1706 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1707 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1708 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1709 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1710 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1711 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1712 window.setStatusBarColor(Color.TRANSPARENT);
1713 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001714 }
1715 }
1716
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 @Override
1718 public void onDetachedFromWindow() {
1719 super.onDetachedFromWindow();
1720 mVisible = false;
1721
Adam Cohend113e0c2010-11-11 10:48:05 -08001722 if (mAttached) {
1723 unregisterReceiver(mReceiver);
1724 mAttached = false;
1725 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 updateRunning();
1727 }
1728
1729 public void onWindowVisibilityChanged(int visibility) {
1730 mVisible = visibility == View.VISIBLE;
1731 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001732 // The following code used to be in onResume, but it turns out onResume is called when
1733 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1734 // is a more appropriate event to handle
1735 if (mVisible) {
1736 mAppsCustomizeTabHost.onWindowVisible();
1737 if (!mWorkspaceLoading) {
1738 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001739 // We want to let Launcher draw itself at least once before we force it to build
1740 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001741 // apps is nice and speedy.
1742 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 if (mStarted) return;
1746 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001747 // We delay the layer building a bit in order to give
1748 // other message processing a time to run. In particular
1749 // this avoids a delay in hiding the IME if it was
1750 // currently shown, because doing that may involve
1751 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001752 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001753 final ViewTreeObserver.OnDrawListener listener = this;
1754 mWorkspace.post(new Runnable() {
1755 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001756 if (mWorkspace != null &&
1757 mWorkspace.getViewTreeObserver() != null) {
1758 mWorkspace.getViewTreeObserver().
1759 removeOnDrawListener(listener);
1760 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 }
1762 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001763 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001764 }
1765 });
1766 }
1767 clearTypedText();
1768 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770
1771 private void sendAdvanceMessage(long delay) {
1772 mHandler.removeMessages(ADVANCE_MSG);
1773 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1774 mHandler.sendMessageDelayed(msg, delay);
1775 mAutoAdvanceSentTime = System.currentTimeMillis();
1776 }
1777
1778 private void updateRunning() {
1779 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1780 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1781 mAutoAdvanceRunning = autoAdvanceRunning;
1782 if (autoAdvanceRunning) {
1783 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1784 sendAdvanceMessage(delay);
1785 } else {
1786 if (!mWidgetsToAdvance.isEmpty()) {
1787 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1788 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1789 }
1790 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001791 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793 }
1794 }
1795
1796 private final Handler mHandler = new Handler() {
1797 @Override
1798 public void handleMessage(Message msg) {
1799 if (msg.what == ADVANCE_MSG) {
1800 int i = 0;
1801 for (View key: mWidgetsToAdvance.keySet()) {
1802 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1803 final int delay = mAdvanceStagger * i;
1804 if (v instanceof Advanceable) {
1805 postDelayed(new Runnable() {
1806 public void run() {
1807 ((Advanceable) v).advance();
1808 }
1809 }, delay);
1810 }
1811 i++;
1812 }
1813 sendAdvanceMessage(mAdvanceInterval);
1814 }
1815 }
1816 };
1817
1818 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001819 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1821 if (v instanceof Advanceable) {
1822 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001823 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001824 updateRunning();
1825 }
1826 }
1827
1828 void removeWidgetToAutoAdvance(View hostView) {
1829 if (mWidgetsToAdvance.containsKey(hostView)) {
1830 mWidgetsToAdvance.remove(hostView);
1831 updateRunning();
1832 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 }
1834
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 launcherInfo.hostView = null;
1838 }
1839
Winson Chung93eef082012-03-23 15:59:27 -07001840 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1841 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1842 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 }
1844
Adam Cohen59400422014-03-05 18:07:04 -08001845 public ArrayList<AppInfo> getAllAppsList() {
1846 return mAppsCustomizeContent.getApps();
1847 }
1848
Winson Chunga6945242014-01-08 14:04:34 -08001849 public DragLayer getDragLayer() {
1850 return mDragLayer;
1851 }
1852
1853 public Workspace getWorkspace() {
1854 return mWorkspace;
1855 }
1856
1857 public Hotseat getHotseat() {
1858 return mHotseat;
1859 }
1860
Jorim Jaggid017f882014-01-14 17:08:48 -08001861 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001862 return mOverviewPanel;
1863 }
1864
1865 public SearchDropTargetBar getSearchBar() {
1866 return mSearchDropTargetBar;
1867 }
1868
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001869 public LauncherAppWidgetHost getAppWidgetHost() {
1870 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Romain Guycbb89e42009-06-08 15:52:54 -07001872
Winson Chunga9abd0e2010-10-27 17:18:37 -07001873 public LauncherModel getModel() {
1874 return mModel;
1875 }
1876
Winson Chunga6945242014-01-08 14:04:34 -08001877 protected SharedPreferences getSharedPrefs() {
1878 return mSharedPrefs;
1879 }
1880
Bjorn Bringertc459e522013-06-07 19:36:01 +01001881 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001882 getWindow().closeAllPanels();
1883
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001884 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001885 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001890 long startTime = 0;
1891 if (DEBUG_RESUME_TIME) {
1892 startTime = System.currentTimeMillis();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 super.onNewIntent(intent);
1895
1896 // Close the menu
1897 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001898 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001899 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001900
Adam Cohened307df2013-10-02 09:37:31 -07001901 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1902 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1903 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001904
Adam Cohen6fecd412013-10-02 17:41:50 -07001905 if (mWorkspace == null) {
1906 // Can be cases where mWorkspace is null, this prevents a NPE
1907 return;
1908 }
1909 Folder openFolder = mWorkspace.getOpenFolder();
1910 // In all these cases, only animate if we're already on home
1911 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001912
1913 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1914 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001915 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001916 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001918 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 closeFolder();
1921 exitSpringLoadedDragMode();
1922
1923 // If we are already on home, then just animate back to the workspace,
1924 // otherwise, just wait until onResume to set the state back to Workspace
1925 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001926 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 } else {
1928 mOnResumeState = State.WORKSPACE;
1929 }
1930
1931 final View v = getWindow().peekDecorView();
1932 if (v != null && v.getWindowToken() != null) {
1933 InputMethodManager imm = (InputMethodManager)getSystemService(
1934 INPUT_METHOD_SERVICE);
1935 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1936 }
1937
1938 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001939 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001940 mAppsCustomizeTabHost.reset();
1941 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001942
Adam Cohen9211d422014-10-07 18:14:53 -07001943 if (mLauncherCallbacks != null) {
1944 mLauncherCallbacks.onHomeIntent();
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
Adam Cohened307df2013-10-02 09:37:31 -07001947
Michael Jurka447bf842013-05-15 14:52:15 +02001948 if (DEBUG_RESUME_TIME) {
1949 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onNewIntent(intent);
1954 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001955 }
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001958 public void onRestoreInstanceState(Bundle state) {
1959 super.onRestoreInstanceState(state);
1960 for (int page: mSynchronouslyBoundPages) {
1961 mWorkspace.restoreInstanceStateForChild(page);
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
1965 @Override
1966 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001967 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001968 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1969 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001970 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001971 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
Michael Jurka883f55b2010-10-21 15:47:14 -07001973 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001974 // We close any open folder since it will not be re-opened, and we need to make sure
1975 // this state is reflected.
1976 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 mWaitingForResult) {
1980 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001982 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1983 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001984 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1985 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1986 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001987 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
1990 if (mFolderInfo != null && mWaitingForResult) {
1991 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1992 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1993 }
Michael Jurka946ad472010-07-09 18:05:18 -07001994
Winson Chung785d2eb2011-04-14 16:08:02 -07001995 // Save the current AppsCustomize tab
1996 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001997 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1998 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001999 if (currentTabTag != null) {
2000 outState.putString("apps_customize_currentTab", currentTabTag);
2001 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002002 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2003 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002004 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002005 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002006
2007 if (mLauncherCallbacks != null) {
2008 mLauncherCallbacks.onSaveInstanceState(outState);
2009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
2011
2012 @Override
2013 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Winson Chunge7a03942011-08-05 15:05:12 -07002016 // Remove all pending runnables
2017 mHandler.removeMessages(ADVANCE_MSG);
2018 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002019 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002020
Winson Chungcd2b0142011-06-08 16:02:26 -07002021 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002022 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002023
2024 // It's possible to receive onDestroy after a new Launcher activity has
2025 // been created. In this case, don't interfere with the new Launcher.
2026 if (mModel.isCurrentCallbacks(this)) {
2027 mModel.stopLoader();
2028 app.setLauncher(null);
2029 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002032 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002034 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002036 mAppWidgetHost = null;
2037
2038 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039
2040 TextKeyListener.getInstance().release();
2041
Winson Chung81b52252012-08-27 15:34:29 -07002042 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2043 // to prevent leaking Launcher activities on orientation change.
2044 if (mModel != null) {
2045 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2046 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002047
2048 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002049 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002050
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002051 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002052 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002053 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054 mWorkspace = null;
2055 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002056
Michael Jurka2ecf9952012-06-18 12:52:28 -07002057 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002058
2059 if (mLauncherCallbacks != null) {
2060 mLauncherCallbacks.onDestroy();
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 public DragController getDragController() {
2065 return mDragController;
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
2069 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002070 if (requestCode >= 0) {
2071 setWaitingForResult(true);
2072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 super.startActivityForResult(intent, requestCode);
2074 }
2075
Winson Chung70d72102011-08-12 11:24:35 -07002076 /**
2077 * Indicates that we want global search for this activity by setting the globalSearch
2078 * argument for {@link #startSearch} to true.
2079 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002081 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002083
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002084 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002085
Karl Rosaen138a0412009-04-23 19:00:21 -07002086 if (initialQuery == null) {
2087 // Use any text typed in the launcher as the initial query
2088 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 if (appSearchData == null) {
2091 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002092 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
Winson Chungadf0c182012-08-23 14:59:07 -07002094 Rect sourceBounds = new Rect();
2095 if (mSearchDropTargetBar != null) {
2096 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2097 }
Romain Guycbb89e42009-06-08 15:52:54 -07002098
Ian Parkinson047036e2014-09-01 15:40:53 +01002099 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002101 if (clearTextImmediately) {
2102 clearTypedText();
2103 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002104 }
2105
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 /**
2107 * Start a text search.
2108 *
2109 * @return {@code true} if the search will start immediately, so any further keypresses
2110 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2111 * to buffer keypresses.
2112 */
2113 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002114 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002115 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2116 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2117 sourceBounds);
2118 }
2119
Michael Jurkaa33411c2012-06-14 16:18:21 -07002120 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002122 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002123 }
2124
2125 /**
2126 * Starts the global search activity. This code is a copied from SearchManager
2127 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002130 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002131 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2132 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2133 if (globalSearchActivity == null) {
2134 Log.w(TAG, "No global search activity found.");
2135 return;
2136 }
2137 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2138 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2139 intent.setComponent(globalSearchActivity);
2140 // Make sure that we have a Bundle to put source in
2141 if (appSearchData == null) {
2142 appSearchData = new Bundle();
2143 } else {
2144 appSearchData = new Bundle(appSearchData);
2145 }
Adam Cohen9211d422014-10-07 18:14:53 -07002146 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 if (!appSearchData.containsKey("source")) {
2148 appSearchData.putString("source", getPackageName());
2149 }
2150 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2151 if (!TextUtils.isEmpty(initialQuery)) {
2152 intent.putExtra(SearchManager.QUERY, initialQuery);
2153 }
2154 if (selectInitialQuery) {
2155 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2156 }
2157 intent.setSourceBounds(sourceBounds);
2158 try {
2159 startActivity(intent);
2160 } catch (ActivityNotFoundException ex) {
2161 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 }
2164
Yura4f93ec62014-02-04 14:15:21 +00002165 public boolean isOnCustomContent() {
2166 return mWorkspace.isOnOrMovingToCustomContent();
2167 }
2168
Winson Chung70d72102011-08-12 11:24:35 -07002169 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002170 public boolean onPrepareOptionsMenu(Menu menu) {
2171 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002172 if (!isOnCustomContent()) {
2173 // Close any open folders
2174 closeFolder();
2175 // Stop resizing any widgets
2176 mWorkspace.exitWidgetResizeMode();
2177 if (!mWorkspace.isInOverviewMode()) {
2178 // Show the overview mode
2179 showOverviewMode(true);
2180 } else {
2181 showWorkspace(true);
2182 }
Winson Chunge0298742014-01-17 12:03:00 -08002183 }
Adam Cohen9211d422014-10-07 18:14:53 -07002184 if (mLauncherCallbacks != null) {
2185 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2186 }
2187
Michael Jurkab94f3f82013-09-11 18:08:54 +02002188 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002191 @Override
2192 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002193 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002194 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002195 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002196 }
2197
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 public boolean isWorkspaceLocked() {
2199 return mWorkspaceLoading || mWaitingForResult;
2200 }
2201
Adam Cohen517a7f52014-03-01 12:12:59 -08002202 public boolean isWorkspaceLoading() {
2203 return mWorkspaceLoading;
2204 }
2205
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002206 private void setWorkspaceLoading(boolean value) {
2207 boolean isLocked = isWorkspaceLocked();
2208 mWorkspaceLoading = value;
2209 if (isLocked != isWorkspaceLocked()) {
2210 onWorkspaceLockedChanged();
2211 }
2212 }
2213
2214 private void setWaitingForResult(boolean value) {
2215 boolean isLocked = isWorkspaceLocked();
2216 mWaitingForResult = value;
2217 if (isLocked != isWorkspaceLocked()) {
2218 onWorkspaceLockedChanged();
2219 }
2220 }
2221
Adam Cohen9211d422014-10-07 18:14:53 -07002222 protected void onWorkspaceLockedChanged() {
2223 if (mLauncherCallbacks != null) {
2224 mLauncherCallbacks.onWorkspaceLockedChanged();
2225 }
2226 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002227
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002229 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2232 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002233 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002236
Sunny Goyale6b63a32015-01-16 16:14:29 -08002237 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002238 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002239 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2240 }
2241
Sunny Goyale6b63a32015-01-16 16:14:29 -08002242 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002243 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2244 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002245 if (appWidgetInfo.configure != null) {
2246 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002247 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002248
Bjorn Bringert7984c942009-12-09 15:38:25 +00002249 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002250 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2251 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002254 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002255 Runnable onComplete = new Runnable() {
2256 @Override
2257 public void run() {
2258 // Exit spring loaded mode if necessary after adding the widget
2259 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2260 null);
2261 }
2262 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002264 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002265 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 }
2267 }
Romain Guycbb89e42009-06-08 15:52:54 -07002268
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002269 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002270 // Close any folders that may be open.
2271 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002272 mWorkspace.moveToCustomContentScreen(animate);
2273 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002274
2275 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2276 int[] cell, int spanX, int spanY) {
2277 switch (info.itemType) {
2278 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2279 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2280 int span[] = new int[2];
2281 span[0] = spanX;
2282 span[1] = spanY;
2283 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2284 container, screenId, cell, span);
2285 break;
2286 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2287 processShortcutFromDrop(info.componentName, container, screenId, cell);
2288 break;
2289 default:
2290 throw new IllegalStateException("Unknown item type: " + info.itemType);
2291 }
2292 }
2293
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294 /**
2295 * Process a shortcut drop.
2296 *
2297 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002301 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2302 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002303 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002306 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307
2308 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 mPendingAddInfo.cellX = cell[0];
2310 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312
2313 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2314 createShortcutIntent.setComponent(componentName);
2315 processShortcut(createShortcutIntent);
2316 }
2317
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 /**
2319 * Process a widget drop.
2320 *
2321 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002325 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2326 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002330 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002331 mPendingAddInfo.minSpanX = info.minSpanX;
2332 mPendingAddInfo.minSpanY = info.minSpanY;
2333
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.cellX = cell[0];
2336 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002338 if (span != null) {
2339 mPendingAddInfo.spanX = span[0];
2340 mPendingAddInfo.spanY = span[1];
2341 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 AppWidgetHostView hostView = info.boundWidget;
2344 int appWidgetId;
2345 if (hostView != null) {
2346 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002347 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002348 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 // In this case, we either need to start an activity to get permission to bind
2350 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002351 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002352 Bundle options = info.bindOptions;
2353
Sunny Goyalffe83f12014-08-14 17:39:34 -07002354 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2355 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002356 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002357 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002358 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002359 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002360 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2361 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2362 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002363 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2364 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002365 // TODO: we need to make sure that this accounts for the options bundle.
2366 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2368 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002369 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370 }
2371
Joe Onoratodeb98af2010-02-19 14:59:39 -08002372 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002373 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 }
2375
Winson Chung24ab2f12010-09-16 14:10:47 -07002376 void processWallpaper(Intent intent) {
2377 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2378 }
2379
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002381 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002382 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 folderInfo.title = getText(R.string.folder_name);
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002387 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002388 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389
2390 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002391 FolderIcon newFolder =
2392 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002395 // Force measure the new folder icon
2396 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2397 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002398 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 }
Romain Guycbb89e42009-06-08 15:52:54 -07002400
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002402 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002403 }
2404
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002406 if (mLauncherCallbacks != null) {
2407 return mLauncherCallbacks.getWallpaperPickerComponent();
2408 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002409 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002410 }
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 Chungc93e5ae2012-07-23 20:48:26 -07002451 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002452 if (mAppsCustomizeContent.getContentType() ==
2453 AppsCustomizePagedView.ContentType.Applications) {
2454 showWorkspace(true);
2455 } else {
2456 showOverviewMode(true);
2457 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002458 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002459 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002461 Folder openFolder = mWorkspace.getOpenFolder();
2462 if (openFolder.isEditingName()) {
2463 openFolder.dismissEditingName();
2464 } else {
2465 closeFolder();
2466 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002468 mWorkspace.exitWidgetResizeMode();
2469
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 // Back button is a no-op here, but give at least some feedback for the button press
2471 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002472 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002473 }
2474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002476 * Re-listen when widgets are reset.
2477 */
2478 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002479 if (mAppWidgetHost != null) {
2480 mAppWidgetHost.startListening();
2481 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002482 }
2483
2484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 * Launches the intent referred by the clicked shortcut.
2486 *
2487 * @param v The view representing the clicked shortcut.
2488 */
2489 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2491 // view has detached (it's possible for this to happen if the view is removed mid touch).
2492 if (v.getWindowToken() == null) {
2493 return;
2494 }
2495
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002496 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 return;
2498 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002499
Adam Cohen1697b792013-09-17 19:08:21 -07002500 if (v instanceof Workspace) {
2501 if (mWorkspace.isInOverviewMode()) {
2502 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
2504 return;
2505 }
2506
2507 if (v instanceof CellLayout) {
2508 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002509 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 }
2511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002514 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002517 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002520 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002522 } else if (tag instanceof AppInfo) {
2523 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002524 } else if (tag instanceof LauncherAppWidgetInfo) {
2525 if (v instanceof PendingAppWidgetHostView) {
2526 onClickPendingWidget((PendingAppWidgetHostView) v);
2527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529 }
2530
Sunny Goyal508da152014-08-14 10:53:27 -07002531 public void onClickPagedViewIcon(View v) {
2532 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002533 if (mLauncherCallbacks != null) {
2534 mLauncherCallbacks.onClickPagedViewIcon(v);
2535 }
Sunny Goyal508da152014-08-14 10:53:27 -07002536 }
2537
Michael Jurka0e260592010-06-30 17:07:39 -07002538 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002539 return false;
2540 }
2541
Michael Jurkaaf442092010-06-10 17:01:57 -07002542 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002543 * Event handler for the app widget view which has not fully restored.
2544 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002545 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002546 if (mIsSafeModeEnabled) {
2547 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2548 return;
2549 }
2550
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002552 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002553 int widgetId = info.appWidgetId;
2554 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2555 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002556 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2557 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 mPendingAddInfo.copyFrom(info);
2559 mPendingAddWidgetId = widgetId;
2560
Sunny Goyalffe83f12014-08-14 17:39:34 -07002561 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2562 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002563 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002564 } else if (info.installProgress < 0) {
2565 // The install has not been queued
2566 final String packageName = info.providerName.getPackageName();
2567 showBrokenAppInstallDialog(packageName,
2568 new DialogInterface.OnClickListener() {
2569 public void onClick(DialogInterface dialog, int id) {
2570 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2571 }
2572 });
2573 } else {
2574 // Download has started.
2575 final String packageName = info.providerName.getPackageName();
2576 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002577 }
2578 }
2579
2580 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002581 * Event handler for the "grid" button that appears on the home screen, which
2582 * enters all apps mode.
2583 *
2584 * @param v The view that was clicked.
2585 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 protected void onClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2588 if (isAllAppsVisible()) {
2589 showWorkspace(true);
2590 } else {
2591 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2592 }
Adam Cohen9211d422014-10-07 18:14:53 -07002593 if (mLauncherCallbacks != null) {
2594 mLauncherCallbacks.onClickAllAppsButton(v);
2595 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 }
2597
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002598 private void showBrokenAppInstallDialog(final String packageName,
2599 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002600 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 .setTitle(R.string.abandoned_promises_title)
2602 .setMessage(R.string.abandoned_promise_explanation)
2603 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2604 .setNeutralButton(R.string.abandoned_clean_this,
2605 new DialogInterface.OnClickListener() {
2606 public void onClick(DialogInterface dialog, int id) {
2607 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2608 mWorkspace.removeAbandonedPromise(packageName, user);
2609 }
2610 })
2611 .create().show();
2612 return;
2613 }
2614
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 /**
2616 * Event handler for an app shortcut click.
2617 *
2618 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2619 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2622 Object tag = v.getTag();
2623 if (!(tag instanceof ShortcutInfo)) {
2624 throw new IllegalArgumentException("Input must be a Shortcut");
2625 }
2626
2627 // Open shortcut
2628 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002629
2630 if (shortcut.isDisabled != 0) {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2634 }
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 final Intent intent = shortcut.intent;
2640
2641 // Check for special shortcuts
2642 if (intent.getComponent() != null) {
2643 final String shortcutClass = intent.getComponent().getClassName();
2644
2645 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2646 MemoryDumpActivity.startDump(this);
2647 return;
2648 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2649 toggleShowWeightWatcher();
2650 return;
2651 }
2652 }
2653
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002655 if ((v instanceof BubbleTextView)
2656 && shortcut.isPromise()
2657 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002658 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002659 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 new DialogInterface.OnClickListener() {
2661 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002669 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002670
2671 if (mLauncherCallbacks != null) {
2672 mLauncherCallbacks.onClickAppShortcut(v);
2673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 }
2675
Sunny Goyal508da152014-08-14 10:53:27 -07002676 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002678 final ShortcutInfo shortcut;
2679 final Intent intent;
2680 if (tag instanceof ShortcutInfo) {
2681 shortcut = (ShortcutInfo) tag;
2682 intent = shortcut.intent;
2683 int[] pos = new int[2];
2684 v.getLocationOnScreen(pos);
2685 intent.setSourceBounds(new Rect(pos[0], pos[1],
2686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002687
Sunny Goyal508da152014-08-14 10:53:27 -07002688 } else if (tag instanceof AppInfo) {
2689 shortcut = null;
2690 intent = ((AppInfo) tag).intent;
2691 } else {
2692 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2693 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694
2695 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 mStats.recordLaunch(intent, shortcut);
2697
2698 if (success && v instanceof BubbleTextView) {
2699 mWaitingForResume = (BubbleTextView) v;
2700 mWaitingForResume.setStayPressed(true);
2701 }
2702 }
2703
2704 /**
2705 * Event handler for a folder icon click.
2706 *
2707 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2708 */
2709 protected void onClickFolderIcon(View v) {
2710 if (LOGD) Log.d(TAG, "onClickFolder");
2711 if (!(v instanceof FolderIcon)){
2712 throw new IllegalArgumentException("Input must be a FolderIcon");
2713 }
2714
2715 FolderIcon folderIcon = (FolderIcon) v;
2716 final FolderInfo info = folderIcon.getFolderInfo();
2717 Folder openFolder = mWorkspace.getFolderForTag(info);
2718
2719 // If the folder info reports that the associated folder is open, then verify that
2720 // it is actually opened. There have been a few instances where this gets out of sync.
2721 if (info.opened && openFolder == null) {
2722 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2723 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2724 info.opened = false;
2725 }
2726
2727 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2728 // Close any open folder
2729 closeFolder();
2730 // Open the requested folder
2731 openFolder(folderIcon);
2732 } else {
2733 // Find the open folder...
2734 int folderScreen;
2735 if (openFolder != null) {
2736 folderScreen = mWorkspace.getPageForView(openFolder);
2737 // .. and close it
2738 closeFolder(openFolder);
2739 if (folderScreen != mWorkspace.getCurrentPage()) {
2740 // Close any folder open on the current screen
2741 closeFolder();
2742 // Pull the folder onto this screen
2743 openFolder(folderIcon);
2744 }
2745 }
2746 }
Adam Cohen9211d422014-10-07 18:14:53 -07002747
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickFolderIcon(v);
2750 }
Michael Jurka5130e402011-10-13 04:55:35 -07002751 }
2752
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002753 /**
2754 * Event handler for the (Add) Widgets button that appears after a long press
2755 * on the home screen.
2756 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002757 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002759 if (mIsSafeModeEnabled) {
2760 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2761 } else {
2762 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickAddWidgetButton(view);
2765 }
Adam Cohen9211d422014-10-07 18:14:53 -07002766 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002767 }
2768
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 /**
2770 * Event handler for the wallpaper picker button that appears after a long press
2771 * on the home screen.
2772 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002773 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2775 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2776 pickWallpaper.setComponent(getWallpaperPickerComponent());
2777 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002778
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickWallpaperPicker(v);
2781 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 }
2783
2784 /**
2785 * Event handler for a click on the settings button that appears after a long press
2786 * on the home screen.
2787 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002788 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onClickSettingsButton(v);
2792 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 }
2794
Michael Jurka5130e402011-10-13 04:55:35 -07002795 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002796 // Provide the same haptic feedback that the system offers for virtual keys.
2797 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002798 }
2799
Adam Cohen61f560d2013-09-30 15:58:20 -07002800 public void performHapticFeedbackOnTouchDown(View v) {
2801 // Provide the same haptic feedback that the system offers for virtual keys.
2802 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2803 }
2804
2805 public View.OnTouchListener getHapticFeedbackTouchListener() {
2806 if (mHapticFeedbackTouchListener == null) {
2807 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2808 @Override
2809 public boolean onTouch(View v, MotionEvent event) {
2810 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2811 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2812 }
2813 return false;
2814 }
2815 };
2816 }
2817 return mHapticFeedbackTouchListener;
2818 }
2819
Adam Cohen9211d422014-10-07 18:14:53 -07002820 public void onDragStarted(View view) {
2821 if (isOnCustomContent()) {
2822 // Custom content screen doesn't participate in drag and drop. If on custom
2823 // content screen, move to default.
2824 moveWorkspaceToDefaultScreen();
2825 }
2826
2827 if (mLauncherCallbacks != null) {
2828 mLauncherCallbacks.onDragStarted(view);
2829 }
2830 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002831
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002832 /**
2833 * Called when the user stops interacting with the launcher.
2834 * This implies that the user is now on the homescreen and is not doing housekeeping.
2835 */
Adam Cohen9211d422014-10-07 18:14:53 -07002836 protected void onInteractionEnd() {
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onInteractionEnd();
2839 }
2840 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002841
2842 /**
2843 * Called when the user starts interacting with the launcher.
2844 * The possible interactions are:
2845 * - open all apps
2846 * - reorder an app shortcut, or a widget
2847 * - open the overview mode.
2848 * This is a good time to stop doing things that only make sense
2849 * when the user is on the homescreen and not doing housekeeping.
2850 */
Adam Cohen9211d422014-10-07 18:14:53 -07002851 protected void onInteractionBegin() {
2852 if (mLauncherCallbacks != null) {
2853 mLauncherCallbacks.onInteractionBegin();
2854 }
2855 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002856
Kenny Guyf07af7b2014-07-31 11:39:16 +01002857 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002858 try {
2859 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002860 launcherApps.showAppDetailsForProfile(componentName, user);
2861 } catch (SecurityException e) {
2862 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2863 Log.e(TAG, "Launcher does not have permission to launch settings");
2864 } catch (ActivityNotFoundException e) {
2865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2866 Log.e(TAG, "Unable to launch settings");
2867 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002868 }
2869
Michael Jurka1e2f4652013-07-08 18:03:46 -07002870 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002871 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2872 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002873 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 // System applications cannot be installed. For now, show a toast explaining that.
2875 // We may give them the option of disabling apps this way.
2876 int messageId = R.string.uninstall_system_app_text;
2877 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002878 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002879 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002880 String packageName = componentName.getPackageName();
2881 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 Intent intent = new Intent(
2883 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002884 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2885 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002886 if (user != null) {
2887 user.addToIntent(intent, Intent.EXTRA_USER);
2888 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002889 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002892 }
2893
Michael Jurka86a720e2012-05-09 11:23:23 -07002894 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002895 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002897 // Only launch using the new animation if the shortcut has not opted out (this is a
2898 // private contract between launcher and may be ignored in the future).
2899 boolean useLaunchAnimation = (v != null) &&
2900 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002901 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2902 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002903
Kenny Guy1317e2d2014-05-08 18:52:50 +01002904 UserHandleCompat user = null;
2905 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2906 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2907 user = userManager.getUserForSerialNumber(serialNumber);
2908 }
2909
2910 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002911 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002912 ActivityOptions opts = null;
2913 if (sClipRevealMethod != null) {
2914 // TODO: call method directly when Launcher3 can depend on M APIs
2915 int left = 0, top = 0;
2916 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2917 if (v instanceof TextView) {
2918 // Launch from center of icon, not entire view
2919 TextView tv = (TextView) v;
2920 Drawable[] drawables = tv.getCompoundDrawables();
2921 if (drawables != null && drawables[1] != null) {
2922 Rect bounds = drawables[1].getBounds();
2923 left = (width - bounds.width()) / 2;
2924 top = tv.getPaddingTop();
2925 width = bounds.width();
2926 height = bounds.height();
2927 }
2928 }
2929 try {
2930 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2931 left, top, width, height);
2932 } catch (IllegalAccessException e) {
2933 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2934 sClipRevealMethod = null;
2935 } catch (InvocationTargetException e) {
2936 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2937 sClipRevealMethod = null;
2938 }
2939 }
2940 if (opts == null) {
2941 opts = Utilities.isLmpOrAbove() ?
2942 ActivityOptions.makeCustomAnimation(this,
2943 R.anim.task_open_enter, R.anim.no_anim) :
2944 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2945 v.getMeasuredWidth(), v.getMeasuredHeight());
2946 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002947 optsBundle = opts.toBundle();
2948 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002949
2950 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2951 // Could be launching some bookkeeping activity
2952 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002953 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002954 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002955 launcherApps.startActivityForProfile(intent.getComponent(), user,
2956 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002957 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002958 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 } catch (SecurityException e) {
2960 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002961 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002963 "or use the exported attribute for this activity. "
2964 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002966 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002968
Michael Jurka86a720e2012-05-09 11:23:23 -07002969 boolean startActivitySafely(View v, Intent intent, Object tag) {
2970 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002971 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2972 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2973 return false;
2974 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002975 try {
2976 success = startActivity(v, intent, tag);
2977 } catch (ActivityNotFoundException e) {
2978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2979 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2980 }
2981 return success;
2982 }
2983
Adam Cohen268c4752012-06-06 17:47:33 -07002984 /**
2985 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2986 * in the DragLayer in the exact absolute location of the original FolderIcon.
2987 */
2988 private void copyFolderIconToImage(FolderIcon fi) {
2989 final int width = fi.getMeasuredWidth();
2990 final int height = fi.getMeasuredHeight();
2991
2992 // Lazy load ImageView, Bitmap and Canvas
2993 if (mFolderIconImageView == null) {
2994 mFolderIconImageView = new ImageView(this);
2995 }
2996 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2997 mFolderIconBitmap.getHeight() != height) {
2998 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2999 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3000 }
3001
3002 DragLayer.LayoutParams lp;
3003 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3004 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3005 } else {
3006 lp = new DragLayer.LayoutParams(width, height);
3007 }
3008
Adam Cohen307fe232012-08-16 17:55:58 -07003009 // The layout from which the folder is being opened may be scaled, adjust the starting
3010 // view size by this scale factor.
3011 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003012 lp.customPosition = true;
3013 lp.x = mRectForFolderAnimation.left;
3014 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003015 lp.width = (int) (scale * width);
3016 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003017
3018 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3019 fi.draw(mFolderIconCanvas);
3020 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003021 if (fi.getFolder() != null) {
3022 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3023 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003024 }
Adam Cohen268c4752012-06-06 17:47:33 -07003025 // Just in case this image view is still in the drag layer from a previous animation,
3026 // we remove it and re-add it.
3027 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3028 mDragLayer.removeView(mFolderIconImageView);
3029 }
3030 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003031 if (fi.getFolder() != null) {
3032 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003033 }
Adam Cohen268c4752012-06-06 17:47:33 -07003034 }
3035
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003037 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003038 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3039 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3040 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3041
Adam Cohenc51934b2011-07-26 21:07:43 -07003042 FolderInfo info = (FolderInfo) fi.getTag();
3043 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3044 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003045 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3046 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003047 }
3048
Adam Cohen268c4752012-06-06 17:47:33 -07003049 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3050 copyFolderIconToImage(fi);
3051 fi.setVisibility(View.INVISIBLE);
3052
Michael Jurka2ecf9952012-06-18 12:52:28 -07003053 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003054 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003055 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003056 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3057 }
3058 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003059 oa.start();
3060 }
3061
Adam Cohen268c4752012-06-06 17:47:33 -07003062 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003063 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003064 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3065 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3066 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003069
Adam Cohen268c4752012-06-06 17:47:33 -07003070 // We remove and re-draw the FolderIcon in-case it has changed
3071 mDragLayer.removeView(mFolderIconImageView);
3072 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003073 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003074 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003075 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003076 oa.addListener(new AnimatorListenerAdapter() {
3077 @Override
3078 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003079 if (cl != null) {
3080 cl.clearFolderLeaveBehind();
3081 // Remove the ImageView copy of the FolderIcon and make the original visible.
3082 mDragLayer.removeView(mFolderIconImageView);
3083 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003084 }
3085 }
3086 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003087 oa.start();
3088 }
3089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003091 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 * is animated relative to the specified View. If the View is null, no animation
3093 * is played.
3094 *
3095 * @param folderInfo The FolderInfo describing the folder to open.
3096 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003097 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003098 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003099 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100
Adam Cohena9cf38f2011-05-02 15:36:58 -07003101 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 // Just verify that the folder hasn't already been added to the DragLayer.
3104 // There was a one-off crash where the folder had a parent already.
3105 if (folder.getParent() == null) {
3106 mDragLayer.addView(folder);
3107 mDragController.addDropTarget((DropTarget) folder);
3108 } else {
3109 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3110 folder.getParent() + ").");
3111 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003112 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003113 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003114
3115 // Notify the accessibility manager that this folder "window" has appeared and occluded
3116 // the workspace items
3117 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3118 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 }
3120
3121 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003122 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003124 if (folder.isEditingName()) {
3125 folder.dismissEditingName();
3126 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 closeFolder(folder);
3128 }
3129 }
3130
3131 void closeFolder(Folder folder) {
3132 folder.getInfo().opened = false;
3133
3134 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3135 if (parent != null) {
3136 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3137 shrinkAndFadeInFolderIcon(fi);
3138 }
3139 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003140
3141 // Notify the accessibility manager that this folder "window" has disappeard and no
3142 // longer occludeds the workspace items
3143 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
3145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003147 if (!isDraggingEnabled()) return false;
3148 if (isWorkspaceLocked()) return false;
3149 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150
Adam Cohen1697b792013-09-17 19:08:21 -07003151 if (v instanceof Workspace) {
3152 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003153 if (mWorkspace.enterOverviewMode()) {
3154 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3155 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3156 return true;
3157 } else {
3158 return false;
3159 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003160 } else {
3161 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003162 }
Adam Cohen1697b792013-09-17 19:08:21 -07003163 }
3164
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003165 CellLayout.CellInfo longClickCellInfo = null;
3166 View itemUnderLongClick = null;
3167 if (v.getTag() instanceof ItemInfo) {
3168 ItemInfo info = (ItemInfo) v.getTag();
3169 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3170 itemUnderLongClick = longClickCellInfo.cell;
3171 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
Winson Chung3d503fb2011-07-13 17:25:49 -07003174 // The hotseat touch handling does not go through Workspace, and we always allow long press
3175 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003176 final boolean inHotseat = isHotseatLayout(v);
3177 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003179 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003180 // User long pressed on empty space
3181 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3182 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003183 if (mWorkspace.isInOverviewMode()) {
3184 mWorkspace.startReordering(v);
3185 } else {
3186 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003189 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3190 mHotseat.getOrderInHotseat(
3191 longClickCellInfo.cellX,
3192 longClickCellInfo.cellY));
3193 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003195 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196 }
3197 }
3198 }
3199 return true;
3200 }
3201
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 boolean isHotseatLayout(View layout) {
3203 return mHotseat != null && layout != null &&
3204 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3205 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003206
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 /**
3208 * Returns the CellLayout of the specified container at the specified screen.
3209 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003210 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3212 if (mHotseat != null) {
3213 return mHotseat.getLayout();
3214 } else {
3215 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003216 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003218 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003219 }
3220 }
3221
Daniel Sandler843e8602010-06-07 14:59:01 -04003222 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003223 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003224 }
3225
Craig Mautner360310b2012-10-26 15:13:08 -07003226 private void setWorkspaceBackground(boolean workspace) {
3227 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003228 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003229 }
3230
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003231 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003232 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3233 int curflags = getWindow().getAttributes().flags
3234 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3235 if (wpflags != curflags) {
3236 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3237 }
Craig Mautner360310b2012-10-26 15:13:08 -07003238 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003239 }
3240
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003241 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3242 if (v instanceof LauncherTransitionable) {
3243 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3244 }
3245 }
3246
Michael Jurkabed61d22012-02-14 22:51:29 -08003247 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3248 if (v instanceof LauncherTransitionable) {
3249 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3250 }
Winson Chung70442722012-02-10 15:43:22 -08003251
3252 // Update the workspace transition step as well
3253 dispatchOnLauncherTransitionStep(v, 0f);
3254 }
3255
3256 private void dispatchOnLauncherTransitionStep(View v, float t) {
3257 if (v instanceof LauncherTransitionable) {
3258 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3259 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003260 }
3261
3262 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3263 if (v instanceof LauncherTransitionable) {
3264 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3265 }
Winson Chung70442722012-02-10 15:43:22 -08003266
3267 // Update the workspace transition step as well
3268 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003269 }
3270
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003271 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 * Things to test when changing the following seven functions.
3273 * - Home from workspace
3274 * - from center screen
3275 * - from other screens
3276 * - Home from all apps
3277 * - from center screen
3278 * - from other screens
3279 * - Back from all apps
3280 * - from center screen
3281 * - from other screens
3282 * - Launch app from workspace and quit
3283 * - with back
3284 * - with home
3285 * - Launch app from all apps and quit
3286 * - with back
3287 * - with home
3288 * - Go to a screen that's not the default, then all
3289 * apps, and launch and app, and go back
3290 * - with back
3291 * -with home
3292 * - On workspace, long press power and go back
3293 * - with back
3294 * - with home
3295 * - On all apps, long press power and go back
3296 * - with back
3297 * - with home
3298 * - On workspace, power off
3299 * - On all apps, power off
3300 * - Launch an app and turn off the screen while in that app
3301 * - Go back with home key
3302 * - Go back with back key TODO: make this not go to workspace
3303 * - From all apps
3304 * - From workspace
3305 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3306 * - From all apps
3307 * - From the center workspace
3308 * - From another workspace
3309 */
3310
3311 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003312 * Zoom the camera out from the workspace to reveal 'toView'.
3313 * Assumes that the view to show is anchored at either the very top or very bottom
3314 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003315 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003316 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003317 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3318 showAppsCustomizeHelper(animated, springLoaded, contentType);
3319 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003320
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003321 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003322 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3323 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003324 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003325 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 mStateAnimation.cancel();
3327 mStateAnimation = null;
3328 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003329
Kenny Guyd794a3f2014-09-16 15:17:58 +01003330 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003331
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003332 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003333
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003335 final int itemsAlphaStagger =
3336 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003337
Michael Jurkabed61d22012-02-14 22:51:29 -08003338 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003339 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003340
Adam Cohen80e6beb2015-02-13 16:14:33 -08003341 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003342
Adam Cohen6c5891a2014-07-09 23:53:15 -07003343 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3344 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003345 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003346 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003347 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003348 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3349 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003350 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3351 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003352
Adam Cohena38dc902014-09-07 17:48:55 +02003353 // If for some reason our views aren't initialized, don't animate
3354 boolean initialized = getAllAppsButton() != null;
3355
3356 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003357 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003358 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3359 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003360
Adam Cohen9bfdb762014-07-21 17:44:06 -07003361 final View page = content.getPageAt(content.getCurrentPage());
3362 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003363
Adam Cohen63f1ec02014-08-12 09:23:13 -07003364 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3365 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003366 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3367 } else {
3368 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3369 }
3370
Adam Cohen9bfdb762014-07-21 17:44:06 -07003371 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003372 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003373 revealView.setVisibility(View.VISIBLE);
3374 // We need to hide this view as the animation start will be posted.
3375 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003376
Adam Cohen9bfdb762014-07-21 17:44:06 -07003377 int width = revealView.getMeasuredWidth();
3378 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003379 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003380
Adam Cohen63f1ec02014-08-12 09:23:13 -07003381 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003382 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003383
Adam Cohen63f1ec02014-08-12 09:23:13 -07003384 // Get the y delta between the center of the page and the center of the all apps button
3385 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3386 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003387
Adam Cohen2854d252014-08-27 16:04:07 -07003388 float alpha = 0;
3389 float xDrift = 0;
3390 float yDrift = 0;
3391 if (material) {
3392 alpha = isWidgetTray ? 0.3f : 1f;
3393 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3394 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3395 } else {
3396 yDrift = 2 * height / 3;
3397 xDrift = 0;
3398 }
3399 final float initAlpha = alpha;
3400
Adam Cohen80e6beb2015-02-13 16:14:33 -08003401 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003402 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003403 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003405 PropertyValuesHolder panelDriftX =
3406 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003407
Adam Cohen2854d252014-08-27 16:04:07 -07003408 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3409 panelAlpha, panelDriftY, panelDriftX);
3410
Adam Cohen9bfdb762014-07-21 17:44:06 -07003411 panelAlphaAndDrift.setDuration(revealDuration);
3412 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Adam Cohen9bfdb762014-07-21 17:44:06 -07003414 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003415
Adam Cohen4e243a22014-08-10 18:30:55 -07003416 if (page != null) {
3417 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003418 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003419
Adam Cohen2854d252014-08-27 16:04:07 -07003420 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3421 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003422 pageDrift.setDuration(revealDuration);
3423 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003424 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003425 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003426
Adam Cohen63f1ec02014-08-12 09:23:13 -07003427 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003428 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003429 itemsAlpha.setDuration(revealDuration);
3430 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3431 itemsAlpha.setStartDelay(itemsAlphaStagger);
3432 mStateAnimation.play(itemsAlpha);
3433 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003434
Adam Cohen4e243a22014-08-10 18:30:55 -07003435 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3436 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003437 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003438 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003439 indicatorsAlpha.setDuration(revealDuration);
3440 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003441
Adam Cohen9bfdb762014-07-21 17:44:06 -07003442 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003443 final View allApps = getAllAppsButton();
3444 int allAppsButtonSize = LauncherAppState.getInstance().
3445 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3446 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003447 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003448 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003449 reveal.setDuration(revealDuration);
3450 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003451
3452 reveal.addListener(new AnimatorListenerAdapter() {
3453 public void onAnimationStart(Animator animation) {
3454 if (!isWidgetTray) {
3455 allApps.setVisibility(View.INVISIBLE);
3456 }
3457 }
3458 public void onAnimationEnd(Animator animation) {
3459 if (!isWidgetTray) {
3460 allApps.setVisibility(View.VISIBLE);
3461 }
3462 }
3463 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003464 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003465 }
Michael Jurka1899a362011-11-03 13:50:45 -07003466
Adam Cohen9bfdb762014-07-21 17:44:06 -07003467 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3468 @Override
3469 public void onAnimationEnd(Animator animation) {
3470 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3471 dispatchOnLauncherTransitionEnd(toView, animated, false);
3472
3473 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003474
3475 for (View v : layerViews.keySet()) {
3476 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3477 v.setLayerType(View.LAYER_TYPE_NONE, null);
3478 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003479 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003480 content.setPageBackgroundsVisible(true);
3481
3482 // Hide the search bar
3483 if (mSearchDropTargetBar != null) {
3484 mSearchDropTargetBar.hideSearchBar(false);
3485 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003486
3487 // This can hold unnecessary references to views.
3488 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003489 }
3490
Adam Cohen9bfdb762014-07-21 17:44:06 -07003491 });
3492
Adam Cohen6c5891a2014-07-09 23:53:15 -07003493 if (workspaceAnim != null) {
3494 mStateAnimation.play(workspaceAnim);
3495 }
Adam Cohen2854d252014-08-27 16:04:07 -07003496
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003497 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3498 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003499 final AnimatorSet stateAnimation = mStateAnimation;
3500 final Runnable startAnimRunnable = new Runnable() {
3501 public void run() {
3502 // Check that mStateAnimation hasn't changed while
3503 // we waited for a layout/draw pass
3504 if (mStateAnimation != stateAnimation)
3505 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003506 dispatchOnLauncherTransitionStart(fromView, animated, false);
3507 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003508
3509 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003510
3511 for (View v : layerViews.keySet()) {
3512 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3513 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3514 }
3515 }
3516
Kenny Guyd794a3f2014-09-16 15:17:58 +01003517 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003518 for (View v : layerViews.keySet()) {
3519 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003520 }
3521 }
3522 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003523 }
3524 };
Adam Cohen2854d252014-08-27 16:04:07 -07003525 toView.bringToFront();
3526 toView.setVisibility(View.VISIBLE);
3527 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003528 } else {
3529 toView.setTranslationX(0.0f);
3530 toView.setTranslationY(0.0f);
3531 toView.setScaleX(1.0f);
3532 toView.setScaleY(1.0f);
3533 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003534 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003535
Daniel Sandlere4f98912013-06-25 15:13:26 -04003536 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003537 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003538 if (mSearchDropTargetBar != null) {
3539 mSearchDropTargetBar.hideSearchBar(false);
3540 }
Michael Jurkaabded662011-03-04 12:06:57 -08003541 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003542 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003543 dispatchOnLauncherTransitionStart(fromView, animated, false);
3544 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003545 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003546 dispatchOnLauncherTransitionStart(toView, animated, false);
3547 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003548 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003549 }
3550
3551 /**
3552 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003553 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003554 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003555 */
Adam Cohened307df2013-10-02 09:37:31 -07003556 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003557 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003558
Michael Jurkab3e22d92011-10-31 15:58:33 -07003559 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003560 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003561 mStateAnimation.cancel();
3562 mStateAnimation = null;
3563 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003564
Kenny Guyd794a3f2014-09-16 15:17:58 +01003565 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003566 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003567
Adam Cohen63f1ec02014-08-12 09:23:13 -07003568 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003569 final int itemsAlphaStagger =
3570 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003571
Winson Chungf0ea4d32011-06-06 14:27:16 -07003572 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003573 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003574 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003575 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003576
Adam Cohened307df2013-10-02 09:37:31 -07003577 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003578 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003579 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003580 } else if (toState == Workspace.State.SPRING_LOADED ||
3581 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003582 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003583 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003584 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003585
Adam Cohena38dc902014-09-07 17:48:55 +02003586 // If for some reason our views aren't initialized, don't animate
3587 boolean initialized = getAllAppsButton() != null;
3588
3589 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003590 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003591 if (workspaceAnim != null) {
3592 mStateAnimation.play(workspaceAnim);
3593 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003594
Adam Cohen9bfdb762014-07-21 17:44:06 -07003595 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3596 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003597
Adam Cohen9bfdb762014-07-21 17:44:06 -07003598 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003599
3600 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3601 int count = content.getChildCount();
3602 for (int i = 0; i < count; i++) {
3603 View child = content.getChildAt(i);
3604 if (child != page) {
3605 child.setVisibility(View.INVISIBLE);
3606 }
3607 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003608 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003609
Adam Cohen2854d252014-08-27 16:04:07 -07003610 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3611 // don't perform all these no-op animations. In particularly, this was causing
3612 // the all-apps button to pop in and out.
3613 if (fromView.getVisibility() == View.VISIBLE) {
3614 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3615 final boolean isWidgetTray =
3616 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003617
Adam Cohen2854d252014-08-27 16:04:07 -07003618 if (isWidgetTray) {
3619 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3620 } else {
3621 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003622 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003623
Adam Cohen2854d252014-08-27 16:04:07 -07003624 int width = revealView.getMeasuredWidth();
3625 int height = revealView.getMeasuredHeight();
3626 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3627
3628 // Hide the real page background, and swap in the fake one
3629 revealView.setVisibility(View.VISIBLE);
3630 content.setPageBackgroundsVisible(false);
3631
3632 final View allAppsButton = getAllAppsButton();
3633 revealView.setTranslationY(0);
3634 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3635 allAppsButton, null);
3636
3637 float xDrift = 0;
3638 float yDrift = 0;
3639 if (material) {
3640 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3641 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3642 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003643 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003644 xDrift = 0;
3645 }
3646
Adam Cohen80e6beb2015-02-13 16:14:33 -08003647 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003648 TimeInterpolator decelerateInterpolator = material ?
3649 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003650 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003651
3652 // The vertical motion of the apps panel should be delayed by one frame
3653 // from the conceal animation in order to give the right feel. We correpsondingly
3654 // shorten the duration so that the slide and conceal end at the same time.
3655 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3656 0, yDrift);
3657 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3658 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3659 panelDriftY.setInterpolator(decelerateInterpolator);
3660 mStateAnimation.play(panelDriftY);
3661
3662 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3663 0, xDrift);
3664 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3665 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3666 panelDriftX.setInterpolator(decelerateInterpolator);
3667 mStateAnimation.play(panelDriftX);
3668
3669 if (isWidgetTray || !material) {
3670 float finalAlpha = material ? 0.4f : 0f;
3671 revealView.setAlpha(1f);
3672 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3673 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003674 panelAlpha.setDuration(material ? revealDuration : 150);
3675 panelAlpha.setInterpolator(decelerateInterpolator);
3676 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003677 mStateAnimation.play(panelAlpha);
3678 }
3679
3680 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003681 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003682
3683 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3684 0, yDrift);
3685 page.setTranslationY(0);
3686 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3687 pageDrift.setInterpolator(decelerateInterpolator);
3688 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3689 mStateAnimation.play(pageDrift);
3690
3691 page.setAlpha(1f);
3692 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3693 itemsAlpha.setDuration(100);
3694 itemsAlpha.setInterpolator(decelerateInterpolator);
3695 mStateAnimation.play(itemsAlpha);
3696 }
3697
3698 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3699 pageIndicators.setAlpha(1f);
3700 ObjectAnimator indicatorsAlpha =
3701 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3702 indicatorsAlpha.setDuration(revealDuration);
3703 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3704 mStateAnimation.play(indicatorsAlpha);
3705
3706 width = revealView.getMeasuredWidth();
3707
3708 if (material) {
3709 if (!isWidgetTray) {
3710 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003711 }
Adam Cohen2854d252014-08-27 16:04:07 -07003712 int allAppsButtonSize = LauncherAppState.getInstance().
3713 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3714 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3715 Animator reveal =
3716 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3717 height / 2, revealRadius, finalRadius);
3718 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3719 reveal.setDuration(revealDuration);
3720 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003721
Adam Cohen2854d252014-08-27 16:04:07 -07003722 reveal.addListener(new AnimatorListenerAdapter() {
3723 public void onAnimationEnd(Animator animation) {
3724 revealView.setVisibility(View.INVISIBLE);
3725 if (!isWidgetTray) {
3726 allAppsButton.setVisibility(View.VISIBLE);
3727 }
3728 }
3729 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003730
Adam Cohen2854d252014-08-27 16:04:07 -07003731 mStateAnimation.play(reveal);
3732 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003733
Adam Cohen2854d252014-08-27 16:04:07 -07003734 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3735 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3736 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003737 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003738
3739 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003740 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003741 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003742 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003743 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3744 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003745 if (onCompleteRunnable != null) {
3746 onCompleteRunnable.run();
3747 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003748
Adam Cohen80e6beb2015-02-13 16:14:33 -08003749 for (View v : layerViews.keySet()) {
3750 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3751 v.setLayerType(View.LAYER_TYPE_NONE, null);
3752 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003753 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003754
Adam Cohen9bfdb762014-07-21 17:44:06 -07003755 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003756 // Unhide side pages
3757 int count = content.getChildCount();
3758 for (int i = 0; i < count; i++) {
3759 View child = content.getChildAt(i);
3760 child.setVisibility(View.VISIBLE);
3761 }
3762
3763 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003764 if (page != null) {
3765 page.setTranslationX(0);
3766 page.setTranslationY(0);
3767 page.setAlpha(1);
3768 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003769 content.setCurrentPage(content.getNextPage());
3770
Chet Haasebc2f0822012-10-26 17:59:53 -07003771 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003772
3773 // This can hold unnecessary references to views.
3774 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003775 }
3776 });
3777
Adam Cohen2854d252014-08-27 16:04:07 -07003778 final AnimatorSet stateAnimation = mStateAnimation;
3779 final Runnable startAnimRunnable = new Runnable() {
3780 public void run() {
3781 // Check that mStateAnimation hasn't changed while
3782 // we waited for a layout/draw pass
3783 if (mStateAnimation != stateAnimation)
3784 return;
3785 dispatchOnLauncherTransitionStart(fromView, animated, false);
3786 dispatchOnLauncherTransitionStart(toView, animated, false);
3787
Adam Cohen80e6beb2015-02-13 16:14:33 -08003788 for (View v : layerViews.keySet()) {
3789 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3790 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3791 }
3792 }
3793
Kenny Guyd794a3f2014-09-16 15:17:58 +01003794 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003795 for (View v : layerViews.keySet()) {
3796 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003797 }
3798 }
3799 mStateAnimation.start();
3800 }
3801 };
3802 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003803 } else {
3804 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003805 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003806 dispatchOnLauncherTransitionStart(fromView, animated, true);
3807 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003808 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003809 dispatchOnLauncherTransitionStart(toView, animated, true);
3810 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003811 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003812 }
3813
Michael Jurkae326f182011-11-21 14:05:46 -08003814 @Override
3815 public void onTrimMemory(int level) {
3816 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003817 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3818 // The widget preview db can result in holding onto over
3819 // 3MB of memory for caching which isn't necessary.
3820 SQLiteDatabase.releaseMemory();
3821
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003822 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003823 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003824 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003825 }
Michael Jurkae326f182011-11-21 14:05:46 -08003826 }
3827 }
3828
Adam Cohened307df2013-10-02 09:37:31 -07003829 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003830 showWorkspace(animated, null);
3831 }
3832
Adam Cohened307df2013-10-02 09:37:31 -07003833 protected void showWorkspace() {
3834 showWorkspace(true);
3835 }
3836
Adam Cohened66b2b2012-01-23 17:28:51 -08003837 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003838 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003839 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003840 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003841 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003842
Winson Chungc7d2b602012-05-16 17:02:20 -07003843 // Show the search bar (only animate if we were showing the drop target bar in spring
3844 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003845 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003846 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003847 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003848
Michael Jurkab3e22d92011-10-31 15:58:33 -07003849 // Set focus to the AppsCustomize button
3850 if (mAllAppsButton != null) {
3851 mAllAppsButton.requestFocus();
3852 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003853 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003854
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003855 // Change the state *after* we've called all the transition code
3856 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003857
Winson Chung5fb63472011-02-02 17:03:37 -08003858 // Resume the auto-advance of widgets
3859 mUserPresent = true;
3860 updateRunning();
3861
alanv1d4fde62012-10-17 13:15:47 -07003862 // Send an accessibility event to announce the context change
3863 getWindow().getDecorView()
3864 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003865
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003866 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003867 }
3868
Adam Cohened307df2013-10-02 09:37:31 -07003869 void showOverviewMode(boolean animated) {
3870 mWorkspace.setVisibility(View.VISIBLE);
3871 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3872 mState = State.WORKSPACE;
3873 onWorkspaceShown(animated);
3874 }
3875
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003876 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003877 }
3878
Winson Chung82963d52013-10-09 11:20:57 -07003879 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3880 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003881 if (mState != State.WORKSPACE) return;
3882
Winson Chung82963d52013-10-09 11:20:57 -07003883 if (resetPageToZero) {
3884 mAppsCustomizeTabHost.reset();
3885 }
Winson Chungc58497e2013-09-03 17:48:37 -07003886 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003887 mAppsCustomizeTabHost.post(new Runnable() {
3888 @Override
3889 public void run() {
3890 // We post this in-case the all apps view isn't yet constructed.
3891 mAppsCustomizeTabHost.requestFocus();
3892 }
3893 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003894
Michael Jurkab3e22d92011-10-31 15:58:33 -07003895 // Change the state *after* we've called all the transition code
3896 mState = State.APPS_CUSTOMIZE;
3897
3898 // Pause the auto-advance of widgets until we are out of AllApps
3899 mUserPresent = false;
3900 updateRunning();
3901 closeFolder();
3902
3903 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003904 getWindow().getDecorView()
3905 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003906 }
3907
Adam Cohen7777d962011-08-18 18:58:38 -07003908 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003909 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003910 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003911 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003912 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003913 }
Adam Cohen7777d962011-08-18 18:58:38 -07003914
Adam Cohenad4e15c2013-10-17 16:21:35 -07003915 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003916 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003917 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3918
Winson Chunge7a03942011-08-05 15:05:12 -07003919 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003920 @Override
3921 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003922 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003923 // Before we show workspace, hide all apps again because
3924 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3925 // clean up our state transition functions
3926 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003927 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003928 } else {
3929 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003930 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003931 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003932 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003933 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003934
Michael Jurkad3ef3062010-11-23 16:23:58 -08003935 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003936 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003937 final boolean animated = true;
3938 final boolean springLoaded = true;
3939 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003940 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003941 }
3942 // Otherwise, we are not in spring loaded mode, so don't do anything.
3943 }
3944
Michael Jurkab3e22d92011-10-31 15:58:33 -07003945 void lockAllApps() {
3946 // TODO
3947 }
3948
3949 void unlockAllApps() {
3950 // TODO
3951 }
3952
Sunny Goyal594d76d2014-11-06 10:12:54 -08003953 protected void disableVoiceButtonProxy(boolean disable) {
3954 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003955 }
3956
Cristina Stancu476493b2013-08-07 17:20:14 +01003957 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003958 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3959 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003960 }
3961
Adam Cohen24ce0b32014-01-14 16:18:14 -08003962 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003963 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3964 if (searchProvider == null) {
3965 return null;
3966 }
3967
3968 Bundle opts = new Bundle();
3969 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3970 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3971
3972 SharedPreferences sp = getSharedPreferences(
3973 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3974 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003975 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003976 if (!searchProvider.provider.flattenToString().equals(
3977 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003978 || (widgetInfo == null)
3979 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003980 // A valid widget is not already bound.
3981 if (widgetId > -1) {
3982 mAppWidgetHost.deleteAppWidgetId(widgetId);
3983 widgetId = -1;
3984 }
3985
3986 // Try to bind a new widget
3987 widgetId = mAppWidgetHost.allocateAppWidgetId();
3988
3989 if (!AppWidgetManagerCompat.getInstance(this)
3990 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3991 mAppWidgetHost.deleteAppWidgetId(widgetId);
3992 widgetId = -1;
3993 }
3994
3995 sp.edit()
3996 .putInt(QSB_WIDGET_ID, widgetId)
3997 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3998 .commit();
3999 }
4000
4001 if (widgetId != -1) {
4002 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
4003 mQsb.updateAppWidgetOptions(opts);
4004 mQsb.setPadding(0, 0, 0, 0);
4005 mSearchDropTargetBar.addView(mQsb);
4006 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004007 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004008 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004009 }
4010
Michael Jurkad7c28052012-04-27 15:43:36 -07004011 @Override
4012 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004013 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004014 final List<CharSequence> text = event.getText();
4015 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004016 // Populate event with a fake title based on the current state.
4017 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004018 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004019 } else {
4020 text.add(getString(R.string.all_apps_home_button_label));
4021 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004022 return result;
4023 }
4024
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004025 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004026 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004027 */
4028 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4029 @Override
4030 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004031 closeSystemDialogs();
4032 }
4033 }
4034
4035 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004036 * Receives notifications whenever the appwidgets are reset.
4037 */
4038 private class AppWidgetResetObserver extends ContentObserver {
4039 public AppWidgetResetObserver() {
4040 super(new Handler());
4041 }
4042
4043 @Override
4044 public void onChange(boolean selfChange) {
4045 onAppWidgetReset();
4046 }
4047 }
4048
4049 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004050 * If the activity is currently paused, signal that we need to run the passed Runnable
4051 * in onResume.
4052 *
4053 * This needs to be called from incoming places where resources might have been loaded
4054 * while we are paused. That is becaues the Configuration might be wrong
4055 * when we're not running, and if it comes back to what it was when we
4056 * were paused, we are not restarted.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 *
4060 * @return true if we are currently paused. The caller might be able to
4061 * skip some work in that case since we will come back again.
4062 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004063 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004064 if (mPaused) {
4065 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004066 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004067 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 }
4069 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004070 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004071 return true;
4072 } else {
4073 return false;
4074 }
4075 }
4076
Michael Jurkac402cd92013-05-20 15:49:32 +02004077 private boolean waitUntilResume(Runnable run) {
4078 return waitUntilResume(run, false);
4079 }
4080
Michael Jurka1e2f4652013-07-08 18:03:46 -07004081 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004082 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004083 }
4084
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004086 * If the activity is currently paused, signal that we need to re-run the loader
4087 * in onResume.
4088 *
4089 * This needs to be called from incoming places where resources might have been loaded
4090 * while we are paused. That is becaues the Configuration might be wrong
4091 * when we're not running, and if it comes back to what it was when we
4092 * were paused, we are not restarted.
4093 *
4094 * Implementation of the method from LauncherModel.Callbacks.
4095 *
4096 * @return true if we are currently paused. The caller might be able to
4097 * skip some work in that case since we will come back again.
4098 */
4099 public boolean setLoadOnResume() {
4100 if (mPaused) {
4101 Log.i(TAG, "setLoadOnResume");
4102 mOnResumeNeedsLoad = true;
4103 return true;
4104 } else {
4105 return false;
4106 }
4107 }
4108
4109 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004111 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004112 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004113 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004114 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004115 } else {
4116 return SCREEN_COUNT / 2;
4117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004119
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 /**
4121 * Refreshes the shortcuts shown on the workspace.
4122 *
4123 * Implementation of the method from LauncherModel.Callbacks.
4124 */
4125 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004126 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004127
Michael Jurka7607c2f2013-04-03 14:33:19 -07004128 // If we're starting binding all over again, clear any bind calls we'd postponed in
4129 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4130 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004131 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004132
Winson Chungd64d1762013-08-20 14:37:16 -07004133 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004134 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004135 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004136
Michael Jurka05bf644e2011-11-30 20:28:53 -08004137 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004138 if (mHotseat != null) {
4139 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004140 }
4141 }
4142
Adam Cohendcd297f2013-06-18 13:13:40 -07004143 @Override
4144 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004145 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004146
Adam Cohen5084cba2013-09-03 12:01:16 -07004147 // If there are no screens, we need to have an empty screen
4148 if (orderedScreenIds.size() == 0) {
4149 mWorkspace.addExtraEmptyScreen();
4150 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004151
4152 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004153 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004154 if (hasCustomContentToLeft()) {
4155 mWorkspace.createCustomContentContainer();
4156 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004157 }
Winson Chung64359a52013-07-08 17:17:08 -07004158 }
4159
4160 @Override
4161 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004162 // Log to disk
4163 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4164 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4165 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004166 int count = orderedScreenIds.size();
4167 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004168 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004169 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004170 }
4171
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004172 @Override
4173 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4174 final long screenId, final int[] cell, final int spanX, final int spanY) {
4175 showWorkspace(true, new Runnable() {
4176
4177 @Override
4178 public void run() {
4179 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4180 addPendingItem(info, container, screenId, cell, spanX, spanY);
4181 }
4182 });
4183 }
4184
Adam Cohen39a06042013-07-19 14:30:12 -07004185 private boolean shouldShowWeightWatcher() {
4186 String spKey = LauncherAppState.getSharedPreferencesKey();
4187 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004188 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004189
4190 return show;
4191 }
4192
4193 private void toggleShowWeightWatcher() {
4194 String spKey = LauncherAppState.getSharedPreferencesKey();
4195 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4196 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4197
4198 show = !show;
4199
4200 SharedPreferences.Editor editor = sp.edit();
4201 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4202 editor.commit();
4203
4204 if (mWeightWatcher != null) {
4205 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4206 }
4207 }
4208
Winson Chungd64d1762013-08-20 14:37:16 -07004209 public void bindAppsAdded(final ArrayList<Long> newScreens,
4210 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004211 final ArrayList<ItemInfo> addAnimated,
4212 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004213 Runnable r = new Runnable() {
4214 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004215 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004216 }
4217 };
4218 if (waitUntilResume(r)) {
4219 return;
4220 }
4221
Winson Chungd64d1762013-08-20 14:37:16 -07004222 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004223 if (newScreens != null) {
4224 bindAddScreens(newScreens);
4225 }
Winson Chungd64d1762013-08-20 14:37:16 -07004226
4227 // We add the items without animation on non-visible pages, and with
4228 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004229 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004230 bindItems(addNotAnimated, 0,
4231 addNotAnimated.size(), false);
4232 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004233 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 bindItems(addAnimated, 0,
4235 addAnimated.size(), true);
4236 }
Winson Chungc58497e2013-09-03 17:48:37 -07004237
Winson Chung87412982013-10-03 18:34:14 -07004238 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004239 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004240
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004241 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004242 addedApps != null && mAppsCustomizeContent != null) {
4243 mAppsCustomizeContent.addApps(addedApps);
4244 }
Winson Chungd64d1762013-08-20 14:37:16 -07004245 }
4246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004247 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004248 * Bind the items start-end from the list.
4249 *
4250 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004251 */
Winson Chung64359a52013-07-08 17:17:08 -07004252 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4253 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004254 Runnable r = new Runnable() {
4255 public void run() {
4256 bindItems(shortcuts, start, end, forceAnimateIcons);
4257 }
4258 };
4259 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004260 return;
4261 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004262
Winson Chungf0c6ae02012-03-21 16:10:31 -07004263 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004264 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4265 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004266 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004267 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004268 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004269 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004270 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004271
4272 // Short circuit if we are loading dock items for a configuration which has no dock
4273 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4274 mHotseat == null) {
4275 continue;
4276 }
4277
Joe Onorato9c1289c2009-08-17 11:03:03 -04004278 switch (item.itemType) {
4279 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4280 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004281 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004282 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004283
Winson Chung64359a52013-07-08 17:17:08 -07004284 /*
4285 * TODO: FIX collision case
4286 */
Winson Chungce376632013-07-11 16:12:41 -07004287 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4288 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4289 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004290 View v = cl.getChildAt(item.cellX, item.cellY);
4291 Object tag = v.getTag();
4292 String desc = "Collision while binding workspace item: " + item
4293 + ". Collides with " + tag;
4294 if (LauncherAppState.isDogfoodBuild()) {
4295 throw (new RuntimeException(desc));
4296 } else {
4297 Log.d(TAG, desc);
4298 }
Winson Chungce376632013-07-11 16:12:41 -07004299 }
Winson Chung64359a52013-07-08 17:17:08 -07004300 }
4301
Adam Cohendcd297f2013-06-18 13:13:40 -07004302 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4303 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004304 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004305 // Animate all the applications up now
4306 shortcut.setAlpha(0f);
4307 shortcut.setScaleX(0f);
4308 shortcut.setScaleY(0f);
4309 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004310 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004311 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004312 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004313 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004314 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004315 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004316 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004317 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4318 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004319 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004320 default:
4321 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004323 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004324
Winson Chung997a9232013-07-24 15:33:46 -07004325 if (animateIcons) {
4326 // Animate to the correct page
4327 if (newShortcutsScreenId > -1) {
4328 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004329 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004330 final Runnable startBounceAnimRunnable = new Runnable() {
4331 public void run() {
4332 anim.playTogether(bounceAnims);
4333 anim.start();
4334 }
4335 };
Winson Chung997a9232013-07-24 15:33:46 -07004336 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004337 // We post the animation slightly delayed to prevent slowdowns
4338 // when we are loading right after we return to launcher.
4339 mWorkspace.postDelayed(new Runnable() {
4340 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004341 if (mWorkspace != null) {
4342 mWorkspace.snapToPage(newScreenIndex);
4343 mWorkspace.postDelayed(startBounceAnimRunnable,
4344 NEW_APPS_ANIMATION_DELAY);
4345 }
Winson Chung94d67682013-09-25 16:29:40 -07004346 }
4347 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004348 } else {
4349 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004350 }
4351 }
Winson Chung64359a52013-07-08 17:17:08 -07004352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004354 }
4355
Joe Onorato9c1289c2009-08-17 11:03:03 -04004356 /**
4357 * Implementation of the method from LauncherModel.Callbacks.
4358 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004359 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004360 Runnable r = new Runnable() {
4361 public void run() {
4362 bindFolders(folders);
4363 }
4364 };
4365 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004366 return;
4367 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004368 sFolders.clear();
4369 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004371
4372 /**
4373 * Add the views for a widget to the workspace.
4374 *
4375 * Implementation of the method from LauncherModel.Callbacks.
4376 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004377 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004378 Runnable r = new Runnable() {
4379 public void run() {
4380 bindAppWidget(item);
4381 }
4382 };
4383 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004384 return;
4385 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004386
Daniel Sandler843e8602010-06-07 14:59:01 -04004387 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4388 if (DEBUG_WIDGETS) {
4389 Log.d(TAG, "bindAppWidget: " + item);
4390 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004391 final Workspace workspace = mWorkspace;
4392
Adam Cohen59400422014-03-05 18:07:04 -08004393 LauncherAppWidgetProviderInfo appWidgetInfo =
4394 LauncherModel.getProviderInfo(this, item.providerName);
4395
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004396 if (!mIsSafeModeEnabled
4397 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4398 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004399 if (appWidgetInfo == null) {
4400 if (DEBUG_WIDGETS) {
4401 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4402 + " belongs to component " + item.providerName
4403 + ", as the povider is null");
4404 }
4405 LauncherModel.deleteItemFromDatabase(this, item);
4406 return;
4407 }
4408 // Note: This assumes that the id remap broadcast is received before this step.
4409 // If that is not the case, the id remap will be ignored and user may see the
4410 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004411 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004412 pendingInfo.spanX = item.spanX;
4413 pendingInfo.spanY = item.spanY;
4414 pendingInfo.minSpanX = item.minSpanX;
4415 pendingInfo.minSpanY = item.minSpanY;
4416 Bundle options =
4417 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4418
Sunny Goyalff572272014-07-23 13:58:07 -07004419 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004420 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4421 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004422
4423 // TODO consider showing a permission dialog when the widget is clicked.
4424 if (!success) {
4425 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4426 if (DEBUG_WIDGETS) {
4427 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4428 + " belongs to component " + item.providerName
4429 + ", as the launcher is unable to bing a new widget id");
4430 }
4431 LauncherModel.deleteItemFromDatabase(this, item);
4432 return;
4433 }
4434
4435 item.appWidgetId = newWidgetId;
4436
4437 // If the widget has a configure activity, it is still needs to set it up, otherwise
4438 // the widget is ready to go.
4439 item.restoreStatus = (appWidgetInfo.configure == null)
4440 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4441 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4442
4443 LauncherModel.updateItemInDatabase(this, item);
4444 }
4445
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004446 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004447 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004448 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004449 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4450 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004451 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004452
Sunny Goyal651077b2014-06-30 14:15:31 -07004453 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4454 } else {
4455 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004456 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4457 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004458 view.updateIcon(mIconCache);
4459 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004460 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004461 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004463
Joe Onorato9c1289c2009-08-17 11:03:03 -04004464 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004465 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004466
Adam Cohendcd297f2013-06-18 13:13:40 -07004467 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004468 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004469 if (!item.isCustomWidget()) {
4470 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4471 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004472
Joe Onorato9c1289c2009-08-17 11:03:03 -04004473 workspace.requestLayout();
4474
Daniel Sandler843e8602010-06-07 14:59:01 -04004475 if (DEBUG_WIDGETS) {
4476 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4477 + (SystemClock.uptimeMillis()-start) + "ms");
4478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004479 }
4480
Sunny Goyalff572272014-07-23 13:58:07 -07004481 /**
4482 * Restores a pending widget.
4483 *
4484 * @param appWidgetId The app widget id
4485 * @param cellInfo The position on screen where to create the widget.
4486 */
4487 private void completeRestoreAppWidget(final int appWidgetId) {
4488 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4489 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4490 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4491 return;
4492 }
4493
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004494 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004495 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4496
4497 mWorkspace.reinflateWidgetsIfNecessary();
4498 LauncherModel.updateItemInDatabase(this, info);
4499 }
4500
Adam Cohen1462de32012-07-24 22:34:36 -07004501 public void onPageBoundSynchronously(int page) {
4502 mSynchronouslyBoundPages.add(page);
4503 }
4504
Joe Onorato9c1289c2009-08-17 11:03:03 -04004505 /**
4506 * Callback saying that there aren't any more items to bind.
4507 *
4508 * Implementation of the method from LauncherModel.Callbacks.
4509 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004510 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004511 Runnable r = new Runnable() {
4512 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004513 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004514 }
4515 };
4516 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004517 return;
4518 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004519 if (mSavedState != null) {
4520 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004521 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004523 mSavedState = null;
4524 }
4525
Adam Cohen1462de32012-07-24 22:34:36 -07004526 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004527
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004528 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004529 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004530
4531 // If we received the result of any pending adds while the loader was running (e.g. the
4532 // widget configuration forced an orientation change), process them now.
4533 if (sPendingAddItem != null) {
4534 final long screenId = completeAdd(sPendingAddItem);
4535
4536 // TODO: this moves the user to the page where the pending item was added. Ideally,
4537 // the screen would be guaranteed to exist after bind, and the page would be set through
4538 // the workspace restore process.
4539 mWorkspace.post(new Runnable() {
4540 @Override
4541 public void run() {
4542 mWorkspace.snapToScreenId(screenId);
4543 }
4544 });
4545 sPendingAddItem = null;
4546 }
4547
Sunny Goyale755d462014-07-22 13:48:29 -07004548 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004549
4550 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004551 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004552 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004553 }
4554
Adam Cohen3ed316a2014-07-23 18:21:20 -07004555 private void sendLoadingCompleteBroadcastIfNecessary() {
4556 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4557 String permission =
4558 getResources().getString(R.string.receive_first_load_broadcast_permission);
4559 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4560 sendBroadcast(intent, permission);
4561 SharedPreferences.Editor editor = mSharedPrefs.edit();
4562 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4563 editor.apply();
4564 }
4565 }
4566
Winson Chunga0b7e862013-09-05 16:03:15 -07004567 public boolean isAllAppsButtonRank(int rank) {
4568 if (mHotseat != null) {
4569 return mHotseat.isAllAppsButtonRank(rank);
4570 }
4571 return false;
4572 }
4573
Winson Chunga2413752012-04-03 14:22:34 -07004574 private boolean canRunNewAppsAnimation() {
4575 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4576 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4577 }
4578
Winson Chungc9168342013-06-26 14:54:55 -07004579 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4580 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4581 PropertyValuesHolder.ofFloat("alpha", 1f),
4582 PropertyValuesHolder.ofFloat("scaleX", 1f),
4583 PropertyValuesHolder.ofFloat("scaleY", 1f));
4584 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4585 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4586 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4587 return bounceAnim;
4588 }
4589
Adam Cohen27772732013-11-11 14:00:56 +00004590 public boolean useVerticalBarLayout() {
4591 return LauncherAppState.getInstance().getDynamicGrid().
4592 getDeviceProfile().isVerticalBarLayout();
4593 }
4594
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004595 protected Rect getSearchBarBounds() {
4596 return LauncherAppState.getInstance().getDynamicGrid().
4597 getDeviceProfile().getSearchBarBounds();
4598 }
4599
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004600 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004601 if (mSearchDropTargetBar == null) {
4602 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004603 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004604 if (mQsb != null) {
4605 mSearchDropTargetBar.removeView(mQsb);
4606 mQsb = null;
4607 }
4608 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004609 }
4610
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004611 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004612 * Add the icons for all apps.
4613 *
4614 * Implementation of the method from LauncherModel.Callbacks.
4615 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004616 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004617 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004618 if (mAppsCustomizeContent != null) {
4619 mAppsCustomizeContent.onPackagesUpdated(
4620 LauncherModel.getSortedWidgetsAndShortcuts(this));
4621 }
Winson Chungc58497e2013-09-03 17:48:37 -07004622 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004623 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004624 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004625 mAppsCustomizeContent.onPackagesUpdated(
4626 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004627 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004628 }
Adam Cohen9211d422014-10-07 18:14:53 -07004629 if (mLauncherCallbacks != null) {
4630 mLauncherCallbacks.bindAllApplications(apps);
4631 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004632 }
4633
4634 /**
4635 * A package was updated.
4636 *
4637 * Implementation of the method from LauncherModel.Callbacks.
4638 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004639 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004640 Runnable r = new Runnable() {
4641 public void run() {
4642 bindAppsUpdated(apps);
4643 }
4644 };
4645 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004646 return;
4647 }
4648
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004649 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004650 mAppsCustomizeContent != null) {
4651 mAppsCustomizeContent.updateApps(apps);
4652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004653 }
4654
Sunny Goyal4390ace2014-10-13 11:33:11 -07004655 @Override
4656 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4657 Runnable r = new Runnable() {
4658 public void run() {
4659 bindWidgetsRestored(widgets);
4660 }
4661 };
4662 if (waitUntilResume(r)) {
4663 return;
4664 }
4665 mWorkspace.widgetsRestored(widgets);
4666 }
4667
Joe Onorato9c1289c2009-08-17 11:03:03 -04004668 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004669 * Some shortcuts were updated in the background.
4670 *
4671 * Implementation of the method from LauncherModel.Callbacks.
4672 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004673 @Override
4674 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4675 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004676 Runnable r = new Runnable() {
4677 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004678 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004679 }
4680 };
4681 if (waitUntilResume(r)) {
4682 return;
4683 }
4684
Sunny Goyal4390ace2014-10-13 11:33:11 -07004685 if (!updated.isEmpty()) {
4686 mWorkspace.updateShortcuts(updated);
4687 }
4688
4689 if (!removed.isEmpty()) {
4690 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4691 for (ShortcutInfo si : removed) {
4692 removedComponents.add(si.getTargetComponent());
4693 }
4694 mWorkspace.removeItemsByComponentName(removedComponents, user);
4695 // Notify the drag controller
4696 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004697 }
4698 }
4699
4700 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004701 * Update the state of a package, typically related to install state.
4702 *
4703 * Implementation of the method from LauncherModel.Callbacks.
4704 */
Sunny Goyale755d462014-07-22 13:48:29 -07004705 @Override
4706 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004707 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004708 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004709 }
4710 }
4711
4712 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004713 * Update the label and icon of all the icons in a package
4714 *
4715 * Implementation of the method from LauncherModel.Callbacks.
4716 */
4717 @Override
4718 public void updatePackageBadge(String packageName) {
4719 if (mWorkspace != null) {
4720 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4721 }
4722 }
4723
4724 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004725 * A package was uninstalled. We take both the super set of packageNames
4726 * in addition to specific applications to remove, the reason being that
4727 * this can be called when a package is updated as well. In that scenario,
4728 * we only remove specific components from the workspace, where as
4729 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004730 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004731 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004732 * Implementation of the method from LauncherModel.Callbacks.
4733 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004734 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004735 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004736 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004737 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004738 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004739 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004740 }
Winson Chungd64d1762013-08-20 14:37:16 -07004741 };
4742 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004743 return;
4744 }
4745
Sunny Goyal1a745e82014-10-02 15:58:31 -07004746 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004747 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4748 for (AppInfo info : appInfos) {
4749 removedComponents.add(info.componentName);
4750 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004751 if (!packageNames.isEmpty()) {
4752 mWorkspace.removeItemsByPackageName(packageNames, user);
4753 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004754 if (!removedComponents.isEmpty()) {
4755 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004756 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004757 // Notify the drag controller
4758 mDragController.onAppsRemoved(packageNames, removedComponents);
4759
Sunny Goyal1a745e82014-10-02 15:58:31 -07004760 } else {
4761 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004762 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004763
Winson Chungdf95eb12013-10-16 14:57:07 -07004764 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004765 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004766 mAppsCustomizeContent != null) {
4767 mAppsCustomizeContent.removeApps(appInfos);
4768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004769 }
Joe Onoratobe386092009-11-17 17:32:16 -08004770
4771 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004772 * A number of packages were updated.
4773 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004774 private ArrayList<Object> mWidgetsAndShortcuts;
4775 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004776 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004777 bindPackagesUpdated(mWidgetsAndShortcuts);
4778 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004779 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004780 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004781 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4782 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4783 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004784 return;
4785 }
4786
Winson Chung64359a52013-07-08 17:17:08 -07004787 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004788 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004789 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004790 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004791 }
4792
Winson Chung400438b2011-01-16 17:53:48 -08004793 private int mapConfigurationOriActivityInfoOri(int configOri) {
4794 final Display d = getWindowManager().getDefaultDisplay();
4795 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4796 switch (d.getRotation()) {
4797 case Surface.ROTATION_0:
4798 case Surface.ROTATION_180:
4799 // We are currently in the same basic orientation as the natural orientation
4800 naturalOri = configOri;
4801 break;
4802 case Surface.ROTATION_90:
4803 case Surface.ROTATION_270:
4804 // We are currently in the other basic orientation to the natural orientation
4805 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4806 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4807 break;
4808 }
4809
4810 int[] oriMap = {
4811 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4812 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4813 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4814 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4815 };
4816 // Since the map starts at portrait, we need to offset if this device's natural orientation
4817 // is landscape.
4818 int indexOffset = 0;
4819 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4820 indexOffset = 1;
4821 }
4822 return oriMap[(d.getRotation() + indexOffset) % 4];
4823 }
Adam Cohen446e9402011-09-15 18:21:21 -07004824
Winson Chung4b919f82012-05-01 10:44:08 -07004825 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004826 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004827 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4828 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4829 .getConfiguration().orientation));
4830 } else {
4831 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4832 }
Winson Chung4b919f82012-05-01 10:44:08 -07004833 }
4834 }
4835 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004836 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004837 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004838 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004839 } else {
4840 mHandler.postDelayed(new Runnable() {
4841 public void run() {
4842 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4843 }
4844 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004845 }
Winson Chung4b919f82012-05-01 10:44:08 -07004846 }
Winson Chung400438b2011-01-16 17:53:48 -08004847 }
4848
Winson Chunge43a1e72014-01-15 10:33:02 -08004849 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004850 if (mLauncherCallbacks != null) {
4851 return mLauncherCallbacks.isLauncherPreinstalled();
4852 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004853 PackageManager pm = getPackageManager();
4854 try {
4855 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4856 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4857 return true;
4858 } else {
4859 return false;
4860 }
4861 } catch (NameNotFoundException e) {
4862 e.printStackTrace();
4863 return false;
4864 }
4865 }
4866
Adam Cohenea90f832014-05-21 15:03:34 -07004867 /**
4868 * This method indicates whether or not we should suggest default wallpaper dimensions
4869 * when our wallpaper cropper was not yet used to set a wallpaper.
4870 */
4871 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004872 if (mLauncherCallbacks != null) {
4873 return mLauncherCallbacks.overrideWallpaperDimensions();
4874 }
Adam Cohenea90f832014-05-21 15:03:34 -07004875 return true;
4876 }
4877
Adam Cohen432609a2014-03-13 17:03:22 -07004878 /**
4879 * To be overridden by subclasses to indicate that there is an activity to launch
4880 * before showing the standard launcher experience.
4881 */
4882 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004883 if (mLauncherCallbacks != null) {
4884 return mLauncherCallbacks.hasFirstRunActivity();
4885 }
Adam Cohen432609a2014-03-13 17:03:22 -07004886 return false;
4887 }
4888
4889 /**
4890 * To be overridden by subclasses to launch any first run activity
4891 */
4892 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004893 if (mLauncherCallbacks != null) {
4894 return mLauncherCallbacks.getFirstRunActivity();
4895 }
Adam Cohen432609a2014-03-13 17:03:22 -07004896 return null;
4897 }
4898
Winson Chunga6945242014-01-08 14:04:34 -08004899 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004900 return !ActivityManager.isRunningInTestHarness() &&
4901 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004902 }
4903
Adam Cohen4a9423d2014-03-28 14:22:31 -07004904 protected boolean hasRunFirstRunActivity() {
4905 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4906 }
4907
Adam Cohen432609a2014-03-13 17:03:22 -07004908 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004909 if (shouldRunFirstRunActivity() &&
4910 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004911 Intent firstRunIntent = getFirstRunActivity();
4912 if (firstRunIntent != null) {
4913 startActivity(firstRunIntent);
4914 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004915 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004916 }
4917 }
Adam Cohen432609a2014-03-13 17:03:22 -07004918 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004919 }
4920
4921 private void markFirstRunActivityShown() {
4922 SharedPreferences.Editor editor = mSharedPrefs.edit();
4923 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4924 editor.apply();
4925 }
4926
Adam Cohen432609a2014-03-13 17:03:22 -07004927 /**
4928 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4929 * screen that must be displayed and dismissed.
4930 */
4931 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004932 if (mLauncherCallbacks != null) {
4933 return mLauncherCallbacks.hasDismissableIntroScreen();
4934 }
Adam Cohen432609a2014-03-13 17:03:22 -07004935 return false;
4936 }
4937
4938 /**
4939 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4940 */
4941 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004942 if (mLauncherCallbacks != null) {
4943 return mLauncherCallbacks.getIntroScreen();
4944 }
Adam Cohen432609a2014-03-13 17:03:22 -07004945 return null;
4946 }
4947
4948 /**
4949 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4950 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4951 */
4952 private boolean shouldShowIntroScreen() {
4953 return hasDismissableIntroScreen() &&
4954 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4955 }
4956
4957 protected void showIntroScreen() {
4958 View introScreen = getIntroScreen();
4959 changeWallpaperVisiblity(false);
4960 if (introScreen != null) {
4961 mDragLayer.showOverlayView(introScreen);
4962 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004963 if (mLauncherOverlayContainer != null) {
4964 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4965 }
Adam Cohen432609a2014-03-13 17:03:22 -07004966 }
4967
4968 public void dismissIntroScreen() {
4969 markIntroScreenDismissed();
4970 if (showFirstRunActivity()) {
4971 // We delay hiding the intro view until the first run activity is showing. This
4972 // avoids a blip.
4973 mWorkspace.postDelayed(new Runnable() {
4974 @Override
4975 public void run() {
4976 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004977 if (mLauncherOverlayContainer != null) {
4978 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4979 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004980 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004981 }
4982 }, ACTIVITY_START_DELAY);
4983 } else {
4984 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004985 if (mLauncherOverlayContainer != null) {
4986 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4987 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004988 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004989 }
4990 changeWallpaperVisiblity(true);
4991 }
4992
4993 private void markIntroScreenDismissed() {
4994 SharedPreferences.Editor editor = mSharedPrefs.edit();
4995 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4996 editor.apply();
4997 }
4998
Sunny Goyal88d60f12014-09-08 03:47:29 -07004999 private void showFirstRunClings() {
5000 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5001 // on the device, then we always show the first run cling experience (or if there is no
5002 // launcher2). Otherwise, we prompt the user upon started for migration
5003 LauncherClings launcherClings = new LauncherClings(this);
5004 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5005 if (mModel.canMigrateFromOldLauncherDb(this)) {
5006 launcherClings.showMigrationCling();
5007 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005008 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005009 }
5010 }
5011 }
5012
Winson Chunga6945242014-01-08 14:04:34 -08005013 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005014 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5015 if (mHotseat != null) mHotseat.setAlpha(1f);
5016 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5017 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005018 }
5019
5020 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005021 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5022 if (mHotseat != null) mHotseat.setAlpha(0f);
5023 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5024 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005025 }
5026
Mathew Inwood72fbec12013-11-19 15:45:07 +00005027 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005028 // Called from search suggestion, not supported in other profiles.
5029 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5030 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5031 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5032 myUser);
5033 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005034 return null;
5035 }
Kenny Guyed131872014-04-30 03:02:21 +01005036 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005037 }
5038
5039 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5040 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005041 // Called from search suggestion, not supported in other profiles.
5042 return createShortcutDragInfo(shortcutIntent, caption, icon,
5043 UserHandleCompat.myUserHandle());
5044 }
5045
5046 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5047 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005048 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005049 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005050 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005051 }
5052
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005053 protected void moveWorkspaceToDefaultScreen() {
5054 mWorkspace.moveToDefaultScreen(false);
5055 }
5056
Mathew Inwood72fbec12013-11-19 15:45:07 +00005057 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5058 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005059 mWorkspace.onExternalDragStartedWithItem(dragView);
5060 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005061 }
5062
Anjali Koppalf5d29132014-02-28 13:33:27 -08005063 @Override
5064 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005065 if (mLauncherCallbacks != null) {
5066 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5067 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005068 }
5069
Winson Chung80baf5a2010-08-09 16:03:15 -07005070 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005071 * Prints out out state for debugging.
5072 */
5073 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005074 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005075 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005076 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5077 Log.d(TAG, "mRestoring=" + mRestoring);
5078 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5079 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005080 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005081 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005082
Winson Chung785d2eb2011-04-14 16:08:02 -07005083 if (mAppsCustomizeContent != null) {
5084 mAppsCustomizeContent.dumpState();
5085 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005086 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005087 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005088
5089 @Override
5090 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5091 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005092 synchronized (sDumpLogs) {
5093 writer.println(" ");
5094 writer.println("Debug logs: ");
5095 for (int i = 0; i < sDumpLogs.size(); i++) {
5096 writer.println(" " + sDumpLogs.get(i));
5097 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005098 }
Adam Cohen9211d422014-10-07 18:14:53 -07005099 if (mLauncherCallbacks != null) {
5100 mLauncherCallbacks.dump(prefix, fd, writer, args);
5101 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005102 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005103
5104 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005105 if (DEBUG_DUMP_LOG) {
5106 synchronized (sDumpLogs) {
5107 Log.d(TAG, "");
5108 Log.d(TAG, "*********************");
5109 Log.d(TAG, "Launcher debug logs: ");
5110 for (int i = 0; i < sDumpLogs.size(); i++) {
5111 Log.d(TAG, " " + sDumpLogs.get(i));
5112 }
5113 Log.d(TAG, "*********************");
5114 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005115 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005116 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005117 }
5118
5119 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005120 addDumpLog(tag, log, null, debugLog);
5121 }
5122
5123 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005124 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005125 if (e != null) {
5126 Log.d(tag, log, e);
5127 } else {
5128 Log.d(tag, log);
5129 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005130 }
Winson Chungede41292013-09-19 16:27:36 -07005131 if (DEBUG_DUMP_LOG) {
5132 sDateStamp.setTime(System.currentTimeMillis());
5133 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005134 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5135 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005136 }
Winson Chungede41292013-09-19 16:27:36 -07005137 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005138 }
5139
Adam Cohen59400422014-03-05 18:07:04 -08005140 public static CustomAppWidget getCustomAppWidget(String name) {
5141 return sCustomAppWidgets.get(name);
5142 }
5143
5144 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5145 return sCustomAppWidgets;
5146 }
5147
Winson Chungede41292013-09-19 16:27:36 -07005148 public void dumpLogsToLocalData() {
5149 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005150 new AsyncTask<Void, Void, Void>() {
5151 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005152 boolean success = false;
5153 sDateStamp.setTime(sRunStart);
5154 String FILENAME = sDateStamp.getMonth() + "-"
5155 + sDateStamp.getDay() + "_"
5156 + sDateStamp.getHours() + "-"
5157 + sDateStamp.getMinutes() + "_"
5158 + sDateStamp.getSeconds() + ".txt";
5159
5160 FileOutputStream fos = null;
5161 File outFile = null;
5162 try {
5163 outFile = new File(getFilesDir(), FILENAME);
5164 outFile.createNewFile();
5165 fos = new FileOutputStream(outFile);
5166 } catch (Exception e) {
5167 e.printStackTrace();
5168 }
5169 if (fos != null) {
5170 PrintWriter writer = new PrintWriter(fos);
5171
5172 writer.println(" ");
5173 writer.println("Debug logs: ");
5174 synchronized (sDumpLogs) {
5175 for (int i = 0; i < sDumpLogs.size(); i++) {
5176 writer.println(" " + sDumpLogs.get(i));
5177 }
5178 }
5179 writer.close();
5180 }
5181 try {
5182 if (fos != null) {
5183 fos.close();
5184 success = true;
5185 }
5186 } catch (IOException e) {
5187 e.printStackTrace();
5188 }
Michael Jurka43467462013-11-14 12:03:58 +01005189 return null;
Winson Chungede41292013-09-19 16:27:36 -07005190 }
Michael Jurka43467462013-11-14 12:03:58 +01005191 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005192 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005194}
Michael Jurka2763be32011-02-24 11:19:57 -08005195
Michael Jurkaabded662011-03-04 12:06:57 -08005196interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005197 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005198 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005199 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005200 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005201 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005202}
Dan Sandlerd5024042014-01-09 15:01:33 -05005203
5204interface DebugIntents {
5205 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5206 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005207}