blob: 5757cb7eeb6762842879eceeff74e056968f9cc6 [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
100import com.android.launcher3.compat.UserHandleCompat;
101import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400102import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800103import com.android.launcher3.PagedView.PageSwitchListener;
Romain Guyedcce092010-03-04 13:03:17 -0800104
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.DataInputStream;
106import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700118import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000119import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120
Winson Chunga6945242014-01-08 14:04:34 -0800121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122/**
123 * Default launcher application.
124 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100125public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700126 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700127 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800128 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Daniel Sandlerf061f822013-06-27 13:59:36 -0400131 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700133 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700135 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700136
Dan Sandlerd5024042014-01-09 15:01:33 -0500137 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700143 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Michael Jurka8b805b12012-04-18 14:23:14 -0700145 private static final int REQUEST_BIND_APPWIDGET = 11;
146
Mathew Inwood876a8462013-06-14 14:12:41 +0100147 /**
148 * IntentStarter uses request codes starting with this. This must be greater than all activity
149 * request codes used internally.
150 */
151 protected static final int REQUEST_LAST = 100;
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
154
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800155 static final int SCREEN_COUNT = 5;
156 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Romain Guy98d01652009-06-30 16:21:04 -0700158 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800159 // To turn on these properties, type
160 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
161 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
162 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800163 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung2672ff92012-05-04 16:22:30 -0700165 // The Intent extra that defines whether to ignore the launch animation
166 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400167 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
170 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700171 // Type: int
172 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
175 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: boolean
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
183 // Type: long
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: int[]
194 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohenb54a5982014-01-08 15:21:04 -0800196
Adam Cohen432609a2014-03-13 17:03:22 -0700197 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800198 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
199
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700201 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100202 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700203 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100204 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Winson Chunga6945242014-01-08 14:04:34 -0800209 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
210
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700212 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700213 private State mState = State.WORKSPACE;
214 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700217 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800218 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700219 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700220 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800223 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000225 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
226 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
227
Winson Chunga2413752012-04-03 14:22:34 -0700228 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700229 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
230 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700231 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700232
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800233 private final BroadcastReceiver mCloseSystemDialogsReceiver
234 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800235 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private LayoutInflater mInflater;
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700240 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800241 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800242 private DragLayer mDragLayer;
243 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700244 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800245 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700246
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700247 private AppWidgetManager mAppWidgetManager;
248 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700249
Michael Jurkac9d95c52011-08-29 14:03:34 -0700250 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700251 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800252 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700253
Michael Jurka0280c3b2010-09-17 15:00:07 -0700254 private int[] mTmpAddItemCellCoordinates = new int[2];
255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private FolderInfo mFolderInfo;
257
Winson Chung3d503fb2011-07-13 17:25:49 -0700258 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800259 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700260
Michael Jurka838a4ca2011-02-07 13:33:06 -0800261 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700262
Winson Chungc51db6a2011-10-05 11:44:49 -0700263 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 private AppsCustomizeTabHost mAppsCustomizeTabHost;
265 private AppsCustomizePagedView mAppsCustomizeContent;
266 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800267 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700270 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
271 // scroll issues (because the workspace may not have been measured yet) and extra work.
272 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
273 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
275 private SpannableStringBuilder mDefaultKeySsb = null;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 private boolean mWorkspaceLoading = true;
278
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800279 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private boolean mRestoring;
281 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700282 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Michael Jurka1e2f4652013-07-08 18:03:46 -0700284 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700285 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
286
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // Keep track of whether the user has left launcher
288 private static boolean sPausedFromUserAction = false;
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private Bundle mSavedInstanceState;
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800294 private boolean mUserPresent = true;
295 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700296 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700299 private static LocaleConfiguration sLocaleConfiguration = null;
300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
307 private final int mAdvanceInterval = 20000;
308 private final int mAdvanceStagger = 250;
309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
311 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
312 new HashMap<View, AppWidgetProviderInfo>();
313
Winson Chung400438b2011-01-16 17:53:48 -0800314 // Determines how long to wait after a rotation before restoring the screen orientation to
315 // match the sensor state.
316 private final int mRestoreScreenOrientationDelay = 500;
317
Michael Jurka4ef207b2010-11-29 17:05:45 -0800318 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700319 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
320 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
321 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800322
Winson Chungd64a6662013-09-30 11:06:59 -0700323 private Intent mAppMarketIntent = null;
324 private static final boolean DISABLE_MARKET_BUTTON = true;
325
Craig Mautner360310b2012-10-26 15:13:08 -0700326 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700327
Adam Cohen1462de32012-07-24 22:34:36 -0700328 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700329 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700330
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700332 static Date sDateStamp = new Date();
333 static DateFormat sDateFormat =
334 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
335 static long sRunStart = System.currentTimeMillis();
336 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337
Winson Chung46353de2012-02-16 14:05:10 -0800338 // We only want to get the SharedPreferences once since it does an FS stat each time we get
339 // it from the context.
340 private SharedPreferences mSharedPrefs;
341
Adam Cohene25af792013-06-06 23:08:25 -0700342 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
343
Winson Chungf0c6ae02012-03-21 16:10:31 -0700344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700346 private ImageView mFolderIconImageView;
347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700350
Michael Jurkaddd62e92011-02-16 17:49:14 -0800351 private BubbleTextView mWaitingForResume;
352
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 private Runnable mBuildLayersRunnable = new Runnable() {
354 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700355 if (mWorkspace != null) {
356 mWorkspace.buildPageHardwareLayers();
357 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 }
359 };
360
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 private static ArrayList<PendingAddArguments> sPendingAddList
362 = new ArrayList<PendingAddArguments>();
363
Michael Jurka7267fa52013-09-26 15:29:57 -0700364 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800365
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 private static class PendingAddArguments {
367 int requestCode;
368 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700369 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700370 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 int cellX;
372 int cellY;
373 }
374
Daniel Sandlerff02d492013-08-05 02:12:05 -0400375 private Stats mStats;
376
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800377 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800378 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 @Override
382 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700383 if (DEBUG_STRICT_MODE) {
384 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
385 .detectDiskReads()
386 .detectDiskWrites()
387 .detectNetwork() // or .detectAll() for all detectable problems
388 .penaltyLog()
389 .build());
390 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
391 .detectLeakedSqlLiteObjects()
392 .detectLeakedClosableObjects()
393 .penaltyLog()
394 .penaltyDeath()
395 .build());
396 }
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400399
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700402 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700403 // Determine the dynamic grid properties
404 Point smallestSize = new Point();
405 Point largestSize = new Point();
406 Point realSize = new Point();
407 Display display = getWindowManager().getDefaultDisplay();
408 display.getCurrentSizeRange(smallestSize, largestSize);
409 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700410 DisplayMetrics dm = new DisplayMetrics();
411 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Winson Chung5f8afe62013-08-12 16:19:28 -0700413 // Lazy-initialize the dynamic grid
414 DeviceProfile grid = app.initDynamicGrid(this,
415 Math.min(smallestSize.x, smallestSize.y),
416 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700417 realSize.x, realSize.y,
418 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700419
420 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700422 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700425 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400426 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800427 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
435 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700437 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
438 // this also ensures that any synchronous binding below doesn't re-trigger another
439 // LauncherModel load.
440 mPaused = false;
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200443 android.os.Debug.startMethodTracing(
444 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
Adam Cohen53805212013-10-01 10:39:23 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100452 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800454 registerContentObservers();
455
Joe Onorato7c312c12009-08-13 21:36:53 -0700456 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 mSavedState = savedInstanceState;
459 restoreState(mSavedState);
460
461 if (PROFILE_STARTUP) {
462 android.os.Debug.stopMethodTracing();
463 }
464
465 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700466 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 // If the user leaves launcher, then we should just load items asynchronously when
468 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500469 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 } else {
471 // We only load the page synchronously if the user rotates (or triggers a
472 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800473 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476
477 // For handling default keys
478 mDefaultKeySsb = new SpannableStringBuilder();
479 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800480
481 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
482 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800483
Adam Cohenf9426d52012-06-04 17:26:21 -0700484 updateGlobalIcons();
485
486 // On large interfaces, we want the screen to auto-rotate based on the current orientation
487 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700488
Adam Cohen432609a2014-03-13 17:03:22 -0700489 if (shouldShowIntroScreen()) {
490 showIntroScreen();
491 } else {
492 showFirstRunActivity();
493 }
494
Winson Chunge43a1e72014-01-15 10:33:02 -0800495 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
496 // on the device, then we always show the first run cling experience (or if there is no
497 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800498 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
499 if (mModel.canMigrateFromOldLauncherDb(this)) {
500 mLauncherClings.showMigrationCling();
501 } else {
502 mLauncherClings.showFirstRunCling();
503 }
Winson Chunga6945242014-01-08 14:04:34 -0800504 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800505 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800506 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700507 }
508
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700509 @Override
510 public void onLauncherProviderChange() { }
511
Winson Chung4a2afa32012-07-19 14:53:05 -0700512 protected void onUserLeaveHint() {
513 super.onUserLeaveHint();
514 sPausedFromUserAction = true;
515 }
516
Winson Chung98ca0f72013-07-29 12:58:51 -0700517 /** To be overriden by subclasses to hint to Launcher that we have custom content */
518 protected boolean hasCustomContentToLeft() {
519 return false;
520 }
521
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 * {@link #addToCustomContentPage}. This will only be invoked if
525 * {@link #hasCustomContentToLeft()} is {@code true}.
526 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 }
529
530 /**
531 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532 * ensure the custom content page is added or removed if necessary.
533 */
534 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600535 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 // Not bound yet, wait for bindScreens to be called.
537 return;
538 }
539
540 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 mWorkspace.createCustomContentContainer();
543 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545 mWorkspace.removeCustomContentPage();
546 }
547 }
548
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 boolean searchVisible = false;
551 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800552 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 int coi = getCurrentOrientationIndexForGlobalIcons();
554 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700556 if (!DISABLE_MARKET_BUTTON) {
557 updateAppMarketIcon();
558 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700559 searchVisible = updateGlobalSearchIcon();
560 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sGlobalSearchIcon[coi] != null) {
563 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700566 if (sVoiceSearchIcon[coi] != null) {
567 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700568 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 }
Winson Chungd64a6662013-09-30 11:06:59 -0700570 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 }
Winson Chungadf0c182012-08-23 14:59:07 -0700573 if (mSearchDropTargetBar != null) {
574 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 if (sLocaleConfiguration == null) {
580 new AsyncTask<Void, Void, LocaleConfiguration>() {
581 @Override
582 protected LocaleConfiguration doInBackground(Void... unused) {
583 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584 readConfiguration(Launcher.this, localeConfiguration);
585 return localeConfiguration;
586 }
587
588 @Override
589 protected void onPostExecute(LocaleConfiguration result) {
590 sLocaleConfiguration = result;
591 checkForLocaleChange(); // recursive, but now with a locale configuration
592 }
593 }.execute();
594 return;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final Configuration configuration = getResources().getConfiguration();
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final String locale = configuration.locale.toString();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final int mcc = configuration.mcc;
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mnc = configuration.mnc;
607
Romain Guy84f296c2009-11-04 15:00:44 -0800608 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 sLocaleConfiguration.locale = locale;
612 sLocaleConfiguration.mcc = mcc;
613 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700614
Joe Onorato0589f0f2010-02-08 13:44:00 -0800615 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616
617 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100618 new AsyncTask<Void, Void, Void>() {
619 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100621 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 }
Michael Jurka43467462013-11-14 12:03:58 +0100623 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700624 }
625 }
626
627 private static class LocaleConfiguration {
628 public String locale;
629 public int mcc = -1;
630 public int mnc = -1;
631 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700632
Romain Guy98d01652009-06-30 16:21:04 -0700633 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634 DataInputStream in = null;
635 try {
636 in = new DataInputStream(context.openFileInput(PREFERENCES));
637 configuration.locale = in.readUTF();
638 configuration.mcc = in.readInt();
639 configuration.mnc = in.readInt();
640 } catch (FileNotFoundException e) {
641 // Ignore
642 } catch (IOException e) {
643 // Ignore
644 } finally {
645 if (in != null) {
646 try {
647 in.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
652 }
653 }
654
655 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656 DataOutputStream out = null;
657 try {
658 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659 out.writeUTF(configuration.locale);
660 out.writeInt(configuration.mcc);
661 out.writeInt(configuration.mnc);
662 out.flush();
663 } catch (FileNotFoundException e) {
664 // Ignore
665 } catch (IOException e) {
666 //noinspection ResultOfMethodCallIgnored
667 context.getFileStreamPath(PREFERENCES).delete();
668 } finally {
669 if (out != null) {
670 try {
671 out.close();
672 } catch (IOException e) {
673 // Ignore
674 }
675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Winson Chung36a62fe2012-05-06 18:04:42 -0700687 boolean isDraggingEnabled() {
688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 static int getScreen() {
694 synchronized (sLock) {
695 return sScreen;
696 }
697 }
698
699 static void setScreen(int screen) {
700 synchronized (sLock) {
701 sScreen = screen;
702 }
703 }
704
Winson Chung557d6ed2011-07-08 15:34:52 -0700705 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 * Copied from View -- the View version of the method isn't called
707 * anywhere else in our process and only exists for API level 17+,
708 * so it's ok to keep our own version with no API requirement.
709 */
710 public static int generateViewId() {
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
719 }
720 }
721
722 public int getViewIdForItem(ItemInfo info) {
723 // This cast is safe given the > 2B range for int.
724 int itemId = (int) info.id;
725 if (mItemIdToViewId.containsKey(itemId)) {
726 return mItemIdToViewId.get(itemId);
727 }
728 int viewId = generateViewId();
729 mItemIdToViewId.put(itemId, viewId);
730 return viewId;
731 }
732
733 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735 * a configuration step, this allows the proper animations to run after other transitions.
736 */
737 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 case REQUEST_PICK_SHORTCUT:
741 processShortcut(args.intent);
742 break;
743 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700745 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 result = true;
747 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 case REQUEST_CREATE_APPWIDGET:
749 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 result = true;
752 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 }
Michael Jurka27614d22012-04-02 06:40:22 -0700754 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
755 // if you turned the screen off and then back while in All Apps, Launcher would not
756 // return to the workspace. Clearing mAddInfo.container here fixes this issue
757 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 protected void onActivityResult(
763 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700764 // Reset the startActivity waiting flag
765 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800766 int pendingAddWidgetId = mPendingAddWidgetId;
767 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 Runnable exitSpringLoaded = new Runnable() {
770 @Override
771 public void run() {
772 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
773 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
774 }
775 };
776
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
780 if (resultCode == RESULT_CANCELED) {
781 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
786 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 }
788 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
790 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
791 mWorkspace.exitOverviewMode(false);
792 }
793 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795
Adam Cohened66b2b2012-01-23 17:28:51 -0800796 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
797 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700798
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 // We have special handling for widgets
800 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800801 final int appWidgetId;
802 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
803 : -1;
804 if (widgetId < 0) {
805 appWidgetId = pendingAddWidgetId;
806 } else {
807 appWidgetId = widgetId;
808 }
809
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 final int result;
811 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800812 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700813 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
814 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 result = RESULT_CANCELED;
816 completeTwoStageWidgetDrop(result, appWidgetId);
817 onComplete = new Runnable() {
818 @Override
819 public void run() {
820 exitSpringLoadedDragModeDelayed(false, 0, null);
821 }
822 };
Winson Chung5aaab772012-04-27 15:24:02 -0700823 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700825 final CellLayout dropLayout =
826 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
827 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700832 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 }
834 };
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
837 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 return;
839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 // The pattern used here is that a user PICKs a specific application,
842 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
845 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700846 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800847 final PendingAddArguments args = new PendingAddArguments();
848 args.requestCode = requestCode;
849 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 args.cellX = mPendingAddInfo.cellX;
853 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
855 sPendingAddList.add(args);
856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700859 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700862 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
868 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700869 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 Runnable onCompleteRunnable = null;
872 int animationType = 0;
873
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 if (resultCode == RESULT_OK) {
876 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
877 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 mPendingAddWidgetInfo);
879 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 onCompleteRunnable = new Runnable() {
881 @Override
882 public void run() {
883 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700885 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
886 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 }
888 };
889 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800890 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700893 if (mDragLayer.getAnimatedView() != null) {
894 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
895 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
896 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700897 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 // The animated view may be null in the case of a rotation during widget configuration
899 onCompleteRunnable.run();
900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
903 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700904 protected void onStop() {
905 super.onStop();
906 FirstFrameAnimatorHelper.setIsVisible(false);
907 }
908
909 @Override
910 protected void onStart() {
911 super.onStart();
912 FirstFrameAnimatorHelper.setIsVisible(true);
913 }
914
915 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200917 long startTime = 0;
918 if (DEBUG_RESUME_TIME) {
919 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400920 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700923
Winson Chung4a2afa32012-07-19 14:53:05 -0700924 // Restore the previous launcher state
925 if (mOnResumeState == State.WORKSPACE) {
926 showWorkspace(false);
927 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800928 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 }
930 mOnResumeState = State.NONE;
931
Craig Mautner360310b2012-10-26 15:13:08 -0700932 // Background was set to gradient in onPause(), restore to black if in all apps.
933 setWorkspaceBackground(mState == State.WORKSPACE);
934
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800935 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700936 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700937 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500939 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700941 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700943 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200944 // We might have postponed some bind calls until onResume (see waitUntilResume) --
945 // execute them here
946 long startTimeCallbacks = 0;
947 if (DEBUG_RESUME_TIME) {
948 startTimeCallbacks = System.currentTimeMillis();
949 }
950
951 if (mAppsCustomizeContent != null) {
952 mAppsCustomizeContent.setBulkBind(true);
953 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700954 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
955 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200956 }
957 if (mAppsCustomizeContent != null) {
958 mAppsCustomizeContent.setBulkBind(false);
959 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700960 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200961 if (DEBUG_RESUME_TIME) {
962 Log.d(TAG, "Time spent processing callbacks in onResume: " +
963 (System.currentTimeMillis() - startTimeCallbacks));
964 }
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Michael Jurka54554252013-08-01 12:52:23 +0200966 if (mOnResumeCallbacks.size() > 0) {
967 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
968 mOnResumeCallbacks.get(i).run();
969 }
970 mOnResumeCallbacks.clear();
971 }
Winson Chunge4e50662012-01-23 14:45:13 -0800972
973 // Reset the pressed state of icons that were locked in the press state while activities
974 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800975 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800976 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800977 mWaitingForResume.setStayPressed(false);
978 }
Winson Chunge4e50662012-01-23 14:45:13 -0800979 if (mAppsCustomizeContent != null) {
980 // Resets the previous all apps icon press state
981 mAppsCustomizeContent.resetDrawableState();
982 }
Winson Chung82963d52013-10-09 11:20:57 -0700983
Adam Cohen06dff352012-06-01 17:17:08 -0700984 // It is possible that widgets can receive updates while launcher is not in the foreground.
985 // Consequently, the widgets will be inflated in the orientation of the foreground activity
986 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
987 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700988 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700989
Winson Chung780fe592013-09-26 14:48:44 -0700990 // Process any items that were added while Launcher was away.
991 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
992
Winson Chung5841efa2013-09-30 18:06:44 -0700993 // Update the voice search button proxy
994 updateVoiceButtonProxyVisible(false);
995
Adam Cohenf9426d52012-06-04 17:26:21 -0700996 // Again, as with the above scenario, it's possible that one or more of the global icons
997 // were updated in the wrong orientation.
998 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200999 if (DEBUG_RESUME_TIME) {
1000 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1001 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001002
1003 if (mWorkspace.getCustomContentCallbacks() != null) {
1004 // If we are resuming and the custom content is the current page, we call onShow().
1005 // It is also poassible that onShow will instead be called slightly after first layout
1006 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1007 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001008 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001009 }
1010 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001011 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001012 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001017 // Ensure that items added to Launcher are queued until Launcher returns
1018 InstallShortcutReceiver.enableInstallQueue();
1019
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001022 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001023 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001024
1025 // We call onHide() aggressively. The custom content callbacks should be able to
1026 // debounce excess onHide calls.
1027 if (mWorkspace.getCustomContentCallbacks() != null) {
1028 mWorkspace.getCustomContentCallbacks().onHide();
1029 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
Adam Cohenbffe7452013-07-22 18:21:45 -07001032 QSBScroller mQsbScroller = new QSBScroller() {
1033 int scrollY = 0;
1034
1035 @Override
1036 public void setScrollY(int scroll) {
1037 scrollY = scroll;
1038
1039 if (mWorkspace.isOnOrMovingToCustomContent()) {
1040 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001041 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001042 }
1043 }
1044 };
1045
1046 public void resetQSBScroll() {
1047 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001048 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001049 }
1050
1051 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1053 // by a onResume or by scrolling otherwise.
1054 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001055
1056 // Custom content is completely hidden
1057 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001058
1059 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1060 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001061 }
1062
Jorim Jaggid017f882014-01-14 17:08:48 -08001063 protected boolean hasSettings() {
1064 return false;
1065 }
1066
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 public interface QSBScroller {
1068 public void setScrollY(int scrollY);
1069 }
1070
Winson Chung98ca0f72013-07-29 12:58:51 -07001071 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001072 CustomContentCallbacks callbacks, String description) {
1073 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001074 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001075 }
1076
Adam Cohenedb40762013-07-18 16:45:45 -07001077 // The custom content needs to offset its content to account for the QSB
1078 public int getTopOffsetForCustomContent() {
1079 return mWorkspace.getPaddingTop();
1080 }
1081
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001082 @Override
1083 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 // Flag the loader to stop early before switching
1085 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001086 if (mAppsCustomizeContent != null) {
1087 mAppsCustomizeContent.surrender();
1088 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001089 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001090 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001092 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001093 @Override
1094 public void onWindowFocusChanged(boolean hasFocus) {
1095 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001096 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 private boolean acceptFilter() {
1100 final InputMethodManager inputManager = (InputMethodManager)
1101 getSystemService(Context.INPUT_METHOD_SERVICE);
1102 return !inputManager.isFullscreenMode();
1103 }
1104
1105 @Override
1106 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001107 final int uniChar = event.getUnicodeChar();
1108 final boolean handled = super.onKeyDown(keyCode, event);
1109 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1110 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1112 keyCode, event);
1113 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001114 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001115 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001116 // showSearchDialog()
1117 // If there are multiple keystrokes before the search dialog takes focus,
1118 // onSearchRequested() will be called for every keystroke,
1119 // but it is idempotent, so it's fine.
1120 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122 }
1123
Joe Onorato8a9625e2010-01-28 15:55:35 -08001124 // Eat the long press event so the keyboard doesn't come up.
1125 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1126 return true;
1127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 return handled;
1130 }
1131
Karl Rosaen138a0412009-04-23 19:00:21 -07001132 private String getTypedText() {
1133 return mDefaultKeySsb.toString();
1134 }
1135
1136 private void clearTypedText() {
1137 mDefaultKeySsb.clear();
1138 mDefaultKeySsb.clearSpans();
1139 Selection.setSelection(mDefaultKeySsb, 0);
1140 }
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001143 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1144 * State
1145 */
1146 private static State intToState(int stateOrdinal) {
1147 State state = State.WORKSPACE;
1148 final State[] stateValues = State.values();
1149 for (int i = 0; i < stateValues.length; i++) {
1150 if (stateValues[i].ordinal() == stateOrdinal) {
1151 state = stateValues[i];
1152 break;
1153 }
1154 }
1155 return state;
1156 }
1157
1158 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 * Restores the previous state, if it exists.
1160 *
1161 * @param savedState The previous state.
1162 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001163 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 private void restoreState(Bundle savedState) {
1165 if (savedState == null) {
1166 return;
1167 }
1168
Michael Jurka883f55b2010-10-21 15:47:14 -07001169 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001170 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001171 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001172 }
1173
Adam Cohen21cd0022013-10-09 18:57:02 -07001174 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1175 PagedView.INVALID_RESTORE_PAGE);
1176 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001177 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001181 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1184 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1187 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001188 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1189 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1190 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001191 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001192 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 mRestoring = true;
1194 }
1195
1196 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1197 if (renameFolder) {
1198 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001199 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 mRestoring = true;
1201 }
Winson Chunga12a2502010-12-20 14:41:35 -08001202
Winson Chung785d2eb2011-04-14 16:08:02 -07001203 // Restore the AppsCustomize tab
1204 if (mAppsCustomizeTabHost != null) {
1205 String curTab = savedState.getString("apps_customize_currentTab");
1206 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001207 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001208 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001209 mAppsCustomizeContent.loadAssociatedPages(
1210 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 }
1212
Winson Chung5afbf7b2011-07-25 11:53:08 -07001213 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1214 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001216 mItemIdToViewId = (HashMap<Integer, Integer>)
1217 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219
1220 /**
1221 * Finds all the views we need and configure them properly.
1222 */
1223 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001224 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001225
Craig Mautner360310b2012-10-26 15:13:08 -07001226 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001227 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1228 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001229 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001230 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001231
John Spurlock77e1f472013-09-11 10:09:51 -04001232 mLauncherView.setSystemUiVisibility(
1233 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001234 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235
Winson Chung4c98d922011-05-31 16:50:48 -07001236 // Setup the drag layer
1237 mDragLayer.setup(this, dragController);
1238
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 // Setup the hotseat
1240 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1241 if (mHotseat != null) {
1242 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001243 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 }
1245
Jorim Jaggid017f882014-01-14 17:08:48 -08001246 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001247 View widgetButton = findViewById(R.id.widget_button);
1248 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001249 @Override
1250 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001251 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001252 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001253 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001254 }
1255 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001256 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1257
1258 View wallpaperButton = findViewById(R.id.wallpaper_button);
1259 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 @Override
1261 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001262 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001263 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001264 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001265 }
1266 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001267 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1268
1269 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001270 if (hasSettings()) {
1271 settingsButton.setOnClickListener(new OnClickListener() {
1272 @Override
1273 public void onClick(View arg0) {
1274 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001275 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001276 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001277 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001278 });
1279 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1280 } else {
1281 settingsButton.setVisibility(View.GONE);
1282 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1283 lp.gravity = Gravity.END | Gravity.TOP;
1284 widgetButton.requestLayout();
1285 }
1286
Adam Cohendbdff6b2013-09-18 19:09:15 -07001287 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001288
Winson Chung4c98d922011-05-31 16:50:48 -07001289 // Setup the workspace
1290 mWorkspace.setHapticFeedbackEnabled(false);
1291 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001292 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001293 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001294
Winson Chungf0ea4d32011-06-06 14:27:16 -07001295 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001296 mSearchDropTargetBar = (SearchDropTargetBar)
1297 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001298
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001300 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 mAppsCustomizeContent = (AppsCustomizePagedView)
1302 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1303 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001304
Winson Chung3d503fb2011-07-13 17:25:49 -07001305 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001306 dragController.setDragScoller(mWorkspace);
1307 dragController.setScrollView(mDragLayer);
1308 dragController.setMoveTarget(mWorkspace);
1309 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001310 if (mSearchDropTargetBar != null) {
1311 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001312 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001313
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001314 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001315 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001316 mWeightWatcher = new WeightWatcher(this);
1317 mWeightWatcher.setAlpha(0.5f);
1318 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001319 new FrameLayout.LayoutParams(
1320 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001321 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001322 Gravity.BOTTOM)
1323 );
Adam Cohen39a06042013-07-19 14:30:12 -07001324
1325 boolean show = shouldShowWeightWatcher();
1326 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329
1330 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001331 * Sets the all apps button. This method is called from {@link Hotseat}.
1332 */
1333 public void setAllAppsButton(View allAppsButton) {
1334 mAllAppsButton = allAppsButton;
1335 }
1336
1337 public View getAllAppsButton() {
1338 return mAllAppsButton;
1339 }
1340
1341 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 * Creates a view representing a shortcut.
1343 *
1344 * @param info The data structure describing the shortcut.
1345 *
1346 * @return A View inflated from R.layout.application.
1347 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001348 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001350 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 /**
1354 * Creates a view representing a shortcut inflated from the specified resource.
1355 *
1356 * @param layoutResId The id of the XML layout used to create the shortcut.
1357 * @param parent The group the shortcut belongs to.
1358 * @param info The data structure describing the shortcut.
1359 *
1360 * @return A View inflated from layoutResId.
1361 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001362 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001363 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1364 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 return favorite;
1367 }
1368
1369 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 * Add a shortcut to the workspace.
1371 *
1372 * @param data The intent describing the shortcut.
1373 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001375 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001376 int cellY) {
1377 int[] cellXY = mTmpAddItemCellCoordinates;
1378 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001379 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001380
Michael Jurkad3ef3062010-11-23 16:23:58 -08001381 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001382
Adam Cohen558baaf2011-08-15 15:22:57 -07001383 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001384 if (info == null) {
1385 return;
1386 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001387 final View view = createShortcut(info);
1388
Adam Cohenfbba09b2011-07-18 21:43:05 -07001389 // First we check if we already know the exact location where we want to add this item.
1390 if (cellX >= 0 && cellY >= 0) {
1391 cellXY[0] = cellX;
1392 cellXY[1] = cellY;
1393 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001394
1395 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001396 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001397 true, null,null)) {
1398 return;
1399 }
1400 DragObject dragObject = new DragObject();
1401 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001402 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1403 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001404 return;
1405 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001407 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001408 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001409 foundCellSpan = (result != null);
1410 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001411 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001412 }
1413
1414 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001415 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001416 return;
1417 }
1418
Adam Cohendcd297f2013-06-18 13:13:40 -07001419 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420
1421 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001422 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001423 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425 }
1426
Adam Cohen2f093b62012-04-30 18:59:53 -07001427 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1428 int minHeight) {
1429 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001430 // We want to account for the extra amount of padding that we are adding to the widget
1431 // to ensure that it gets the full amount of space that it has requested
1432 int requiredWidth = minWidth + padding.left + padding.right;
1433 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001434 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001435 }
1436
Adam Cohen2f093b62012-04-30 18:59:53 -07001437 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1438 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001439 }
1440
Adam Cohen2f093b62012-04-30 18:59:53 -07001441 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1442 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001443 }
1444
Adam Cohen2f093b62012-04-30 18:59:53 -07001445 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1446 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001447 }
1448
Adam Cohen2f093b62012-04-30 18:59:53 -07001449 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1450 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001451 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001452 }
1453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001455 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001457 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001461 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1462 if (appWidgetInfo == null) {
1463 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1464 }
Romain Guycbb89e42009-06-08 15:52:54 -07001465
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001466 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001468
Adam Cohen2f093b62012-04-30 18:59:53 -07001469 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1470 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001473 // We have saved the position to which the widget was dragged-- this really only matters
1474 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 int[] cellXY = mTmpAddItemCellCoordinates;
1476 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001477 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1480 cellXY[0] = mPendingAddInfo.cellX;
1481 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001482 spanXY[0] = mPendingAddInfo.spanX;
1483 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 foundCellSpan = true;
1485 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001488 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1489 spanXY[1], cellXY, finalSpan);
1490 spanXY[0] = finalSpan[0];
1491 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001492 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001494 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001495 }
1496
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001498 if (appWidgetId != -1) {
1499 // Deleting an app widget ID is a void call but writes to disk before returning
1500 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001501 new AsyncTask<Void, Void, Void>() {
1502 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001503 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001504 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001505 }
Michael Jurka43467462013-11-14 12:03:58 +01001506 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001507 }
Winson Chung93eef082012-03-23 15:59:27 -07001508 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001509 return;
1510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001513 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1514 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 launcherInfo.spanX = spanXY[0];
1516 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001517 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1518 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 if (hostView == null) {
1525 // Perform actual inflation because we're live
1526 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1527 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1528 } else {
1529 // The AppWidgetHostView has already been inflated and instantiated
1530 launcherInfo.hostView = hostView;
1531 }
Romain Guycbb89e42009-06-08 15:52:54 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001534 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001535 launcherInfo.notifyWidgetSizeChanged(this);
1536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001539
1540 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001542 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Adam Cohended9f8d2010-11-03 13:25:16 -07001545 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1546 @Override
1547 public void onReceive(Context context, Intent intent) {
1548 final String action = intent.getAction();
1549 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1550 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001551 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001552 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001553
Winson Chungc100e8e2011-08-09 16:02:43 -07001554 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001555 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001556 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001557 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001558 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001559 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1560 mUserPresent = true;
1561 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001562 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1563 mModel.resetLoadedState(false, true);
1564 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1565 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1566 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1567 mModel.resetLoadedState(false, true);
1568 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1569 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1570 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001571 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1572 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1573 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001574 }
1575 }
1576 };
1577
1578 @Override
1579 public void onAttachedToWindow() {
1580 super.onAttachedToWindow();
1581
1582 // Listen for broadcasts related to user-presence
1583 final IntentFilter filter = new IntentFilter();
1584 filter.addAction(Intent.ACTION_SCREEN_OFF);
1585 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001586 // For handling managed profiles
1587 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1588 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 if (ENABLE_DEBUG_INTENTS) {
1590 filter.addAction(DebugIntents.DELETE_DATABASE);
1591 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1592 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001594 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001595 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 mVisible = true;
1597 }
1598
1599 @Override
1600 public void onDetachedFromWindow() {
1601 super.onDetachedFromWindow();
1602 mVisible = false;
1603
Adam Cohend113e0c2010-11-11 10:48:05 -08001604 if (mAttached) {
1605 unregisterReceiver(mReceiver);
1606 mAttached = false;
1607 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 updateRunning();
1609 }
1610
1611 public void onWindowVisibilityChanged(int visibility) {
1612 mVisible = visibility == View.VISIBLE;
1613 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001614 // The following code used to be in onResume, but it turns out onResume is called when
1615 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1616 // is a more appropriate event to handle
1617 if (mVisible) {
1618 mAppsCustomizeTabHost.onWindowVisible();
1619 if (!mWorkspaceLoading) {
1620 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001621 // We want to let Launcher draw itself at least once before we force it to build
1622 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001623 // apps is nice and speedy.
1624 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001625 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001627 if (mStarted) return;
1628 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001629 // We delay the layer building a bit in order to give
1630 // other message processing a time to run. In particular
1631 // this avoids a delay in hiding the IME if it was
1632 // currently shown, because doing that may involve
1633 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001634 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001635 final ViewTreeObserver.OnDrawListener listener = this;
1636 mWorkspace.post(new Runnable() {
1637 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001638 if (mWorkspace != null &&
1639 mWorkspace.getViewTreeObserver() != null) {
1640 mWorkspace.getViewTreeObserver().
1641 removeOnDrawListener(listener);
1642 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001643 }
1644 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 }
1647 });
1648 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001649 // When Launcher comes back to foreground, a different Activity might be responsible for
1650 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001651 if (!DISABLE_MARKET_BUTTON) {
1652 updateAppMarketIcon();
1653 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001654 clearTypedText();
1655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657
1658 private void sendAdvanceMessage(long delay) {
1659 mHandler.removeMessages(ADVANCE_MSG);
1660 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1661 mHandler.sendMessageDelayed(msg, delay);
1662 mAutoAdvanceSentTime = System.currentTimeMillis();
1663 }
1664
1665 private void updateRunning() {
1666 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1667 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1668 mAutoAdvanceRunning = autoAdvanceRunning;
1669 if (autoAdvanceRunning) {
1670 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1671 sendAdvanceMessage(delay);
1672 } else {
1673 if (!mWidgetsToAdvance.isEmpty()) {
1674 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1675 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1676 }
1677 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001678 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680 }
1681 }
1682
1683 private final Handler mHandler = new Handler() {
1684 @Override
1685 public void handleMessage(Message msg) {
1686 if (msg.what == ADVANCE_MSG) {
1687 int i = 0;
1688 for (View key: mWidgetsToAdvance.keySet()) {
1689 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1690 final int delay = mAdvanceStagger * i;
1691 if (v instanceof Advanceable) {
1692 postDelayed(new Runnable() {
1693 public void run() {
1694 ((Advanceable) v).advance();
1695 }
1696 }, delay);
1697 }
1698 i++;
1699 }
1700 sendAdvanceMessage(mAdvanceInterval);
1701 }
1702 }
1703 };
1704
1705 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001706 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1708 if (v instanceof Advanceable) {
1709 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001710 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 updateRunning();
1712 }
1713 }
1714
1715 void removeWidgetToAutoAdvance(View hostView) {
1716 if (mWidgetsToAdvance.containsKey(hostView)) {
1717 mWidgetsToAdvance.remove(hostView);
1718 updateRunning();
1719 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001720 }
1721
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724 launcherInfo.hostView = null;
1725 }
1726
Winson Chung93eef082012-03-23 15:59:27 -07001727 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1728 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1729 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 }
1731
Winson Chunga6945242014-01-08 14:04:34 -08001732 public DragLayer getDragLayer() {
1733 return mDragLayer;
1734 }
1735
1736 public Workspace getWorkspace() {
1737 return mWorkspace;
1738 }
1739
1740 public Hotseat getHotseat() {
1741 return mHotseat;
1742 }
1743
Jorim Jaggid017f882014-01-14 17:08:48 -08001744 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001745 return mOverviewPanel;
1746 }
1747
1748 public SearchDropTargetBar getSearchBar() {
1749 return mSearchDropTargetBar;
1750 }
1751
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001752 public LauncherAppWidgetHost getAppWidgetHost() {
1753 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Winson Chunga9abd0e2010-10-27 17:18:37 -07001756 public LauncherModel getModel() {
1757 return mModel;
1758 }
1759
Winson Chunga6945242014-01-08 14:04:34 -08001760 public LauncherClings getLauncherClings() {
1761 return mLauncherClings;
1762 }
1763
1764 protected SharedPreferences getSharedPrefs() {
1765 return mSharedPrefs;
1766 }
1767
Bjorn Bringertc459e522013-06-07 19:36:01 +01001768 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001769 getWindow().closeAllPanels();
1770
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001771 // Whatever we were doing is hereby canceled.
1772 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001773 }
1774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 @Override
1776 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001777 long startTime = 0;
1778 if (DEBUG_RESUME_TIME) {
1779 startTime = System.currentTimeMillis();
1780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 super.onNewIntent(intent);
1782
1783 // Close the menu
1784 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001785 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001787
Adam Cohened307df2013-10-02 09:37:31 -07001788 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1789 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1790 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001791
Adam Cohen6fecd412013-10-02 17:41:50 -07001792 if (mWorkspace == null) {
1793 // Can be cases where mWorkspace is null, this prevents a NPE
1794 return;
1795 }
1796 Folder openFolder = mWorkspace.getOpenFolder();
1797 // In all these cases, only animate if we're already on home
1798 mWorkspace.exitWidgetResizeMode();
1799 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001800 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001801 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001802 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001803
Adam Cohen6fecd412013-10-02 17:41:50 -07001804 closeFolder();
1805 exitSpringLoadedDragMode();
1806
1807 // If we are already on home, then just animate back to the workspace,
1808 // otherwise, just wait until onResume to set the state back to Workspace
1809 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001810 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001811 } else {
1812 mOnResumeState = State.WORKSPACE;
1813 }
1814
1815 final View v = getWindow().peekDecorView();
1816 if (v != null && v.getWindowToken() != null) {
1817 InputMethodManager imm = (InputMethodManager)getSystemService(
1818 INPUT_METHOD_SERVICE);
1819 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1820 }
1821
1822 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001823 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001824 mAppsCustomizeTabHost.reset();
1825 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001826
1827 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
Adam Cohened307df2013-10-02 09:37:31 -07001829
Michael Jurka447bf842013-05-15 14:52:15 +02001830 if (DEBUG_RESUME_TIME) {
1831 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
1834
Adam Coppa120b8e2013-11-12 16:26:04 +00001835 /**
1836 * Override point for subclasses to prevent movement to the default screen when the home
1837 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1838 */
1839 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1840 return true;
1841 }
1842
1843 /**
1844 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1845 */
1846 protected void onHomeIntent() {
1847 // Do nothing
1848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001851 public void onRestoreInstanceState(Bundle state) {
1852 super.onRestoreInstanceState(state);
1853 for (int page: mSynchronouslyBoundPages) {
1854 mWorkspace.restoreInstanceStateForChild(page);
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
1857
1858 @Override
1859 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001860 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001861 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1862 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001863 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001864 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865
Michael Jurka883f55b2010-10-21 15:47:14 -07001866 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001867 // We close any open folder since it will not be re-opened, and we need to make sure
1868 // this state is reflected.
1869 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870
Adam Cohendcd297f2013-06-18 13:13:40 -07001871 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 mWaitingForResult) {
1873 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001874 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001875 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1876 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001877 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1878 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1879 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001880 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882
1883 if (mFolderInfo != null && mWaitingForResult) {
1884 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1885 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1886 }
Michael Jurka946ad472010-07-09 18:05:18 -07001887
Winson Chung785d2eb2011-04-14 16:08:02 -07001888 // Save the current AppsCustomize tab
1889 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001890 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1891 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001892 if (currentTabTag != null) {
1893 outState.putString("apps_customize_currentTab", currentTabTag);
1894 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001895 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1896 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001897 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001898 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
1901 @Override
1902 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Winson Chunge7a03942011-08-05 15:05:12 -07001905 // Remove all pending runnables
1906 mHandler.removeMessages(ADVANCE_MSG);
1907 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001908 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001909
Winson Chungcd2b0142011-06-08 16:02:26 -07001910 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001911 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001912 mModel.stopLoader();
1913 app.setLauncher(null);
1914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001916 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001918 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001920 mAppWidgetHost = null;
1921
1922 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
1924 TextKeyListener.getInstance().release();
1925
Winson Chung81b52252012-08-27 15:34:29 -07001926 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1927 // to prevent leaking Launcher activities on orientation change.
1928 if (mModel != null) {
1929 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1930 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001931
1932 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001933 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001934
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001935 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001936 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001937 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001938 mWorkspace = null;
1939 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001940
Michael Jurka2ecf9952012-06-18 12:52:28 -07001941 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 public DragController getDragController() {
1945 return mDragController;
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
1949 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001950 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 super.startActivityForResult(intent, requestCode);
1952 }
1953
Winson Chung70d72102011-08-12 11:24:35 -07001954 /**
1955 * Indicates that we want global search for this activity by setting the globalSearch
1956 * argument for {@link #startSearch} to true.
1957 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001959 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001961
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001962 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001963
Karl Rosaen138a0412009-04-23 19:00:21 -07001964 if (initialQuery == null) {
1965 // Use any text typed in the launcher as the initial query
1966 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 if (appSearchData == null) {
1969 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001970 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
Winson Chungadf0c182012-08-23 14:59:07 -07001972 Rect sourceBounds = new Rect();
1973 if (mSearchDropTargetBar != null) {
1974 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1975 }
Romain Guycbb89e42009-06-08 15:52:54 -07001976
Bjorn Bringertc459e522013-06-07 19:36:01 +01001977 startSearch(initialQuery, selectInitialQuery,
1978 appSearchData, sourceBounds);
1979 }
1980
1981 public void startSearch(String initialQuery,
1982 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001983 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001984 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001985 }
1986
1987 /**
1988 * Starts the global search activity. This code is a copied from SearchManager
1989 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001990 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001991 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001992 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001993 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1994 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1995 if (globalSearchActivity == null) {
1996 Log.w(TAG, "No global search activity found.");
1997 return;
1998 }
1999 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2000 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2001 intent.setComponent(globalSearchActivity);
2002 // Make sure that we have a Bundle to put source in
2003 if (appSearchData == null) {
2004 appSearchData = new Bundle();
2005 } else {
2006 appSearchData = new Bundle(appSearchData);
2007 }
2008 // Set source to package name of app that starts global search, if not set already.
2009 if (!appSearchData.containsKey("source")) {
2010 appSearchData.putString("source", getPackageName());
2011 }
2012 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2013 if (!TextUtils.isEmpty(initialQuery)) {
2014 intent.putExtra(SearchManager.QUERY, initialQuery);
2015 }
2016 if (selectInitialQuery) {
2017 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2018 }
2019 intent.setSourceBounds(sourceBounds);
2020 try {
2021 startActivity(intent);
2022 } catch (ActivityNotFoundException ex) {
2023 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
2026
Yura4f93ec62014-02-04 14:15:21 +00002027 public boolean isOnCustomContent() {
2028 return mWorkspace.isOnOrMovingToCustomContent();
2029 }
2030
Winson Chung70d72102011-08-12 11:24:35 -07002031 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002032 public boolean onPrepareOptionsMenu(Menu menu) {
2033 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002034 if (!isOnCustomContent()) {
2035 // Close any open folders
2036 closeFolder();
2037 // Stop resizing any widgets
2038 mWorkspace.exitWidgetResizeMode();
2039 if (!mWorkspace.isInOverviewMode()) {
2040 // Show the overview mode
2041 showOverviewMode(true);
2042 } else {
2043 showWorkspace(true);
2044 }
Winson Chunge0298742014-01-17 12:03:00 -08002045 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002046 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002049 @Override
2050 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002051 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002052 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002053 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002054 }
2055
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 public boolean isWorkspaceLocked() {
2057 return mWorkspaceLoading || mWaitingForResult;
2058 }
2059
Adam Cohen517a7f52014-03-01 12:12:59 -08002060 public boolean isWorkspaceLoading() {
2061 return mWorkspaceLoading;
2062 }
2063
Michael Jurka0280c3b2010-09-17 15:00:07 -07002064 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002067 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2068 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002069 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002070 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002071 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002072
Adam Cohenad4e15c2013-10-17 16:21:35 -07002073 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2074 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2075 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2076 }
2077
2078 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2079 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2080 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002081 if (appWidgetInfo.configure != null) {
2082 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002083 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002084
Bjorn Bringert7984c942009-12-09 15:38:25 +00002085 // Launch over to configure widget, if needed
2086 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002087 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002088 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002089 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002091 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002092 Runnable onComplete = new Runnable() {
2093 @Override
2094 public void run() {
2095 // Exit spring loaded mode if necessary after adding the widget
2096 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2097 null);
2098 }
2099 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002100 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002101 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002102 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104 }
Romain Guycbb89e42009-06-08 15:52:54 -07002105
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002106 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002107 // Close any folders that may be open.
2108 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002109 mWorkspace.moveToCustomContentScreen(animate);
2110 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002111 /**
2112 * Process a shortcut drop.
2113 *
2114 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002115 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002116 * @param cell The cell it should be added to, optional
2117 * @param position The location on the screen where it was dropped, optional
2118 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002119 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002120 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002121 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002122 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002123 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002125
2126 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002127 mPendingAddInfo.cellX = cell[0];
2128 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002129 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002130
2131 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2132 createShortcutIntent.setComponent(componentName);
2133 processShortcut(createShortcutIntent);
2134 }
2135
Adam Cohenfbba09b2011-07-18 21:43:05 -07002136 /**
2137 * Process a widget drop.
2138 *
2139 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002141 * @param cell The cell it should be added to, optional
2142 * @param position The location on the screen where it was dropped, optional
2143 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002145 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002146 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002147 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002148 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002149 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002150 mPendingAddInfo.minSpanX = info.minSpanX;
2151 mPendingAddInfo.minSpanY = info.minSpanY;
2152
Adam Cohenfbba09b2011-07-18 21:43:05 -07002153 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002154 mPendingAddInfo.cellX = cell[0];
2155 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002156 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002157 if (span != null) {
2158 mPendingAddInfo.spanX = span[0];
2159 mPendingAddInfo.spanY = span[1];
2160 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002161
Adam Cohened66b2b2012-01-23 17:28:51 -08002162 AppWidgetHostView hostView = info.boundWidget;
2163 int appWidgetId;
2164 if (hostView != null) {
2165 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002166 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002167 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002168 // In this case, we either need to start an activity to get permission to bind
2169 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002170 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002171 Bundle options = info.bindOptions;
2172
2173 boolean success = false;
2174 if (options != null) {
2175 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2176 info.componentName, options);
2177 } else {
2178 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2179 info.componentName);
2180 }
2181 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002182 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002183 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002184 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002185 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2186 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2187 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002188 // TODO: we need to make sure that this accounts for the options bundle.
2189 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002190 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2191 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002192 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002193 }
2194
Joe Onoratodeb98af2010-02-19 14:59:39 -08002195 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002196 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Winson Chung24ab2f12010-09-16 14:10:47 -07002199 void processWallpaper(Intent intent) {
2200 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2201 }
2202
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002204 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002205 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 folderInfo.title = getText(R.string.folder_name);
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002209 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002210 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002211 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212
2213 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002214 FolderIcon newFolder =
2215 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002218 // Force measure the new folder icon
2219 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2220 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002221 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 }
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002225 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 }
2227
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002228 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002229 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002230 }
2231
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002232 /**
2233 * Registers various content observers. The current implementation registers
2234 * only a favorites observer to keep track of the favorites applications.
2235 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002236 private void registerContentObservers() {
2237 ContentResolver resolver = getContentResolver();
2238 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2239 true, mWidgetObserver);
2240 }
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 @Override
2243 public boolean dispatchKeyEvent(KeyEvent event) {
2244 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2245 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002247 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002248 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002249 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002250 dumpState();
2251 return true;
2252 }
2253 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002254 }
2255 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2256 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002257 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 return true;
2259 }
2260 }
2261
2262 return super.dispatchKeyEvent(event);
2263 }
2264
Joe Onorato88ec0992009-11-19 13:16:06 -08002265 @Override
2266 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002267 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002268 if (mAppsCustomizeContent.getContentType() ==
2269 AppsCustomizePagedView.ContentType.Applications) {
2270 showWorkspace(true);
2271 } else {
2272 showOverviewMode(true);
2273 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002274 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002275 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002276 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002277 Folder openFolder = mWorkspace.getOpenFolder();
2278 if (openFolder.isEditingName()) {
2279 openFolder.dismissEditingName();
2280 } else {
2281 closeFolder();
2282 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002283 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002284 mWorkspace.exitWidgetResizeMode();
2285
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002286 // Back button is a no-op here, but give at least some feedback for the button press
2287 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002288 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002289 }
2290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002292 * Re-listen when widgets are reset.
2293 */
2294 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002295 if (mAppWidgetHost != null) {
2296 mAppWidgetHost.startListening();
2297 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002298 }
2299
2300 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 * Launches the intent referred by the clicked shortcut.
2302 *
2303 * @param v The view representing the clicked shortcut.
2304 */
2305 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002306 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2307 // view has detached (it's possible for this to happen if the view is removed mid touch).
2308 if (v.getWindowToken() == null) {
2309 return;
2310 }
2311
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002312 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002313 return;
2314 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002315
Adam Cohen1697b792013-09-17 19:08:21 -07002316 if (v instanceof Workspace) {
2317 if (mWorkspace.isInOverviewMode()) {
2318 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002319 }
2320 return;
2321 }
2322
2323 if (v instanceof CellLayout) {
2324 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002325 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002326 }
2327 }
2328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002330 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002331 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002333 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002334 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002335 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002336 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002337 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 }
2339 }
2340
Michael Jurka0e260592010-06-30 17:07:39 -07002341 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002342 return false;
2343 }
2344
Michael Jurkaaf442092010-06-10 17:01:57 -07002345 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002346 * Event handler for the search button
2347 *
2348 * @param v The view that was clicked.
2349 */
2350 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002351 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2352
Amith Yamasania135ba82011-08-09 17:42:01 -07002353 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002354 }
2355
2356 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002357 * Event handler for the voice button
2358 *
2359 * @param v The view that was clicked.
2360 */
2361 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002362 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002363
Bjorn Bringertc459e522013-06-07 19:36:01 +01002364 startVoice();
2365 }
2366
2367 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002368 try {
2369 final SearchManager searchManager =
2370 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2371 ComponentName activityName = searchManager.getGlobalSearchActivity();
2372 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002373 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002374 if (activityName != null) {
2375 intent.setPackage(activityName.getPackageName());
2376 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002377 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002378 } catch (ActivityNotFoundException e) {
2379 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002380 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002381 startActivitySafely(null, intent, "onClickVoiceButton");
2382 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002383 }
2384
2385 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002386 * Event handler for the "grid" button that appears on the home screen, which
2387 * enters all apps mode.
2388 *
2389 * @param v The view that was clicked.
2390 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002391 protected void onClickAllAppsButton(View v) {
2392 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2393 if (isAllAppsVisible()) {
2394 showWorkspace(true);
2395 } else {
2396 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2397 }
2398 }
2399
2400 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002401 * Event handler for a paged view icon click.
2402 * @param v The view that was clicked.
2403 * @param appInfo The {link AppInfo} of the view.
2404 */
2405 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2406 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2407 startActivitySafely(v, appInfo.intent, appInfo);
2408 getStats().recordLaunch(appInfo.intent);
2409 }
2410
2411 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002412 * Event handler for an app shortcut click.
2413 *
2414 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2415 */
2416 protected void onClickAppShortcut(View v) {
2417 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2418 Object tag = v.getTag();
2419 if (!(tag instanceof ShortcutInfo)) {
2420 throw new IllegalArgumentException("Input must be a Shortcut");
2421 }
2422
2423 // Open shortcut
2424 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2425 final Intent intent = shortcut.intent;
2426
2427 // Check for special shortcuts
2428 if (intent.getComponent() != null) {
2429 final String shortcutClass = intent.getComponent().getClassName();
2430
2431 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2432 MemoryDumpActivity.startDump(this);
2433 return;
2434 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2435 toggleShowWeightWatcher();
2436 return;
2437 }
2438 }
2439
2440 // Start activities
2441 int[] pos = new int[2];
2442 v.getLocationOnScreen(pos);
2443 intent.setSourceBounds(new Rect(pos[0], pos[1],
2444 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2445
2446 boolean success = startActivitySafely(v, intent, tag);
2447
2448 mStats.recordLaunch(intent, shortcut);
2449
2450 if (success && v instanceof BubbleTextView) {
2451 mWaitingForResume = (BubbleTextView) v;
2452 mWaitingForResume.setStayPressed(true);
2453 }
2454 }
2455
2456 /**
2457 * Event handler for a folder icon click.
2458 *
2459 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2460 */
2461 protected void onClickFolderIcon(View v) {
2462 if (LOGD) Log.d(TAG, "onClickFolder");
2463 if (!(v instanceof FolderIcon)){
2464 throw new IllegalArgumentException("Input must be a FolderIcon");
2465 }
2466
2467 FolderIcon folderIcon = (FolderIcon) v;
2468 final FolderInfo info = folderIcon.getFolderInfo();
2469 Folder openFolder = mWorkspace.getFolderForTag(info);
2470
2471 // If the folder info reports that the associated folder is open, then verify that
2472 // it is actually opened. There have been a few instances where this gets out of sync.
2473 if (info.opened && openFolder == null) {
2474 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2475 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2476 info.opened = false;
2477 }
2478
2479 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2480 // Close any open folder
2481 closeFolder();
2482 // Open the requested folder
2483 openFolder(folderIcon);
2484 } else {
2485 // Find the open folder...
2486 int folderScreen;
2487 if (openFolder != null) {
2488 folderScreen = mWorkspace.getPageForView(openFolder);
2489 // .. and close it
2490 closeFolder(openFolder);
2491 if (folderScreen != mWorkspace.getCurrentPage()) {
2492 // Close any folder open on the current screen
2493 closeFolder();
2494 // Pull the folder onto this screen
2495 openFolder(folderIcon);
2496 }
2497 }
2498 }
Michael Jurka5130e402011-10-13 04:55:35 -07002499 }
2500
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002501 /**
2502 * Event handler for the (Add) Widgets button that appears after a long press
2503 * on the home screen.
2504 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002505 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002507 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2508 }
2509
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 /**
2511 * Event handler for the wallpaper picker button that appears after a long press
2512 * on the home screen.
2513 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002514 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2516 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2517 pickWallpaper.setComponent(getWallpaperPickerComponent());
2518 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2519 }
2520
2521 /**
2522 * Event handler for a click on the settings button that appears after a long press
2523 * on the home screen.
2524 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002525 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2527 }
2528
Michael Jurka5130e402011-10-13 04:55:35 -07002529 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002530 // Provide the same haptic feedback that the system offers for virtual keys.
2531 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002532 }
2533
Adam Cohen61f560d2013-09-30 15:58:20 -07002534 public void performHapticFeedbackOnTouchDown(View v) {
2535 // Provide the same haptic feedback that the system offers for virtual keys.
2536 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2537 }
2538
2539 public View.OnTouchListener getHapticFeedbackTouchListener() {
2540 if (mHapticFeedbackTouchListener == null) {
2541 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2542 @Override
2543 public boolean onTouch(View v, MotionEvent event) {
2544 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2545 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2546 }
2547 return false;
2548 }
2549 };
2550 }
2551 return mHapticFeedbackTouchListener;
2552 }
2553
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002554 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002555 if (!DISABLE_MARKET_BUTTON) {
2556 if (mAppMarketIntent != null) {
2557 startActivitySafely(v, mAppMarketIntent, "app market");
2558 } else {
2559 Log.e(TAG, "Invalid app market intent.");
2560 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002561 }
2562 }
2563
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002564 public void onDragStarted(View view) {}
2565
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002566 /**
2567 * Called when the user stops interacting with the launcher.
2568 * This implies that the user is now on the homescreen and is not doing housekeeping.
2569 */
2570 protected void onInteractionEnd() {}
2571
2572 /**
2573 * Called when the user starts interacting with the launcher.
2574 * The possible interactions are:
2575 * - open all apps
2576 * - reorder an app shortcut, or a widget
2577 * - open the overview mode.
2578 * This is a good time to stop doing things that only make sense
2579 * when the user is on the homescreen and not doing housekeeping.
2580 */
2581 protected void onInteractionBegin() {}
2582
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002583 void startApplicationDetailsActivity(ComponentName componentName) {
2584 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002585 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2586 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002587 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2588 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002589 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002590 }
2591
Michael Jurka1e2f4652013-07-08 18:03:46 -07002592 // returns true if the activity was started
2593 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002594 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002595 // System applications cannot be installed. For now, show a toast explaining that.
2596 // We may give them the option of disabling apps this way.
2597 int messageId = R.string.uninstall_system_app_text;
2598 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002599 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002600 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002601 String packageName = componentName.getPackageName();
2602 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002603 Intent intent = new Intent(
2604 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002605 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2606 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002607 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002608 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002609 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002610 }
2611
Michael Jurka86a720e2012-05-09 11:23:23 -07002612 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyed131872014-04-30 03:02:21 +01002613 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002615 // Only launch using the new animation if the shortcut has not opted out (this is a
2616 // private contract between launcher and may be ignored in the future).
2617 boolean useLaunchAnimation = (v != null) &&
2618 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002619 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2620 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2621 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, 0);
2622 UserHandleCompat user = serialNumber == 0 ? null :
2623 userManager.getUserForSerialNumber(serialNumber);
2624
Winson Chung2672ff92012-05-04 16:22:30 -07002625 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002626 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2627 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guyed131872014-04-30 03:02:21 +01002628 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2629 // Could be launching some bookkeeping activity
2630 startActivity(intent, opts.toBundle());
2631 } else {
2632 launcherApps.startActivityForProfile(intent.getComponent(),
2633 intent.getSourceBounds(),
2634 opts.toBundle(), user);
2635 }
Winson Chungc7450e32012-04-17 17:34:08 -07002636 } else {
2637 startActivity(intent);
2638 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002639 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 } catch (SecurityException e) {
2641 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002642 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002644 "or use the exported attribute for this activity. "
2645 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002647 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002649
Michael Jurka86a720e2012-05-09 11:23:23 -07002650 boolean startActivitySafely(View v, Intent intent, Object tag) {
2651 boolean success = false;
2652 try {
2653 success = startActivity(v, intent, tag);
2654 } catch (ActivityNotFoundException e) {
2655 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2656 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2657 }
2658 return success;
2659 }
2660
Adam Cohen268c4752012-06-06 17:47:33 -07002661 /**
2662 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2663 * in the DragLayer in the exact absolute location of the original FolderIcon.
2664 */
2665 private void copyFolderIconToImage(FolderIcon fi) {
2666 final int width = fi.getMeasuredWidth();
2667 final int height = fi.getMeasuredHeight();
2668
2669 // Lazy load ImageView, Bitmap and Canvas
2670 if (mFolderIconImageView == null) {
2671 mFolderIconImageView = new ImageView(this);
2672 }
2673 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2674 mFolderIconBitmap.getHeight() != height) {
2675 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2676 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2677 }
2678
2679 DragLayer.LayoutParams lp;
2680 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2681 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2682 } else {
2683 lp = new DragLayer.LayoutParams(width, height);
2684 }
2685
Adam Cohen307fe232012-08-16 17:55:58 -07002686 // The layout from which the folder is being opened may be scaled, adjust the starting
2687 // view size by this scale factor.
2688 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002689 lp.customPosition = true;
2690 lp.x = mRectForFolderAnimation.left;
2691 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002692 lp.width = (int) (scale * width);
2693 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002694
2695 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2696 fi.draw(mFolderIconCanvas);
2697 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002698 if (fi.getFolder() != null) {
2699 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2700 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002701 }
Adam Cohen268c4752012-06-06 17:47:33 -07002702 // Just in case this image view is still in the drag layer from a previous animation,
2703 // we remove it and re-add it.
2704 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2705 mDragLayer.removeView(mFolderIconImageView);
2706 }
2707 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002708 if (fi.getFolder() != null) {
2709 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002710 }
Adam Cohen268c4752012-06-06 17:47:33 -07002711 }
2712
Adam Cohen2801caf2011-05-13 20:57:39 -07002713 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002714 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002715 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2716 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2717 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2718
Adam Cohenc51934b2011-07-26 21:07:43 -07002719 FolderInfo info = (FolderInfo) fi.getTag();
2720 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2721 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002722 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2723 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002724 }
2725
Adam Cohen268c4752012-06-06 17:47:33 -07002726 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2727 copyFolderIconToImage(fi);
2728 fi.setVisibility(View.INVISIBLE);
2729
Michael Jurka2ecf9952012-06-18 12:52:28 -07002730 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002731 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002732 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2733 oa.start();
2734 }
2735
Adam Cohen268c4752012-06-06 17:47:33 -07002736 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002737 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002738 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2739 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2740 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2741
Adam Cohen268c4752012-06-06 17:47:33 -07002742 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002743
Adam Cohen268c4752012-06-06 17:47:33 -07002744 // We remove and re-draw the FolderIcon in-case it has changed
2745 mDragLayer.removeView(mFolderIconImageView);
2746 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002747 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002748 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002749 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002750 oa.addListener(new AnimatorListenerAdapter() {
2751 @Override
2752 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002753 if (cl != null) {
2754 cl.clearFolderLeaveBehind();
2755 // Remove the ImageView copy of the FolderIcon and make the original visible.
2756 mDragLayer.removeView(mFolderIconImageView);
2757 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002758 }
2759 }
2760 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002761 oa.start();
2762 }
2763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002765 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 * is animated relative to the specified View. If the View is null, no animation
2767 * is played.
2768 *
2769 * @param folderInfo The FolderInfo describing the folder to open.
2770 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002771 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002772 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002773 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774
Adam Cohena9cf38f2011-05-02 15:36:58 -07002775 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776
Adam Cohen4554ee12011-08-03 16:13:21 -07002777 // Just verify that the folder hasn't already been added to the DragLayer.
2778 // There was a one-off crash where the folder had a parent already.
2779 if (folder.getParent() == null) {
2780 mDragLayer.addView(folder);
2781 mDragController.addDropTarget((DropTarget) folder);
2782 } else {
2783 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2784 folder.getParent() + ").");
2785 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002786 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002787 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002788
2789 // Notify the accessibility manager that this folder "window" has appeared and occluded
2790 // the workspace items
2791 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2792 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002793 }
2794
2795 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002796 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002797 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002798 if (folder.isEditingName()) {
2799 folder.dismissEditingName();
2800 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002801 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002802
2803 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002804 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002805 }
2806 }
2807
2808 void closeFolder(Folder folder) {
2809 folder.getInfo().opened = false;
2810
2811 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2812 if (parent != null) {
2813 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2814 shrinkAndFadeInFolderIcon(fi);
2815 }
2816 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002817
2818 // Notify the accessibility manager that this folder "window" has disappeard and no
2819 // longer occludeds the workspace items
2820 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 }
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002824 if (!isDraggingEnabled()) return false;
2825 if (isWorkspaceLocked()) return false;
2826 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827
Adam Cohen1697b792013-09-17 19:08:21 -07002828 if (v instanceof Workspace) {
2829 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002830 if (mWorkspace.enterOverviewMode()) {
2831 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2832 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2833 return true;
2834 } else {
2835 return false;
2836 }
Adam Cohen1697b792013-09-17 19:08:21 -07002837 }
Adam Cohen1697b792013-09-17 19:08:21 -07002838 }
2839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002841 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 }
2843
Michael Jurka0280c3b2010-09-17 15:00:07 -07002844 resetAddInfo();
2845 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002847 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 return true;
2849 }
2850
Winson Chung3d503fb2011-07-13 17:25:49 -07002851 // The hotseat touch handling does not go through Workspace, and we always allow long press
2852 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002853 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002854 final boolean inHotseat = isHotseatLayout(v);
2855 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002856 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002857 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002858 // User long pressed on empty space
2859 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2860 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2861 // Disabling reordering until we sort out some issues.
2862 if (mWorkspace.isInOverviewMode()) {
2863 mWorkspace.startReordering(v);
2864 } else {
2865 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002868 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2869 mHotseat.getOrderInHotseat(
2870 longClickCellInfo.cellX,
2871 longClickCellInfo.cellY));
2872 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002874 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
2876 }
2877 }
2878 return true;
2879 }
2880
Winson Chung3d503fb2011-07-13 17:25:49 -07002881 boolean isHotseatLayout(View layout) {
2882 return mHotseat != null && layout != null &&
2883 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2884 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002885
Winson Chung3d503fb2011-07-13 17:25:49 -07002886 /**
2887 * Returns the CellLayout of the specified container at the specified screen.
2888 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002889 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2891 if (mHotseat != null) {
2892 return mHotseat.getLayout();
2893 } else {
2894 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002895 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002896 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002897 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002898 }
2899 }
2900
Daniel Sandler843e8602010-06-07 14:59:01 -04002901 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002902 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002903 }
2904
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002905 /**
2906 * Helper method for the cameraZoomIn/cameraZoomOut animations
2907 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002908 * @param scaleFactor The scale factor used for the zoom
2909 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002910 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002911 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002912 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002913 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002914
Craig Mautner360310b2012-10-26 15:13:08 -07002915 private void setWorkspaceBackground(boolean workspace) {
2916 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002917 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002918 }
2919
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002920 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002921 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2922 int curflags = getWindow().getAttributes().flags
2923 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2924 if (wpflags != curflags) {
2925 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2926 }
Craig Mautner360310b2012-10-26 15:13:08 -07002927 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002928 }
2929
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002930 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2931 if (v instanceof LauncherTransitionable) {
2932 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2933 }
2934 }
2935
Michael Jurkabed61d22012-02-14 22:51:29 -08002936 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2937 if (v instanceof LauncherTransitionable) {
2938 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2939 }
Winson Chung70442722012-02-10 15:43:22 -08002940
2941 // Update the workspace transition step as well
2942 dispatchOnLauncherTransitionStep(v, 0f);
2943 }
2944
2945 private void dispatchOnLauncherTransitionStep(View v, float t) {
2946 if (v instanceof LauncherTransitionable) {
2947 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2948 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002949 }
2950
2951 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2952 if (v instanceof LauncherTransitionable) {
2953 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2954 }
Winson Chung70442722012-02-10 15:43:22 -08002955
2956 // Update the workspace transition step as well
2957 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002958 }
2959
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002960 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002961 * Things to test when changing the following seven functions.
2962 * - Home from workspace
2963 * - from center screen
2964 * - from other screens
2965 * - Home from all apps
2966 * - from center screen
2967 * - from other screens
2968 * - Back from all apps
2969 * - from center screen
2970 * - from other screens
2971 * - Launch app from workspace and quit
2972 * - with back
2973 * - with home
2974 * - Launch app from all apps and quit
2975 * - with back
2976 * - with home
2977 * - Go to a screen that's not the default, then all
2978 * apps, and launch and app, and go back
2979 * - with back
2980 * -with home
2981 * - On workspace, long press power and go back
2982 * - with back
2983 * - with home
2984 * - On all apps, long press power and go back
2985 * - with back
2986 * - with home
2987 * - On workspace, power off
2988 * - On all apps, power off
2989 * - Launch an app and turn off the screen while in that app
2990 * - Go back with home key
2991 * - Go back with back key TODO: make this not go to workspace
2992 * - From all apps
2993 * - From workspace
2994 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2995 * - From all apps
2996 * - From the center workspace
2997 * - From another workspace
2998 */
2999
3000 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003001 * Zoom the camera out from the workspace to reveal 'toView'.
3002 * Assumes that the view to show is anchored at either the very top or very bottom
3003 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003004 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003005 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003006 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3007 showAppsCustomizeHelper(animated, springLoaded, contentType);
3008 }
3009 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3010 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003012 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003013 mStateAnimation.cancel();
3014 mStateAnimation = null;
3015 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003016 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003017
Winson Chungf0ea4d32011-06-06 14:27:16 -07003018 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3019 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3020 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003021 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003022 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003023 final int startDelay =
3024 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003025
Michael Jurkab3e22d92011-10-31 15:58:33 -07003026 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003027
Michael Jurkad74c9842011-07-10 12:44:21 -07003028 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003029 Animator workspaceAnim =
3030 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003031 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003032 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3033 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003034 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3035 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003036
3037 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003038 toView.setScaleX(scale);
3039 toView.setScaleY(scale);
3040 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3041 scaleAnim.
3042 scaleX(1f).scaleY(1f).
3043 setDuration(duration).
3044 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003045
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003047 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003048 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003049 .ofFloat(toView, "alpha", 0f, 1f)
3050 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003051 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003052 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3053 @Override
3054 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003055 if (animation == null) {
3056 throw new RuntimeException("animation is null");
3057 }
Winson Chung70442722012-02-10 15:43:22 -08003058 float t = (Float) animation.getAnimatedValue();
3059 dispatchOnLauncherTransitionStep(fromView, t);
3060 dispatchOnLauncherTransitionStep(toView, t);
3061 }
3062 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003063
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003064 // toView should appear right at the end of the workspace shrink
3065 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003066 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003067 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003068 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003069
3070 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003071 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003072 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003073 // Prepare the position
3074 toView.setTranslationX(0.0f);
3075 toView.setTranslationY(0.0f);
3076 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003077 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003078 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003079 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003080 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003081 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3082 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003083
Winson Chungc7d2b602012-05-16 17:02:20 -07003084 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003085 if (mSearchDropTargetBar != null) {
3086 mSearchDropTargetBar.hideSearchBar(false);
3087 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003088 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003089 });
3090
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003091 if (workspaceAnim != null) {
3092 mStateAnimation.play(workspaceAnim);
3093 }
Michael Jurka1899a362011-11-03 13:50:45 -07003094
3095 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003096
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003097 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3098 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003099
3100 // If any of the objects being animated haven't been measured/laid out
3101 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003102 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003103 (mWorkspace.getMeasuredWidth() == 0) ||
3104 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003105 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003106 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003107
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003108 final AnimatorSet stateAnimation = mStateAnimation;
3109 final Runnable startAnimRunnable = new Runnable() {
3110 public void run() {
3111 // Check that mStateAnimation hasn't changed while
3112 // we waited for a layout/draw pass
3113 if (mStateAnimation != stateAnimation)
3114 return;
3115 setPivotsForZoom(toView, scale);
3116 dispatchOnLauncherTransitionStart(fromView, animated, false);
3117 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003118 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003119 }
3120 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003121 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003122 final ViewTreeObserver observer = toView.getViewTreeObserver();
3123 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3124 public void onGlobalLayout() {
3125 startAnimRunnable.run();
3126 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3127 }
3128 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003129 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003130 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003131 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003132 } else {
3133 toView.setTranslationX(0.0f);
3134 toView.setTranslationY(0.0f);
3135 toView.setScaleX(1.0f);
3136 toView.setScaleY(1.0f);
3137 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003138 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003139
Daniel Sandlere4f98912013-06-25 15:13:26 -04003140 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003141 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003142 if (mSearchDropTargetBar != null) {
3143 mSearchDropTargetBar.hideSearchBar(false);
3144 }
Michael Jurkaabded662011-03-04 12:06:57 -08003145 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003146 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003147 dispatchOnLauncherTransitionStart(fromView, animated, false);
3148 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003149 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003150 dispatchOnLauncherTransitionStart(toView, animated, false);
3151 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003152 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003153 }
3154
3155 /**
3156 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003157 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003158 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003159 */
Adam Cohened307df2013-10-02 09:37:31 -07003160 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003161 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003162
Michael Jurkab3e22d92011-10-31 15:58:33 -07003163 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003164 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003165 mStateAnimation.cancel();
3166 mStateAnimation = null;
3167 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003168 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003169
Winson Chungf0ea4d32011-06-06 14:27:16 -07003170 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003171 final int fadeOutDuration =
3172 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003173 final float scaleFactor = (float)
3174 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3175 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003176 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003177 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003178 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003179 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3180 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003181 toState, animated, stagger, -1);
3182 } else if (toState == Workspace.State.SPRING_LOADED ||
3183 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003184 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003185 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003186 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003187
Michael Jurkab3e22d92011-10-31 15:58:33 -07003188 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003189 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003190 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003191 final LauncherViewPropertyAnimator scaleAnim =
3192 new LauncherViewPropertyAnimator(fromView);
3193 scaleAnim.
3194 scaleX(scaleFactor).scaleY(scaleFactor).
3195 setDuration(duration).
3196 setInterpolator(new Workspace.ZoomInInterpolator());
3197
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003198 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003199 .ofFloat(fromView, "alpha", 1f, 0f)
3200 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003201 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003202 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3203 @Override
3204 public void onAnimationUpdate(ValueAnimator animation) {
3205 float t = 1f - (Float) animation.getAnimatedValue();
3206 dispatchOnLauncherTransitionStep(fromView, t);
3207 dispatchOnLauncherTransitionStep(toView, t);
3208 }
3209 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003210
Michael Jurka2ecf9952012-06-18 12:52:28 -07003211 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003212
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003213 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3214 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003215 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003216
3217 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003218 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003219 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003220 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003221 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3222 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003223 if (onCompleteRunnable != null) {
3224 onCompleteRunnable.run();
3225 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003226 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003227 }
3228 });
3229
Winson Chungf0ea4d32011-06-06 14:27:16 -07003230 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003231 if (workspaceAnim != null) {
3232 mStateAnimation.play(workspaceAnim);
3233 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003234 dispatchOnLauncherTransitionStart(fromView, animated, true);
3235 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003236 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003237 } else {
3238 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003239 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003240 dispatchOnLauncherTransitionStart(fromView, animated, true);
3241 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003242 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003243 dispatchOnLauncherTransitionStart(toView, animated, true);
3244 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003245 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003246 }
3247
Michael Jurkae326f182011-11-21 14:05:46 -08003248 @Override
3249 public void onTrimMemory(int level) {
3250 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003251 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003252 mAppsCustomizeTabHost.onTrimMemory();
3253 }
3254 }
3255
Adam Cohened307df2013-10-02 09:37:31 -07003256 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003257 showWorkspace(animated, null);
3258 }
3259
Adam Cohened307df2013-10-02 09:37:31 -07003260 protected void showWorkspace() {
3261 showWorkspace(true);
3262 }
3263
Adam Cohened66b2b2012-01-23 17:28:51 -08003264 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003265 if (mWorkspace.isInOverviewMode()) {
3266 mWorkspace.exitOverviewMode(animated);
3267 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003268 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003269 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003270 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003271 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272
Winson Chungc7d2b602012-05-16 17:02:20 -07003273 // Show the search bar (only animate if we were showing the drop target bar in spring
3274 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003275 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003276 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003277 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003278
Michael Jurkab3e22d92011-10-31 15:58:33 -07003279 // Set focus to the AppsCustomize button
3280 if (mAllAppsButton != null) {
3281 mAllAppsButton.requestFocus();
3282 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003283 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003284
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003285 // Change the state *after* we've called all the transition code
3286 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003287
Winson Chung5fb63472011-02-02 17:03:37 -08003288 // Resume the auto-advance of widgets
3289 mUserPresent = true;
3290 updateRunning();
3291
alanv1d4fde62012-10-17 13:15:47 -07003292 // Send an accessibility event to announce the context change
3293 getWindow().getDecorView()
3294 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003295
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003296 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003297 }
3298
Adam Cohened307df2013-10-02 09:37:31 -07003299 void showOverviewMode(boolean animated) {
3300 mWorkspace.setVisibility(View.VISIBLE);
3301 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3302 mState = State.WORKSPACE;
3303 onWorkspaceShown(animated);
3304 }
3305
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003306 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003307 }
3308
Winson Chung82963d52013-10-09 11:20:57 -07003309 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3310 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311 if (mState != State.WORKSPACE) return;
3312
Winson Chung82963d52013-10-09 11:20:57 -07003313 if (resetPageToZero) {
3314 mAppsCustomizeTabHost.reset();
3315 }
Winson Chungc58497e2013-09-03 17:48:37 -07003316 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 mAppsCustomizeTabHost.requestFocus();
3318
Michael Jurkab3e22d92011-10-31 15:58:33 -07003319 // Change the state *after* we've called all the transition code
3320 mState = State.APPS_CUSTOMIZE;
3321
3322 // Pause the auto-advance of widgets until we are out of AllApps
3323 mUserPresent = false;
3324 updateRunning();
3325 closeFolder();
3326
3327 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003328 getWindow().getDecorView()
3329 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003330 }
3331
Adam Cohen7777d962011-08-18 18:58:38 -07003332 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003333 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003334 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003335 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003336 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003337 }
Adam Cohen7777d962011-08-18 18:58:38 -07003338
Adam Cohenad4e15c2013-10-17 16:21:35 -07003339 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003340 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003341 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3342
Winson Chunge7a03942011-08-05 15:05:12 -07003343 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003344 @Override
3345 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003346 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003347 // Before we show workspace, hide all apps again because
3348 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3349 // clean up our state transition functions
3350 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003351 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003352 } else {
3353 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003354 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003355 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003356 }, delay);
3357
Winson Chung557d6ed2011-07-08 15:34:52 -07003358 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003359
Michael Jurkad3ef3062010-11-23 16:23:58 -08003360 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003361 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362 final boolean animated = true;
3363 final boolean springLoaded = true;
3364 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003365 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003366 }
3367 // Otherwise, we are not in spring loaded mode, so don't do anything.
3368 }
3369
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 void lockAllApps() {
3371 // TODO
3372 }
3373
3374 void unlockAllApps() {
3375 // TODO
3376 }
3377
Winson Chungf0ea4d32011-06-06 14:27:16 -07003378 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003379 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003380 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003381 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003382 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003384 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003385 int duration = 0;
3386 if (mSearchDropTargetBar != null) {
3387 duration = mSearchDropTargetBar.getTransitionInDuration();
3388 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003389 mHotseat.animate().alpha(1f).setDuration(duration);
3390 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003392 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003393 }
3394 }
3395 }
3396
3397 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003398 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003399 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003400 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003401 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003402 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003403 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003404 int duration = 0;
3405 if (mSearchDropTargetBar != null) {
3406 duration = mSearchDropTargetBar.getTransitionOutDuration();
3407 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003408 mHotseat.animate().alpha(0f).setDuration(duration);
3409 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003410 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003411 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003413 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003414 }
3415
Patrick Dubroy5f445422011-02-18 14:35:21 -08003416 /**
3417 * Add an item from all apps or customize onto the given workspace screen.
3418 * If layout is null, add to the current screen.
3419 */
3420 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003421 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003422 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003423 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003424 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003425
Winson Chungdff8ebb2011-09-08 17:25:31 -07003426 /** Maps the current orientation to an index for referencing orientation correct global icons */
3427 private int getCurrentOrientationIndexForGlobalIcons() {
3428 // default - 0, landscape - 1
3429 switch (getResources().getConfiguration().orientation) {
3430 case Configuration.ORIENTATION_LANDSCAPE:
3431 return 1;
3432 default:
3433 return 0;
3434 }
3435 }
3436
Michael Jurkaae65ee32012-04-30 11:45:54 -07003437 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003438 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003439 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003440 // Look for the toolbar icon specified in the activity meta-data
3441 Bundle metaData = packageManager.getActivityInfo(
3442 activityName, PackageManager.GET_META_DATA).metaData;
3443 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003444 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003445 if (iconResId != 0) {
3446 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003447 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003448 }
3449 }
3450 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003451 // This can happen if the activity defines an invalid drawable
3452 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3453 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003454 } catch (Resources.NotFoundException nfe) {
3455 // This can happen if the activity defines an invalid drawable
3456 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3457 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003458 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003459 return null;
3460 }
3461
3462 // if successful in getting icon, return it; otherwise, set button to use default drawable
3463 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003464 int buttonId, ComponentName activityName, int fallbackDrawableId,
3465 String toolbarResourceName) {
3466 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003467 Resources r = getResources();
3468 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3469 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003470
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003471 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003472 // If we were unable to find the icon via the meta-data, use a generic one
3473 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003474 toolbarIcon = r.getDrawable(fallbackDrawableId);
3475 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003476 if (button != null) {
3477 button.setCompoundDrawables(toolbarIcon, null, null, null);
3478 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003479 return null;
3480 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003481 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003482 if (button != null) {
3483 button.setCompoundDrawables(toolbarIcon, null, null, null);
3484 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003485 return toolbarIcon.getConstantState();
3486 }
3487 }
3488
3489 // if successful in getting icon, return it; otherwise, set button to use default drawable
3490 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003491 int buttonId, ComponentName activityName, int fallbackDrawableId,
3492 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003493 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003494 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003495
Michael Jurka19e0fc52011-07-22 18:00:21 -07003496 if (button != null) {
3497 // If we were unable to find the icon via the meta-data, use a
3498 // generic one
3499 if (toolbarIcon == null) {
3500 button.setImageResource(fallbackDrawableId);
3501 } else {
3502 button.setImageDrawable(toolbarIcon);
3503 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003504 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003505
3506 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3507
Michael Jurka0423dcf2010-10-05 14:56:18 -07003508 }
3509
Winson Chung1b884092012-06-08 17:13:02 -07003510 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003511 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003512 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003513 }
3514
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003515 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003516 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003517 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003518 }
3519
Winson Chungbb185bd2011-11-21 12:31:42 -08003520 private void invalidatePressedFocusedStates(View container, View button) {
3521 if (container instanceof HolographicLinearLayout) {
3522 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3523 layout.invalidatePressedFocusedStates();
3524 } else if (button instanceof HolographicImageView) {
3525 HolographicImageView view = (HolographicImageView) button;
3526 view.invalidatePressedFocusedStates();
3527 }
3528 }
3529
Cristina Stancu476493b2013-08-07 17:20:14 +01003530 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003531 if (mQsb == null) {
3532 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3533 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003534 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003535 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003536 }
3537
3538 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003539 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003540 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003541 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003542 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003543
Winson Chung1cad91e2011-05-25 17:41:01 -07003544 final SearchManager searchManager =
3545 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3546 ComponentName activityName = searchManager.getGlobalSearchActivity();
3547 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003548 int coi = getCurrentOrientationIndexForGlobalIcons();
3549 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003550 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3551 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3552 if (sGlobalSearchIcon[coi] == null) {
3553 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3554 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3555 TOOLBAR_ICON_METADATA_NAME);
3556 }
3557
Winson Chungc51db6a2011-10-05 11:44:49 -07003558 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3559 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003560 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003561 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003562 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003563 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003564 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3565 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003566 if (searchButton != null) searchButton.setVisibility(View.GONE);
3567 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003568 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003569 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003570 }
3571 }
3572
Cristina Stancu476493b2013-08-07 17:20:14 +01003573 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003574 final View searchButtonContainer = findViewById(R.id.search_button_container);
3575 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003576 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003577 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003578 }
3579
Cristina Stancu476493b2013-08-07 17:20:14 +01003580 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003581 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003582 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003583
Winson Chungc51db6a2011-10-05 11:44:49 -07003584 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003585 final SearchManager searchManager =
3586 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3587 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3588
3589 ComponentName activityName = null;
3590 if (globalSearchActivity != null) {
3591 // Check if the global search activity handles voice search
3592 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3593 intent.setPackage(globalSearchActivity.getPackageName());
3594 activityName = intent.resolveActivity(getPackageManager());
3595 }
3596
3597 if (activityName == null) {
3598 // Fallback: check if an activity other than the global search activity
3599 // resolves this
3600 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3601 activityName = intent.resolveActivity(getPackageManager());
3602 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003603 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003604 int coi = getCurrentOrientationIndexForGlobalIcons();
3605 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003606 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3607 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3608 if (sVoiceSearchIcon[coi] == null) {
3609 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3610 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3611 TOOLBAR_ICON_METADATA_NAME);
3612 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003613 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003614 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003615 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003616 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003617 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003618 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003619 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003620 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003621 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003622 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003623 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003624 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003625
Cristina Stancu476493b2013-08-07 17:20:14 +01003626 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003627 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3628 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003629 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003630 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003631 }
3632
Winson Chung5841efa2013-09-30 18:06:44 -07003633 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003634 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3635 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003636 boolean visible = !forceDisableVoiceButtonProxy &&
3637 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003638 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003639 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003640 }
3641 }
Winson Chung5841efa2013-09-30 18:06:44 -07003642
3643 /**
3644 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3645 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3646 */
3647 public void disableVoiceButtonProxy(boolean disabled) {
3648 updateVoiceButtonProxyVisible(disabled);
3649 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003650 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003651 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003652 */
3653 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003654 if (!DISABLE_MARKET_BUTTON) {
3655 final View marketButton = findViewById(R.id.market_button);
3656 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3657 // Find the app market activity by resolving an intent.
3658 // (If multiple app markets are installed, it will return the ResolverActivity.)
3659 ComponentName activityName = intent.resolveActivity(getPackageManager());
3660 if (activityName != null) {
3661 int coi = getCurrentOrientationIndexForGlobalIcons();
3662 mAppMarketIntent = intent;
3663 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3664 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3665 TOOLBAR_ICON_METADATA_NAME);
3666 marketButton.setVisibility(View.VISIBLE);
3667 } else {
3668 // We should hide and disable the view so that we don't try and restore the visibility
3669 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3670 marketButton.setVisibility(View.GONE);
3671 marketButton.setEnabled(false);
3672 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003673 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003674 }
3675
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003676 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003677 if (!DISABLE_MARKET_BUTTON) {
3678 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3679 Resources r = getResources();
3680 Drawable marketIconDrawable = d.newDrawable(r);
3681 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3682 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3683 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003684
Winson Chungd64a6662013-09-30 11:06:59 -07003685 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3686 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003687 }
3688
Michael Jurkad7c28052012-04-27 15:43:36 -07003689 @Override
3690 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003691 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003692 final List<CharSequence> text = event.getText();
3693 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003694 // Populate event with a fake title based on the current state.
3695 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003696 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003697 } else {
3698 text.add(getString(R.string.all_apps_home_button_label));
3699 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003700 return result;
3701 }
3702
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003703 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003704 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003705 */
3706 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3707 @Override
3708 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003709 closeSystemDialogs();
3710 }
3711 }
3712
3713 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003714 * Receives notifications whenever the appwidgets are reset.
3715 */
3716 private class AppWidgetResetObserver extends ContentObserver {
3717 public AppWidgetResetObserver() {
3718 super(new Handler());
3719 }
3720
3721 @Override
3722 public void onChange(boolean selfChange) {
3723 onAppWidgetReset();
3724 }
3725 }
3726
3727 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003728 * If the activity is currently paused, signal that we need to run the passed Runnable
3729 * in onResume.
3730 *
3731 * This needs to be called from incoming places where resources might have been loaded
3732 * while we are paused. That is becaues the Configuration might be wrong
3733 * when we're not running, and if it comes back to what it was when we
3734 * were paused, we are not restarted.
3735 *
3736 * Implementation of the method from LauncherModel.Callbacks.
3737 *
3738 * @return true if we are currently paused. The caller might be able to
3739 * skip some work in that case since we will come back again.
3740 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003741 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 if (mPaused) {
3743 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003744 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003745 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003746 }
3747 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003748 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003749 return true;
3750 } else {
3751 return false;
3752 }
3753 }
3754
Michael Jurkac402cd92013-05-20 15:49:32 +02003755 private boolean waitUntilResume(Runnable run) {
3756 return waitUntilResume(run, false);
3757 }
3758
Michael Jurka1e2f4652013-07-08 18:03:46 -07003759 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003760 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003761 }
3762
Michael Jurka7607c2f2013-04-03 14:33:19 -07003763 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003764 * If the activity is currently paused, signal that we need to re-run the loader
3765 * in onResume.
3766 *
3767 * This needs to be called from incoming places where resources might have been loaded
3768 * while we are paused. That is becaues the Configuration might be wrong
3769 * when we're not running, and if it comes back to what it was when we
3770 * were paused, we are not restarted.
3771 *
3772 * Implementation of the method from LauncherModel.Callbacks.
3773 *
3774 * @return true if we are currently paused. The caller might be able to
3775 * skip some work in that case since we will come back again.
3776 */
3777 public boolean setLoadOnResume() {
3778 if (mPaused) {
3779 Log.i(TAG, "setLoadOnResume");
3780 mOnResumeNeedsLoad = true;
3781 return true;
3782 } else {
3783 return false;
3784 }
3785 }
3786
3787 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003789 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003791 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003792 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003793 } else {
3794 return SCREEN_COUNT / 2;
3795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003797
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 /**
3799 * Refreshes the shortcuts shown on the workspace.
3800 *
3801 * Implementation of the method from LauncherModel.Callbacks.
3802 */
3803 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003804 mWorkspaceLoading = true;
3805
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 // If we're starting binding all over again, clear any bind calls we'd postponed in
3807 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3808 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003809 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003810
Winson Chungd64d1762013-08-20 14:37:16 -07003811 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003812 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003813 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003814
Michael Jurka05bf644e2011-11-30 20:28:53 -08003815 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003816 if (mHotseat != null) {
3817 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 }
3819 }
3820
Adam Cohendcd297f2013-06-18 13:13:40 -07003821 @Override
3822 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003823 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003824
Adam Cohen5084cba2013-09-03 12:01:16 -07003825 // If there are no screens, we need to have an empty screen
3826 if (orderedScreenIds.size() == 0) {
3827 mWorkspace.addExtraEmptyScreen();
3828 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003829
3830 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003831 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003832 if (hasCustomContentToLeft()) {
3833 mWorkspace.createCustomContentContainer();
3834 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003835 }
Winson Chung64359a52013-07-08 17:17:08 -07003836 }
3837
3838 @Override
3839 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003840 // Log to disk
3841 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3842 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3843 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003844 int count = orderedScreenIds.size();
3845 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003846 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003847 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003848 }
3849
Adam Cohen39a06042013-07-19 14:30:12 -07003850 private boolean shouldShowWeightWatcher() {
3851 String spKey = LauncherAppState.getSharedPreferencesKey();
3852 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003853 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003854
3855 return show;
3856 }
3857
3858 private void toggleShowWeightWatcher() {
3859 String spKey = LauncherAppState.getSharedPreferencesKey();
3860 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3861 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3862
3863 show = !show;
3864
3865 SharedPreferences.Editor editor = sp.edit();
3866 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3867 editor.commit();
3868
3869 if (mWeightWatcher != null) {
3870 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3871 }
3872 }
3873
Winson Chungd64d1762013-08-20 14:37:16 -07003874 public void bindAppsAdded(final ArrayList<Long> newScreens,
3875 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003876 final ArrayList<ItemInfo> addAnimated,
3877 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003878 Runnable r = new Runnable() {
3879 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003880 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003881 }
3882 };
3883 if (waitUntilResume(r)) {
3884 return;
3885 }
3886
Winson Chungd64d1762013-08-20 14:37:16 -07003887 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003888 if (newScreens != null) {
3889 bindAddScreens(newScreens);
3890 }
Winson Chungd64d1762013-08-20 14:37:16 -07003891
3892 // We add the items without animation on non-visible pages, and with
3893 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003894 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003895 bindItems(addNotAnimated, 0,
3896 addNotAnimated.size(), false);
3897 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003898 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003899 bindItems(addAnimated, 0,
3900 addAnimated.size(), true);
3901 }
Winson Chungc58497e2013-09-03 17:48:37 -07003902
Winson Chung87412982013-10-03 18:34:14 -07003903 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003904 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003905
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003906 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003907 addedApps != null && mAppsCustomizeContent != null) {
3908 mAppsCustomizeContent.addApps(addedApps);
3909 }
Winson Chungd64d1762013-08-20 14:37:16 -07003910 }
3911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003912 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 * Bind the items start-end from the list.
3914 *
3915 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003916 */
Winson Chung64359a52013-07-08 17:17:08 -07003917 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3918 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003919 Runnable r = new Runnable() {
3920 public void run() {
3921 bindItems(shortcuts, start, end, forceAnimateIcons);
3922 }
3923 };
3924 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003925 return;
3926 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003927
Winson Chungf0c6ae02012-03-21 16:10:31 -07003928 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003929 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3930 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003931 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003932 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003933 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003934 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003936
3937 // Short circuit if we are loading dock items for a configuration which has no dock
3938 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3939 mHotseat == null) {
3940 continue;
3941 }
3942
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 switch (item.itemType) {
3944 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3945 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003946 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003947 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003948
Winson Chung64359a52013-07-08 17:17:08 -07003949 /*
3950 * TODO: FIX collision case
3951 */
Winson Chungce376632013-07-11 16:12:41 -07003952 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3953 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3954 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003955 View v = cl.getChildAt(item.cellX, item.cellY);
3956 Object tag = v.getTag();
3957 String desc = "Collision while binding workspace item: " + item
3958 + ". Collides with " + tag;
3959 if (LauncherAppState.isDogfoodBuild()) {
3960 throw (new RuntimeException(desc));
3961 } else {
3962 Log.d(TAG, desc);
3963 }
Winson Chungce376632013-07-11 16:12:41 -07003964 }
Winson Chung64359a52013-07-08 17:17:08 -07003965 }
3966
Adam Cohendcd297f2013-06-18 13:13:40 -07003967 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3968 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003969 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003970 // Animate all the applications up now
3971 shortcut.setAlpha(0f);
3972 shortcut.setScaleX(0f);
3973 shortcut.setScaleY(0f);
3974 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003975 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003976 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003978 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003979 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003980 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003981 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003982 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3983 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003985 default:
3986 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003988 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003989
Winson Chung997a9232013-07-24 15:33:46 -07003990 if (animateIcons) {
3991 // Animate to the correct page
3992 if (newShortcutsScreenId > -1) {
3993 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003994 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003995 final Runnable startBounceAnimRunnable = new Runnable() {
3996 public void run() {
3997 anim.playTogether(bounceAnims);
3998 anim.start();
3999 }
4000 };
Winson Chung997a9232013-07-24 15:33:46 -07004001 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004002 // We post the animation slightly delayed to prevent slowdowns
4003 // when we are loading right after we return to launcher.
4004 mWorkspace.postDelayed(new Runnable() {
4005 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004006 if (mWorkspace != null) {
4007 mWorkspace.snapToPage(newScreenIndex);
4008 mWorkspace.postDelayed(startBounceAnimRunnable,
4009 NEW_APPS_ANIMATION_DELAY);
4010 }
Winson Chung94d67682013-09-25 16:29:40 -07004011 }
4012 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004013 } else {
4014 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004015 }
4016 }
Winson Chung64359a52013-07-08 17:17:08 -07004017 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004019 }
4020
Joe Onorato9c1289c2009-08-17 11:03:03 -04004021 /**
4022 * Implementation of the method from LauncherModel.Callbacks.
4023 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004024 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004025 Runnable r = new Runnable() {
4026 public void run() {
4027 bindFolders(folders);
4028 }
4029 };
4030 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004031 return;
4032 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004033 sFolders.clear();
4034 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004035 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036
4037 /**
4038 * Add the views for a widget to the workspace.
4039 *
4040 * Implementation of the method from LauncherModel.Callbacks.
4041 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004042 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004043 Runnable r = new Runnable() {
4044 public void run() {
4045 bindAppWidget(item);
4046 }
4047 };
4048 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004049 return;
4050 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004051
Daniel Sandler843e8602010-06-07 14:59:01 -04004052 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4053 if (DEBUG_WIDGETS) {
4054 Log.d(TAG, "bindAppWidget: " + item);
4055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 final Workspace workspace = mWorkspace;
4057
4058 final int appWidgetId = item.appWidgetId;
4059 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004060 if (DEBUG_WIDGETS) {
4061 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4062 }
4063
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4065
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004067 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068
Adam Cohendcd297f2013-06-18 13:13:40 -07004069 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004071 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4072
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 workspace.requestLayout();
4074
Daniel Sandler843e8602010-06-07 14:59:01 -04004075 if (DEBUG_WIDGETS) {
4076 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4077 + (SystemClock.uptimeMillis()-start) + "ms");
4078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 }
4080
Adam Cohen1462de32012-07-24 22:34:36 -07004081 public void onPageBoundSynchronously(int page) {
4082 mSynchronouslyBoundPages.add(page);
4083 }
4084
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085 /**
4086 * Callback saying that there aren't any more items to bind.
4087 *
4088 * Implementation of the method from LauncherModel.Callbacks.
4089 */
Adam Cohene25af792013-06-06 23:08:25 -07004090 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004091 Runnable r = new Runnable() {
4092 public void run() {
4093 finishBindingItems(upgradePath);
4094 }
4095 };
4096 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004097 return;
4098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 if (mSavedState != null) {
4100 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004101 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 mSavedState = null;
4104 }
4105
Adam Cohen1462de32012-07-24 22:34:36 -07004106 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004108 // If we received the result of any pending adds while the loader was running (e.g. the
4109 // widget configuration forced an orientation change), process them now.
4110 for (int i = 0; i < sPendingAddList.size(); i++) {
4111 completeAdd(sPendingAddList.get(i));
4112 }
4113 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114
Winson Chungc51db6a2011-10-05 11:44:49 -07004115 // Update the market app icon as necessary (the other icons will be managed in response to
4116 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004117 if (!DISABLE_MARKET_BUTTON) {
4118 updateAppMarketIcon();
4119 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004120
Winson Chungf0c6ae02012-03-21 16:10:31 -07004121 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004122 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004123 mWorkspace.getUniqueComponents(true, null);
4124 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004125 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004126 }
4127
Winson Chunga0b7e862013-09-05 16:03:15 -07004128 public boolean isAllAppsButtonRank(int rank) {
4129 if (mHotseat != null) {
4130 return mHotseat.isAllAppsButtonRank(rank);
4131 }
4132 return false;
4133 }
4134
Winson Chunga2413752012-04-03 14:22:34 -07004135 private boolean canRunNewAppsAnimation() {
4136 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4137 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4138 }
4139
Winson Chungc9168342013-06-26 14:54:55 -07004140 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4141 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4142 PropertyValuesHolder.ofFloat("alpha", 1f),
4143 PropertyValuesHolder.ofFloat("scaleX", 1f),
4144 PropertyValuesHolder.ofFloat("scaleY", 1f));
4145 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4146 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4147 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4148 return bounceAnim;
4149 }
4150
Adam Cohen27772732013-11-11 14:00:56 +00004151 public boolean useVerticalBarLayout() {
4152 return LauncherAppState.getInstance().getDynamicGrid().
4153 getDeviceProfile().isVerticalBarLayout();
4154 }
4155
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004156 protected Rect getSearchBarBounds() {
4157 return LauncherAppState.getInstance().getDynamicGrid().
4158 getDeviceProfile().getSearchBarBounds();
4159 }
4160
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004161 @Override
4162 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004163 boolean searchVisible = updateGlobalSearchIcon();
4164 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004165 if (mSearchDropTargetBar != null) {
4166 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4167 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004168 }
4169
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004170 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004171 * Add the icons for all apps.
4172 *
4173 * Implementation of the method from LauncherModel.Callbacks.
4174 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004175 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004176 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004177 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4178 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4179 getHotseat().addAllAppsFolder(mIconCache, apps,
4180 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4181 }
4182 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004183 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004184 if (mAppsCustomizeContent != null) {
4185 mAppsCustomizeContent.onPackagesUpdated(
4186 LauncherModel.getSortedWidgetsAndShortcuts(this));
4187 }
Winson Chungc58497e2013-09-03 17:48:37 -07004188 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004189 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004190 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004191 mAppsCustomizeContent.onPackagesUpdated(
4192 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004193 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004195 }
4196
4197 /**
4198 * A package was updated.
4199 *
4200 * Implementation of the method from LauncherModel.Callbacks.
4201 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004202 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004203 Runnable r = new Runnable() {
4204 public void run() {
4205 bindAppsUpdated(apps);
4206 }
4207 };
4208 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004209 return;
4210 }
4211
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004212 if (mWorkspace != null) {
4213 mWorkspace.updateShortcuts(apps);
4214 }
Winson Chungc58497e2013-09-03 17:48:37 -07004215
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004216 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004217 mAppsCustomizeContent != null) {
4218 mAppsCustomizeContent.updateApps(apps);
4219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 }
4221
4222 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004223 * Update the state of a package, typically related to install state.
4224 *
4225 * Implementation of the method from LauncherModel.Callbacks.
4226 */
4227 public void updatePackageState(String pkgName, int state) {
4228 if (mWorkspace != null) {
4229 mWorkspace.updatePackageState(pkgName, state);
4230 }
4231 }
4232
4233 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004234 * A package was uninstalled. We take both the super set of packageNames
4235 * in addition to specific applications to remove, the reason being that
4236 * this can be called when a package is updated as well. In that scenario,
4237 * we only remove specific components from the workspace, where as
4238 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Winson Chung83892cc2013-05-01 16:53:33 -07004242 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004243 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004244 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004245 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004246 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004247 }
Winson Chungd64d1762013-08-20 14:37:16 -07004248 };
4249 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004250 return;
4251 }
4252
Winson Chungdf95eb12013-10-16 14:57:07 -07004253 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004254 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004255 }
4256 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004257 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004258 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004259
Winson Chunga1820962011-10-03 16:31:06 -07004260 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004261 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004262
Winson Chungdf95eb12013-10-16 14:57:07 -07004263 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004264 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004265 mAppsCustomizeContent != null) {
4266 mAppsCustomizeContent.removeApps(appInfos);
4267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004268 }
Joe Onoratobe386092009-11-17 17:32:16 -08004269
4270 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004271 * A number of packages were updated.
4272 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004273 private ArrayList<Object> mWidgetsAndShortcuts;
4274 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004275 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004276 bindPackagesUpdated(mWidgetsAndShortcuts);
4277 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004278 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004280 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4281 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4282 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004283 return;
4284 }
4285
Winson Chung64359a52013-07-08 17:17:08 -07004286 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004287 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004288 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004289 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004290 }
4291
Winson Chung400438b2011-01-16 17:53:48 -08004292 private int mapConfigurationOriActivityInfoOri(int configOri) {
4293 final Display d = getWindowManager().getDefaultDisplay();
4294 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4295 switch (d.getRotation()) {
4296 case Surface.ROTATION_0:
4297 case Surface.ROTATION_180:
4298 // We are currently in the same basic orientation as the natural orientation
4299 naturalOri = configOri;
4300 break;
4301 case Surface.ROTATION_90:
4302 case Surface.ROTATION_270:
4303 // We are currently in the other basic orientation to the natural orientation
4304 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4305 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4306 break;
4307 }
4308
4309 int[] oriMap = {
4310 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4311 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4312 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4313 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4314 };
4315 // Since the map starts at portrait, we need to offset if this device's natural orientation
4316 // is landscape.
4317 int indexOffset = 0;
4318 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4319 indexOffset = 1;
4320 }
4321 return oriMap[(d.getRotation() + indexOffset) % 4];
4322 }
Adam Cohen446e9402011-09-15 18:21:21 -07004323
Winson Chung4b919f82012-05-01 10:44:08 -07004324 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004325 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004326 getResources().getBoolean(R.bool.allow_rotation);
4327 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004328 }
Winson Chung4b919f82012-05-01 10:44:08 -07004329 public void lockScreenOrientation() {
4330 if (isRotationEnabled()) {
4331 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4332 .getConfiguration().orientation));
4333 }
4334 }
4335 public void unlockScreenOrientation(boolean immediate) {
4336 if (isRotationEnabled()) {
4337 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004338 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004339 } else {
4340 mHandler.postDelayed(new Runnable() {
4341 public void run() {
4342 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4343 }
4344 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004345 }
Winson Chung4b919f82012-05-01 10:44:08 -07004346 }
Winson Chung400438b2011-01-16 17:53:48 -08004347 }
4348
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004349 /**
4350 * Called when the SearchBar hint should be changed.
4351 *
4352 * @param hint the hint to be displayed in the search bar.
4353 */
4354 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004355 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004356 }
4357
Winson Chunge43a1e72014-01-15 10:33:02 -08004358 protected boolean isLauncherPreinstalled() {
4359 PackageManager pm = getPackageManager();
4360 try {
4361 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4362 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4363 return true;
4364 } else {
4365 return false;
4366 }
4367 } catch (NameNotFoundException e) {
4368 e.printStackTrace();
4369 return false;
4370 }
4371 }
4372
Winson Chunge6eabff2013-09-24 11:01:23 -07004373 protected String getFirstRunClingSearchBarHint() {
4374 return "";
4375 }
4376 protected String getFirstRunCustomContentHint() {
4377 return "";
4378 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004379 protected int getFirstRunFocusedHotseatAppDrawableId() {
4380 return -1;
4381 }
4382 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4383 return null;
4384 }
4385 protected int getFirstRunFocusedHotseatAppRank() {
4386 return -1;
4387 }
4388 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4389 return "";
4390 }
4391 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4392 return "";
4393 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004394
Winson Chungaf40f202013-09-18 18:26:31 -07004395 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004396 mLauncherClings.dismissFirstRunCling(v);
4397 }
4398 public void dismissMigrationClingCopyApps(View v) {
4399 mLauncherClings.dismissMigrationClingCopyApps(v);
4400 }
4401 public void dismissMigrationClingUseDefault(View v) {
4402 mLauncherClings.dismissMigrationClingUseDefault(v);
4403 }
4404 public void dismissMigrationWorkspaceCling(View v) {
4405 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004406 }
Winson Chung82f55532011-08-09 14:14:23 -07004407 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004408 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004409 }
4410 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004411 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004412 }
4413
Adam Cohen432609a2014-03-13 17:03:22 -07004414
4415 /**
4416 * To be overridden by subclasses to indicate that there is an activity to launch
4417 * before showing the standard launcher experience.
4418 */
4419 protected boolean hasFirstRunActivity() {
4420 return false;
4421 }
4422
4423 /**
4424 * To be overridden by subclasses to launch any first run activity
4425 */
4426 protected Intent getFirstRunActivity() {
4427 return null;
4428 }
4429
Winson Chunga6945242014-01-08 14:04:34 -08004430 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004431 return !ActivityManager.isRunningInTestHarness() &&
4432 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004433 }
4434
Adam Cohen4a9423d2014-03-28 14:22:31 -07004435 protected boolean hasRunFirstRunActivity() {
4436 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4437 }
4438
Adam Cohen432609a2014-03-13 17:03:22 -07004439 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004440 if (shouldRunFirstRunActivity() &&
4441 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004442 Intent firstRunIntent = getFirstRunActivity();
4443 if (firstRunIntent != null) {
4444 startActivity(firstRunIntent);
4445 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004446 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004447 }
4448 }
Adam Cohen432609a2014-03-13 17:03:22 -07004449 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004450 }
4451
4452 private void markFirstRunActivityShown() {
4453 SharedPreferences.Editor editor = mSharedPrefs.edit();
4454 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4455 editor.apply();
4456 }
4457
Adam Cohen432609a2014-03-13 17:03:22 -07004458 /**
4459 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4460 * screen that must be displayed and dismissed.
4461 */
4462 protected boolean hasDismissableIntroScreen() {
4463 return false;
4464 }
4465
4466 /**
4467 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4468 */
4469 protected View getIntroScreen() {
4470 return null;
4471 }
4472
4473 /**
4474 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4475 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4476 */
4477 private boolean shouldShowIntroScreen() {
4478 return hasDismissableIntroScreen() &&
4479 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4480 }
4481
4482 protected void showIntroScreen() {
4483 View introScreen = getIntroScreen();
4484 changeWallpaperVisiblity(false);
4485 if (introScreen != null) {
4486 mDragLayer.showOverlayView(introScreen);
4487 }
4488 }
4489
4490 public void dismissIntroScreen() {
4491 markIntroScreenDismissed();
4492 if (showFirstRunActivity()) {
4493 // We delay hiding the intro view until the first run activity is showing. This
4494 // avoids a blip.
4495 mWorkspace.postDelayed(new Runnable() {
4496 @Override
4497 public void run() {
4498 mDragLayer.dismissOverlayView();
4499 }
4500 }, ACTIVITY_START_DELAY);
4501 } else {
4502 mDragLayer.dismissOverlayView();
4503 }
4504 changeWallpaperVisiblity(true);
4505 }
4506
4507 private void markIntroScreenDismissed() {
4508 SharedPreferences.Editor editor = mSharedPrefs.edit();
4509 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4510 editor.apply();
4511 }
4512
Winson Chunga6945242014-01-08 14:04:34 -08004513 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004514 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4515 if (mHotseat != null) mHotseat.setAlpha(1f);
4516 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4517 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004518 }
4519
4520 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004521 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4522 if (mHotseat != null) mHotseat.setAlpha(0f);
4523 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4524 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004525 }
4526
Mathew Inwood72fbec12013-11-19 15:45:07 +00004527 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004528 // Called from search suggestion, not supported in other profiles.
4529 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4530 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4531 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4532 myUser);
4533 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004534 return null;
4535 }
Kenny Guyed131872014-04-30 03:02:21 +01004536 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004537 }
4538
4539 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4540 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004541 // Called from search suggestion, not supported in other profiles.
4542 return createShortcutDragInfo(shortcutIntent, caption, icon,
4543 UserHandleCompat.myUserHandle());
4544 }
4545
4546 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4547 Bitmap icon, UserHandleCompat user) {
4548 return new ShortcutInfo(shortcutIntent, caption, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004549 }
4550
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004551 protected void moveWorkspaceToDefaultScreen() {
4552 mWorkspace.moveToDefaultScreen(false);
4553 }
4554
Mathew Inwood72fbec12013-11-19 15:45:07 +00004555 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4556 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004557 mWorkspace.onExternalDragStartedWithItem(dragView);
4558 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004559 }
4560
Anjali Koppalf5d29132014-02-28 13:33:27 -08004561 @Override
4562 public void onPageSwitch(View newPage, int newPageIndex) {
4563 }
4564
Winson Chung80baf5a2010-08-09 16:03:15 -07004565 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004566 * Prints out out state for debugging.
4567 */
4568 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004569 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004570 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004571 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4572 Log.d(TAG, "mRestoring=" + mRestoring);
4573 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4574 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004575 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004576 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004577
Winson Chung785d2eb2011-04-14 16:08:02 -07004578 if (mAppsCustomizeContent != null) {
4579 mAppsCustomizeContent.dumpState();
4580 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004581 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004582 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004583
4584 @Override
4585 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4586 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004587 synchronized (sDumpLogs) {
4588 writer.println(" ");
4589 writer.println("Debug logs: ");
4590 for (int i = 0; i < sDumpLogs.size(); i++) {
4591 writer.println(" " + sDumpLogs.get(i));
4592 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004593 }
4594 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004595
4596 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004597 if (DEBUG_DUMP_LOG) {
4598 synchronized (sDumpLogs) {
4599 Log.d(TAG, "");
4600 Log.d(TAG, "*********************");
4601 Log.d(TAG, "Launcher debug logs: ");
4602 for (int i = 0; i < sDumpLogs.size(); i++) {
4603 Log.d(TAG, " " + sDumpLogs.get(i));
4604 }
4605 Log.d(TAG, "*********************");
4606 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004608 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004609 }
4610
4611 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004612 addDumpLog(tag, log, null, debugLog);
4613 }
4614
4615 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004616 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004617 if (e != null) {
4618 Log.d(tag, log, e);
4619 } else {
4620 Log.d(tag, log);
4621 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004622 }
Winson Chungede41292013-09-19 16:27:36 -07004623 if (DEBUG_DUMP_LOG) {
4624 sDateStamp.setTime(System.currentTimeMillis());
4625 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004626 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4627 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004628 }
Winson Chungede41292013-09-19 16:27:36 -07004629 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004630 }
4631
Winson Chungede41292013-09-19 16:27:36 -07004632 public void dumpLogsToLocalData() {
4633 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004634 new AsyncTask<Void, Void, Void>() {
4635 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004636 boolean success = false;
4637 sDateStamp.setTime(sRunStart);
4638 String FILENAME = sDateStamp.getMonth() + "-"
4639 + sDateStamp.getDay() + "_"
4640 + sDateStamp.getHours() + "-"
4641 + sDateStamp.getMinutes() + "_"
4642 + sDateStamp.getSeconds() + ".txt";
4643
4644 FileOutputStream fos = null;
4645 File outFile = null;
4646 try {
4647 outFile = new File(getFilesDir(), FILENAME);
4648 outFile.createNewFile();
4649 fos = new FileOutputStream(outFile);
4650 } catch (Exception e) {
4651 e.printStackTrace();
4652 }
4653 if (fos != null) {
4654 PrintWriter writer = new PrintWriter(fos);
4655
4656 writer.println(" ");
4657 writer.println("Debug logs: ");
4658 synchronized (sDumpLogs) {
4659 for (int i = 0; i < sDumpLogs.size(); i++) {
4660 writer.println(" " + sDumpLogs.get(i));
4661 }
4662 }
4663 writer.close();
4664 }
4665 try {
4666 if (fos != null) {
4667 fos.close();
4668 success = true;
4669 }
4670 } catch (IOException e) {
4671 e.printStackTrace();
4672 }
Michael Jurka43467462013-11-14 12:03:58 +01004673 return null;
Winson Chungede41292013-09-19 16:27:36 -07004674 }
Michael Jurka43467462013-11-14 12:03:58 +01004675 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004676 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004678}
Michael Jurka2763be32011-02-24 11:19:57 -08004679
Michael Jurkaabded662011-03-04 12:06:57 -08004680interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004681 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004682 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004683 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004684 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004685 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004686}
Dan Sandlerd5024042014-01-09 15:01:33 -05004687
4688interface DebugIntents {
4689 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4690 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004691}