blob: 45c95736eb8bd0677a8ff0fb77813fe0a283f001 [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
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Bjorn Bringert85f418d2013-09-06 12:50:05 +010048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 private static final int REQUEST_BIND_APPWIDGET = 11;
140
Mathew Inwood876a8462013-06-14 14:12:41 +0100141 /**
142 * IntentStarter uses request codes starting with this. This must be greater than all activity
143 * request codes used internally.
144 */
145 protected static final int REQUEST_LAST = 100;
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800153 // To turn on these properties, type
154 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
155 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
156 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Winson Chung2672ff92012-05-04 16:22:30 -0700158 // The Intent extra that defines whether to ignore the launch animation
159 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400160 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
168 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: boolean
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
176 // Type: long
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100185 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700186 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100187 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700188 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700190
Adam Cohen39a06042013-07-19 14:30:12 -0700191 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700192 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700200 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
201 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chungaf40f202013-09-18 18:26:31 -0700202 private static final int SHOW_CLING_DURATION = 250;
203 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800206 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Winson Chunga2413752012-04-03 14:22:34 -0700208 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700209 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700220 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800221 private DragLayer mDragLayer;
222 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700223 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700236 private View mOverviewPanel;
237
Michael Jurka838a4ca2011-02-07 13:33:06 -0800238 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700239
Winson Chungc51db6a2011-10-05 11:44:49 -0700240 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700241 private AppsCustomizeTabHost mAppsCustomizeTabHost;
242 private AppsCustomizePagedView mAppsCustomizeContent;
243 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100244 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka1e2f4652013-07-08 18:03:46 -0700261 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700262 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
263
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // Keep track of whether the user has left launcher
265 private static boolean sPausedFromUserAction = false;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private Bundle mSavedInstanceState;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800271 private boolean mUserPresent = true;
272 private boolean mVisible = false;
273 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700274 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700275 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700277 private static LocaleConfiguration sLocaleConfiguration = null;
278
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700279 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
Adam Cohen61f560d2013-09-30 15:58:20 -0700281 private View.OnTouchListener mHapticFeedbackTouchListener;
282
Adam Cohended9f8d2010-11-03 13:25:16 -0700283 // Related to the auto-advancing of widgets
284 private final int ADVANCE_MSG = 1;
285 private final int mAdvanceInterval = 20000;
286 private final int mAdvanceStagger = 250;
287 private long mAutoAdvanceSentTime;
288 private long mAutoAdvanceTimeLeft = -1;
289 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
290 new HashMap<View, AppWidgetProviderInfo>();
291
Winson Chung400438b2011-01-16 17:53:48 -0800292 // Determines how long to wait after a rotation before restoring the screen orientation to
293 // match the sensor state.
294 private final int mRestoreScreenOrientationDelay = 500;
295
Michael Jurka4ef207b2010-11-29 17:05:45 -0800296 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700297 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800300
Winson Chungd64a6662013-09-30 11:06:59 -0700301 private Intent mAppMarketIntent = null;
302 private static final boolean DISABLE_MARKET_BUTTON = true;
303
Craig Mautner360310b2012-10-26 15:13:08 -0700304 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
307
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700308 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700309 static Date sDateStamp = new Date();
310 static DateFormat sDateFormat =
311 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
312 static long sRunStart = System.currentTimeMillis();
313 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700314
Winson Chung46353de2012-02-16 14:05:10 -0800315 // We only want to get the SharedPreferences once since it does an FS stat each time we get
316 // it from the context.
317 private SharedPreferences mSharedPrefs;
318
Adam Cohene25af792013-06-06 23:08:25 -0700319 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
320
Winson Chungf0c6ae02012-03-21 16:10:31 -0700321 // Holds the page that we need to animate to, and the icon views that we need to animate up
322 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700323 private ImageView mFolderIconImageView;
324 private Bitmap mFolderIconBitmap;
325 private Canvas mFolderIconCanvas;
326 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327
Michael Jurkaddd62e92011-02-16 17:49:14 -0800328 private BubbleTextView mWaitingForResume;
329
Michael Jurka7267fa52013-09-26 15:29:57 -0700330 protected TransparentBars mTransparentBars;
331
Michael Jurka22143132012-10-04 17:42:38 +0200332 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
333 = new HideFromAccessibilityHelper();
334
Michael Jurkac1f5d262011-09-30 19:32:27 -0700335 private Runnable mBuildLayersRunnable = new Runnable() {
336 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700337 if (mWorkspace != null) {
338 mWorkspace.buildPageHardwareLayers();
339 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700340 }
341 };
342
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800343 private static ArrayList<PendingAddArguments> sPendingAddList
344 = new ArrayList<PendingAddArguments>();
345
Michael Jurka7267fa52013-09-26 15:29:57 -0700346 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800347
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800348 private static class PendingAddArguments {
349 int requestCode;
350 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700351 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700352 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 int cellX;
354 int cellY;
355 }
356
Daniel Sandlerff02d492013-08-05 02:12:05 -0400357 private Stats mStats;
358
Michael Jurka0a457bf2012-11-19 14:05:05 -0800359 private static boolean isPropertyEnabled(String propertyName) {
360 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700361 }
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 @Override
364 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700365 if (DEBUG_STRICT_MODE) {
366 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
367 .detectDiskReads()
368 .detectDiskWrites()
369 .detectNetwork() // or .detectAll() for all detectable problems
370 .penaltyLog()
371 .build());
372 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
373 .detectLeakedSqlLiteObjects()
374 .detectLeakedClosableObjects()
375 .penaltyLog()
376 .penaltyDeath()
377 .build());
378 }
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400381
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400382 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400383 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700384
385 // Determine the dynamic grid properties
386 Point smallestSize = new Point();
387 Point largestSize = new Point();
388 Point realSize = new Point();
389 Display display = getWindowManager().getDefaultDisplay();
390 display.getCurrentSizeRange(smallestSize, largestSize);
391 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700392 DisplayMetrics dm = new DisplayMetrics();
393 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700394 // Lazy-initialize the dynamic grid
395 DeviceProfile grid = app.initDynamicGrid(this,
396 Math.min(smallestSize.x, smallestSize.y),
397 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700398 realSize.x, realSize.y,
399 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700400
401 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400402 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700403 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800404 mModel = app.setLauncher(this);
405 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400406 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700408
Daniel Sandlerff02d492013-08-05 02:12:05 -0400409 mStats = new Stats(this);
410
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700411 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700412 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
413 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700414
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700415 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
416 // this also ensures that any synchronous binding below doesn't re-trigger another
417 // LauncherModel load.
418 mPaused = false;
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200421 android.os.Debug.startMethodTracing(
422 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700427
428 mTransparentBars = new TransparentBars(findViewById(R.id.launcher));
429 mTransparentBars.requestTransparentBars(true);
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100432 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800434 registerContentObservers();
435
Joe Onorato7c312c12009-08-13 21:36:53 -0700436 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 mSavedState = savedInstanceState;
439 restoreState(mSavedState);
440
Winson Chunga12a2502010-12-20 14:41:35 -0800441 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700442 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200443 mAppsCustomizeContent.onPackagesUpdated(
444 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700445 }
Winson Chunga12a2502010-12-20 14:41:35 -0800446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
448 android.os.Debug.stopMethodTracing();
449 }
450
451 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700452 if (sPausedFromUserAction) {
453 // If the user leaves launcher, then we should just load items asynchronously when
454 // they return.
455 mModel.startLoader(true, -1);
456 } else {
457 // We only load the page synchronously if the user rotates (or triggers a
458 // configuration change) while launcher is in the foreground
459 mModel.startLoader(true, mWorkspace.getCurrentPage());
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462
463 // For handling default keys
464 mDefaultKeySsb = new SpannableStringBuilder();
465 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800466
467 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
468 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800469
Adam Cohenf9426d52012-06-04 17:26:21 -0700470 updateGlobalIcons();
471
472 // On large interfaces, we want the screen to auto-rotate based on the current orientation
473 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700474
475 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700476 }
477
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 protected void onUserLeaveHint() {
479 super.onUserLeaveHint();
480 sPausedFromUserAction = true;
481 }
482
Winson Chung98ca0f72013-07-29 12:58:51 -0700483 /** To be overriden by subclasses to hint to Launcher that we have custom content */
484 protected boolean hasCustomContentToLeft() {
485 return false;
486 }
487
Dave Hawkeya8881582013-09-17 15:55:33 -0600488 /**
489 * To be overridden by subclasses to create the custom content and call
490 * {@link #addToCustomContentPage}. This will only be invoked if
491 * {@link #hasCustomContentToLeft()} is {@code true}.
492 */
493 protected void addCustomContentToLeft() {
494 }
495
496 /**
497 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
498 * ensure the custom content page is added or removed if necessary.
499 */
500 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600501 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600502 // Not bound yet, wait for bindScreens to be called.
503 return;
504 }
505
506 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
507 // Create the custom content page and call the subclass to populate it.
508 mWorkspace.createCustomContentPage();
509 addCustomContentToLeft();
510 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
511 mWorkspace.removeCustomContentPage();
512 }
513 }
514
Adam Cohenf9426d52012-06-04 17:26:21 -0700515 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700516 boolean searchVisible = false;
517 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800518 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700519 int coi = getCurrentOrientationIndexForGlobalIcons();
520 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
521 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700522 if (!DISABLE_MARKET_BUTTON) {
523 updateAppMarketIcon();
524 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700525 searchVisible = updateGlobalSearchIcon();
526 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700527 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700528 if (sGlobalSearchIcon[coi] != null) {
529 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700530 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700531 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700532 if (sVoiceSearchIcon[coi] != null) {
533 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700534 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700535 }
Winson Chungd64a6662013-09-30 11:06:59 -0700536 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700537 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800538 }
Winson Chungadf0c182012-08-23 14:59:07 -0700539 if (mSearchDropTargetBar != null) {
540 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 }
Romain Guycbb89e42009-06-08 15:52:54 -0700543
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700545 if (sLocaleConfiguration == null) {
546 new AsyncTask<Void, Void, LocaleConfiguration>() {
547 @Override
548 protected LocaleConfiguration doInBackground(Void... unused) {
549 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
550 readConfiguration(Launcher.this, localeConfiguration);
551 return localeConfiguration;
552 }
553
554 @Override
555 protected void onPostExecute(LocaleConfiguration result) {
556 sLocaleConfiguration = result;
557 checkForLocaleChange(); // recursive, but now with a locale configuration
558 }
559 }.execute();
560 return;
561 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 final Configuration configuration = getResources().getConfiguration();
564
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700565 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 final String locale = configuration.locale.toString();
567
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700568 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 final int mcc = configuration.mcc;
570
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700571 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 final int mnc = configuration.mnc;
573
Romain Guy84f296c2009-11-04 15:00:44 -0800574 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575
Romain Guy84f296c2009-11-04 15:00:44 -0800576 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700577 sLocaleConfiguration.locale = locale;
578 sLocaleConfiguration.mcc = mcc;
579 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700580
Joe Onorato0589f0f2010-02-08 13:44:00 -0800581 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700582
583 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
584 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700585 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 public void run() {
587 writeConfiguration(Launcher.this, localeConfiguration);
588 }
589 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700590 }
591 }
592
593 private static class LocaleConfiguration {
594 public String locale;
595 public int mcc = -1;
596 public int mnc = -1;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
Romain Guy98d01652009-06-30 16:21:04 -0700599 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
600 DataInputStream in = null;
601 try {
602 in = new DataInputStream(context.openFileInput(PREFERENCES));
603 configuration.locale = in.readUTF();
604 configuration.mcc = in.readInt();
605 configuration.mnc = in.readInt();
606 } catch (FileNotFoundException e) {
607 // Ignore
608 } catch (IOException e) {
609 // Ignore
610 } finally {
611 if (in != null) {
612 try {
613 in.close();
614 } catch (IOException e) {
615 // Ignore
616 }
617 }
618 }
619 }
620
621 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
622 DataOutputStream out = null;
623 try {
624 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
625 out.writeUTF(configuration.locale);
626 out.writeInt(configuration.mcc);
627 out.writeInt(configuration.mnc);
628 out.flush();
629 } catch (FileNotFoundException e) {
630 // Ignore
631 } catch (IOException e) {
632 //noinspection ResultOfMethodCallIgnored
633 context.getFileStreamPath(PREFERENCES).delete();
634 } finally {
635 if (out != null) {
636 try {
637 out.close();
638 } catch (IOException e) {
639 // Ignore
640 }
641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
643 }
644
Anton Hanssona2f665f2013-09-26 12:18:32 +0100645 public Stats getStats() {
646 return mStats;
647 }
648
Bjorn Bringertc459e522013-06-07 19:36:01 +0100649 public LayoutInflater getInflater() {
650 return mInflater;
651 }
652
Adam Cohen716b51e2011-06-30 12:09:54 -0700653 public DragLayer getDragLayer() {
654 return mDragLayer;
655 }
656
Winson Chung36a62fe2012-05-06 18:04:42 -0700657 boolean isDraggingEnabled() {
658 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
659 // that is subsequently removed from the workspace in startBinding().
660 return !mModel.isLoadingWorkspace();
661 }
662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 static int getScreen() {
664 synchronized (sLock) {
665 return sScreen;
666 }
667 }
668
669 static void setScreen(int screen) {
670 synchronized (sLock) {
671 sScreen = screen;
672 }
673 }
674
Winson Chung557d6ed2011-07-08 15:34:52 -0700675 /**
676 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
677 * a configuration step, this allows the proper animations to run after other transitions.
678 */
679 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700680 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 switch (args.requestCode) {
682 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700684 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 break;
686 case REQUEST_PICK_SHORTCUT:
687 processShortcut(args.intent);
688 break;
689 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700690 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700691 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700692 result = true;
693 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 case REQUEST_CREATE_APPWIDGET:
695 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700696 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700697 result = true;
698 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800699 }
Michael Jurka27614d22012-04-02 06:40:22 -0700700 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
701 // if you turned the screen off and then back while in All Apps, Launcher would not
702 // return to the workspace. Clearing mAddInfo.container here fixes this issue
703 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700704 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700708 protected void onActivityResult(
709 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700710 // Reset the startActivity waiting flag
711 mWaitingForResult = false;
712
Michael Jurka8b805b12012-04-18 14:23:14 -0700713 if (requestCode == REQUEST_BIND_APPWIDGET) {
714 int appWidgetId = data != null ?
715 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
716 if (resultCode == RESULT_CANCELED) {
717 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
718 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700719 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 }
721 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200722 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
723 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
724 mWorkspace.exitOverviewMode(false);
725 }
726 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200728
Winson Chung557d6ed2011-07-08 15:34:52 -0700729 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
731 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700732
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 // We have special handling for widgets
734 if (isWidgetDrop) {
735 int appWidgetId = data != null ?
736 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700737 if (appWidgetId < 0) {
738 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
739 "widget configuration activity.");
740 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
741 } else {
742 completeTwoStageWidgetDrop(resultCode, appWidgetId);
743 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800744 return;
745 }
746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 // The pattern used here is that a user PICKs a specific application,
748 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
751 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700752 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 final PendingAddArguments args = new PendingAddArguments();
754 args.requestCode = requestCode;
755 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700757 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 args.cellX = mPendingAddInfo.cellX;
759 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800760 if (isWorkspaceLocked()) {
761 sPendingAddList.add(args);
762 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700763 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700767 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
769 null);
770 }
771
772 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 Runnable onCompleteRunnable = null;
776 int animationType = 0;
777
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700778 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 if (resultCode == RESULT_OK) {
780 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
781 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700782 mPendingAddWidgetInfo);
783 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 onCompleteRunnable = new Runnable() {
785 @Override
786 public void run() {
787 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
790 null);
791 }
792 };
793 } else if (resultCode == RESULT_CANCELED) {
794 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
795 onCompleteRunnable = new Runnable() {
796 @Override
797 public void run() {
798 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
799 null);
800 }
801 };
802 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700803 if (mDragLayer.getAnimatedView() != null) {
804 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
805 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
806 animationType, boundWidget, true);
807 } else {
808 // The animated view may be null in the case of a rotation during widget configuration
809 onCompleteRunnable.run();
810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812
813 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700814 protected void onStop() {
815 super.onStop();
816 FirstFrameAnimatorHelper.setIsVisible(false);
817 }
818
819 @Override
820 protected void onStart() {
821 super.onStart();
822 FirstFrameAnimatorHelper.setIsVisible(true);
823 }
824
825 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200827 long startTime = 0;
828 if (DEBUG_RESUME_TIME) {
829 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400830 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700833
Winson Chung4a2afa32012-07-19 14:53:05 -0700834 // Restore the previous launcher state
835 if (mOnResumeState == State.WORKSPACE) {
836 showWorkspace(false);
837 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chungc58497e2013-09-03 17:48:37 -0700838 showAllApps(false, AppsCustomizePagedView.ContentType.Applications);
Winson Chung4a2afa32012-07-19 14:53:05 -0700839 }
840 mOnResumeState = State.NONE;
841
Craig Mautner360310b2012-10-26 15:13:08 -0700842 // Background was set to gradient in onPause(), restore to black if in all apps.
843 setWorkspaceBackground(mState == State.WORKSPACE);
844
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800845 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700846 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700847 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700849 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700851 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700853 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200854 // We might have postponed some bind calls until onResume (see waitUntilResume) --
855 // execute them here
856 long startTimeCallbacks = 0;
857 if (DEBUG_RESUME_TIME) {
858 startTimeCallbacks = System.currentTimeMillis();
859 }
860
861 if (mAppsCustomizeContent != null) {
862 mAppsCustomizeContent.setBulkBind(true);
863 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700864 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
865 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200866 }
867 if (mAppsCustomizeContent != null) {
868 mAppsCustomizeContent.setBulkBind(false);
869 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700870 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200871 if (DEBUG_RESUME_TIME) {
872 Log.d(TAG, "Time spent processing callbacks in onResume: " +
873 (System.currentTimeMillis() - startTimeCallbacks));
874 }
Michael Jurka447bf842013-05-15 14:52:15 +0200875 }
Michael Jurka54554252013-08-01 12:52:23 +0200876 if (mOnResumeCallbacks.size() > 0) {
877 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
878 mOnResumeCallbacks.get(i).run();
879 }
880 mOnResumeCallbacks.clear();
881 }
Winson Chunge4e50662012-01-23 14:45:13 -0800882
883 // Reset the pressed state of icons that were locked in the press state while activities
884 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800885 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800886 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800887 mWaitingForResume.setStayPressed(false);
888 }
Winson Chunge4e50662012-01-23 14:45:13 -0800889 if (mAppsCustomizeContent != null) {
890 // Resets the previous all apps icon press state
891 mAppsCustomizeContent.resetDrawableState();
892 }
Winson Chung780fe592013-09-26 14:48:44 -0700893 // Reset AllApps to its initial state
894 if (mAppsCustomizeTabHost != null) {
895 mAppsCustomizeTabHost.reset();
896 }
Adam Cohen06dff352012-06-01 17:17:08 -0700897 // It is possible that widgets can receive updates while launcher is not in the foreground.
898 // Consequently, the widgets will be inflated in the orientation of the foreground activity
899 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
900 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700901 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700902
Winson Chung780fe592013-09-26 14:48:44 -0700903 // Process any items that were added while Launcher was away.
904 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
905
Winson Chung5841efa2013-09-30 18:06:44 -0700906 // Update the voice search button proxy
907 updateVoiceButtonProxyVisible(false);
908
Adam Cohenf9426d52012-06-04 17:26:21 -0700909 // Again, as with the above scenario, it's possible that one or more of the global icons
910 // were updated in the wrong orientation.
911 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200912 if (DEBUG_RESUME_TIME) {
913 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
914 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700915
916 if (mWorkspace.getCustomContentCallbacks() != null) {
917 // If we are resuming and the custom content is the current page, we call onShow().
918 // It is also poassible that onShow will instead be called slightly after first layout
919 // if PagedView#setRestorePage was set to the custom content page in onCreate().
920 if (mWorkspace.isOnOrMovingToCustomContent()) {
921 mWorkspace.getCustomContentCallbacks().onShow();
922 }
923 }
924
Adam Cohenedaaa302013-10-01 17:33:27 -0700925 mWorkspace.updateInteractionForState();
Adam Cohen06dff352012-06-01 17:17:08 -0700926 }
927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700929 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700930 // Ensure that items added to Launcher are queued until Launcher returns
931 InstallShortcutReceiver.enableInstallQueue();
932
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700933 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700934 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800935 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700936 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700937
938 // We call onHide() aggressively. The custom content callbacks should be able to
939 // debounce excess onHide calls.
940 if (mWorkspace.getCustomContentCallbacks() != null) {
941 mWorkspace.getCustomContentCallbacks().onHide();
942 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 }
Romain Guycbb89e42009-06-08 15:52:54 -0700944
Adam Cohen66a01fd2013-06-11 12:48:00 -0700945 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700946 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600947 addCustomContentToLeft();
948 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700949 }
950
Adam Cohenbffe7452013-07-22 18:21:45 -0700951 QSBScroller mQsbScroller = new QSBScroller() {
952 int scrollY = 0;
953
954 @Override
955 public void setScrollY(int scroll) {
956 scrollY = scroll;
957
958 if (mWorkspace.isOnOrMovingToCustomContent()) {
959 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100960 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700961 }
962 }
963 };
964
965 public void resetQSBScroll() {
966 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100967 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700968 }
969
970 public interface CustomContentCallbacks {
971 // Custom content is completely shown
972 public void onShow();
973
974 // Custom content is completely hidden
975 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700976
977 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
978 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700979 }
980
Adam Cohen30bacb22013-08-29 14:25:25 -0700981 protected void startSettings() {
982 }
983
Adam Cohenbffe7452013-07-22 18:21:45 -0700984 public interface QSBScroller {
985 public void setScrollY(int scrollY);
986 }
987
Adam Cohen66a01fd2013-06-11 12:48:00 -0700988 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700989 public QSBScroller addToCustomContentPage(View customContent) {
990 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700991 }
992
Winson Chung98ca0f72013-07-29 12:58:51 -0700993 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700994 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700995 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700996 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700997 }
998
Adam Cohenedb40762013-07-18 16:45:45 -0700999 // The custom content needs to offset its content to account for the QSB
1000 public int getTopOffsetForCustomContent() {
1001 return mWorkspace.getPaddingTop();
1002 }
1003
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001004 @Override
1005 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 // Flag the loader to stop early before switching
1007 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001008 if (mAppsCustomizeContent != null) {
1009 mAppsCustomizeContent.surrender();
1010 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001011 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001012 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001013
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001014 // We can't hide the IME if it was forced open. So don't bother
1015 /*
1016 @Override
1017 public void onWindowFocusChanged(boolean hasFocus) {
1018 super.onWindowFocusChanged(hasFocus);
1019
1020 if (hasFocus) {
1021 final InputMethodManager inputManager = (InputMethodManager)
1022 getSystemService(Context.INPUT_METHOD_SERVICE);
1023 WindowManager.LayoutParams lp = getWindow().getAttributes();
1024 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
1025 android.os.Handler()) {
1026 protected void onReceiveResult(int resultCode, Bundle resultData) {
1027 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
1028 }
1029 });
1030 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
1031 }
1032 }
1033 */
1034
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 private boolean acceptFilter() {
1036 final InputMethodManager inputManager = (InputMethodManager)
1037 getSystemService(Context.INPUT_METHOD_SERVICE);
1038 return !inputManager.isFullscreenMode();
1039 }
1040
1041 @Override
1042 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001043 final int uniChar = event.getUnicodeChar();
1044 final boolean handled = super.onKeyDown(keyCode, event);
1045 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1046 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1048 keyCode, event);
1049 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001051 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001052 // showSearchDialog()
1053 // If there are multiple keystrokes before the search dialog takes focus,
1054 // onSearchRequested() will be called for every keystroke,
1055 // but it is idempotent, so it's fine.
1056 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058 }
1059
Joe Onorato8a9625e2010-01-28 15:55:35 -08001060 // Eat the long press event so the keyboard doesn't come up.
1061 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1062 return true;
1063 }
1064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 return handled;
1066 }
1067
Karl Rosaen138a0412009-04-23 19:00:21 -07001068 private String getTypedText() {
1069 return mDefaultKeySsb.toString();
1070 }
1071
1072 private void clearTypedText() {
1073 mDefaultKeySsb.clear();
1074 mDefaultKeySsb.clearSpans();
1075 Selection.setSelection(mDefaultKeySsb, 0);
1076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001079 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1080 * State
1081 */
1082 private static State intToState(int stateOrdinal) {
1083 State state = State.WORKSPACE;
1084 final State[] stateValues = State.values();
1085 for (int i = 0; i < stateValues.length; i++) {
1086 if (stateValues[i].ordinal() == stateOrdinal) {
1087 state = stateValues[i];
1088 break;
1089 }
1090 }
1091 return state;
1092 }
1093
1094 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 * Restores the previous state, if it exists.
1096 *
1097 * @param savedState The previous state.
1098 */
1099 private void restoreState(Bundle savedState) {
1100 if (savedState == null) {
1101 return;
1102 }
1103
Michael Jurka883f55b2010-10-21 15:47:14 -07001104 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001105 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001106 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001107 }
1108
Winson Chungf0c6ae02012-03-21 16:10:31 -07001109 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001111 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Winson Chung3d503fb2011-07-13 17:25:49 -07001114 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116
Winson Chung3d503fb2011-07-13 17:25:49 -07001117 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1118 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001119 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1121 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001122 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1123 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1124 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001125 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 mRestoring = true;
1127 }
1128
1129 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1130 if (renameFolder) {
1131 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001132 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 mRestoring = true;
1134 }
Winson Chunga12a2502010-12-20 14:41:35 -08001135
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 // Restore the AppsCustomize tab
1137 if (mAppsCustomizeTabHost != null) {
1138 String curTab = savedState.getString("apps_customize_currentTab");
1139 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001140 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001142 mAppsCustomizeContent.loadAssociatedPages(
1143 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 }
1145
Winson Chung5afbf7b2011-07-25 11:53:08 -07001146 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1147 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 }
1150
1151 /**
1152 * Finds all the views we need and configure them properly.
1153 */
1154 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001155 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001156
Craig Mautner360310b2012-10-26 15:13:08 -07001157 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001158 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1159 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001160
John Spurlock77e1f472013-09-11 10:09:51 -04001161 mLauncherView.setSystemUiVisibility(
1162 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001163 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164
Winson Chung4c98d922011-05-31 16:50:48 -07001165 // Setup the drag layer
1166 mDragLayer.setup(this, dragController);
1167
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 // Setup the hotseat
1169 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1170 if (mHotseat != null) {
1171 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001172 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001173 }
1174
Adam Cohenf358a4b2013-07-23 16:47:31 -07001175 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001176 View widgetButton = findViewById(R.id.widget_button);
1177 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001178 @Override
1179 public void onClick(View arg0) {
Winson Chungc58497e2013-09-03 17:48:37 -07001180 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001181 }
1182 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001183 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1184
1185 View wallpaperButton = findViewById(R.id.wallpaper_button);
1186 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001187 @Override
1188 public void onClick(View arg0) {
1189 startWallpaper();
1190 }
1191 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001192 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1193
1194 View settingsButton = findViewById(R.id.settings_button);
1195 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001196 @Override
1197 public void onClick(View arg0) {
1198 startSettings();
1199 }
1200 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001201 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001202 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001203
Winson Chung4c98d922011-05-31 16:50:48 -07001204 // Setup the workspace
1205 mWorkspace.setHapticFeedbackEnabled(false);
1206 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001207 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001208 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001209
Winson Chungf0ea4d32011-06-06 14:27:16 -07001210 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001211 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001212
Winson Chungf0ea4d32011-06-06 14:27:16 -07001213 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001214 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001215 mAppsCustomizeContent = (AppsCustomizePagedView)
1216 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1217 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001218
Winson Chung3d503fb2011-07-13 17:25:49 -07001219 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001220 dragController.setDragScoller(mWorkspace);
1221 dragController.setScrollView(mDragLayer);
1222 dragController.setMoveTarget(mWorkspace);
1223 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001224 if (mSearchDropTargetBar != null) {
1225 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001226 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001227
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001228 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001229 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001230 mWeightWatcher = new WeightWatcher(this);
1231 mWeightWatcher.setAlpha(0.5f);
1232 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001233 new FrameLayout.LayoutParams(
1234 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001235 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001236 Gravity.BOTTOM)
1237 );
Adam Cohen39a06042013-07-19 14:30:12 -07001238
1239 boolean show = shouldShowWeightWatcher();
1240 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
1244 /**
1245 * Creates a view representing a shortcut.
1246 *
1247 * @param info The data structure describing the shortcut.
1248 *
1249 * @return A View inflated from R.layout.application.
1250 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001251 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001253 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
1256 /**
1257 * Creates a view representing a shortcut inflated from the specified resource.
1258 *
1259 * @param layoutResId The id of the XML layout used to create the shortcut.
1260 * @param parent The group the shortcut belongs to.
1261 * @param info The data structure describing the shortcut.
1262 *
1263 * @return A View inflated from layoutResId.
1264 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001265 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001266 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1267 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 return favorite;
1270 }
1271
1272 /**
1273 * Add an application shortcut to the workspace.
1274 *
1275 * @param data The intent describing the application.
1276 * @param cellInfo The position on screen where to create the shortcut.
1277 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001278 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001280 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001281
Adam Cohenfbba09b2011-07-18 21:43:05 -07001282 // First we check if we already know the exact location where we want to add this item.
1283 if (cellX >= 0 && cellY >= 0) {
1284 cellXY[0] = cellX;
1285 cellXY[1] = cellY;
1286 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001287 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 return;
1289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001291 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001292
Romain Guy73b979d2009-06-09 12:57:21 -07001293 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001294 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001296 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001297 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001298 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001299 } else {
1300 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
Romain Guycbb89e42009-06-08 15:52:54 -07001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 /**
1305 * Add a shortcut to the workspace.
1306 *
1307 * @param data The intent describing the shortcut.
1308 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001310 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 int cellY) {
1312 int[] cellXY = mTmpAddItemCellCoordinates;
1313 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Michael Jurkad3ef3062010-11-23 16:23:58 -08001316 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001317
Adam Cohen558baaf2011-08-15 15:22:57 -07001318 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001319 if (info == null) {
1320 return;
1321 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001322 final View view = createShortcut(info);
1323
Adam Cohenfbba09b2011-07-18 21:43:05 -07001324 // First we check if we already know the exact location where we want to add this item.
1325 if (cellX >= 0 && cellY >= 0) {
1326 cellXY[0] = cellX;
1327 cellXY[1] = cellY;
1328 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001329
1330 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001331 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001332 true, null,null)) {
1333 return;
1334 }
1335 DragObject dragObject = new DragObject();
1336 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001337 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1338 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001339 return;
1340 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001341 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001342 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001344 foundCellSpan = (result != null);
1345 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001346 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001347 }
1348
1349 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001350 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 return;
1352 }
1353
Adam Cohendcd297f2013-06-18 13:13:40 -07001354 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
1356 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360 }
1361
Adam Cohen2f093b62012-04-30 18:59:53 -07001362 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1363 int minHeight) {
1364 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001365 // We want to account for the extra amount of padding that we are adding to the widget
1366 // to ensure that it gets the full amount of space that it has requested
1367 int requiredWidth = minWidth + padding.left + padding.right;
1368 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001369 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001370 }
1371
Adam Cohen2f093b62012-04-30 18:59:53 -07001372 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1373 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001374 }
1375
Adam Cohen2f093b62012-04-30 18:59:53 -07001376 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1377 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001378 }
1379
Adam Cohen2f093b62012-04-30 18:59:53 -07001380 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1381 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001382 }
1383
Adam Cohen2f093b62012-04-30 18:59:53 -07001384 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1385 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001386 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001390 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001392 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001393 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001395 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001396 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1397 if (appWidgetInfo == null) {
1398 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001401 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001403
Adam Cohen2f093b62012-04-30 18:59:53 -07001404 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1405 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001408 // We have saved the position to which the widget was dragged-- this really only matters
1409 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 int[] cellXY = mTmpAddItemCellCoordinates;
1411 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001412 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001413 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1415 cellXY[0] = mPendingAddInfo.cellX;
1416 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001417 spanXY[0] = mPendingAddInfo.spanX;
1418 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001419 foundCellSpan = true;
1420 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001421 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001423 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1424 spanXY[1], cellXY, finalSpan);
1425 spanXY[0] = finalSpan[0];
1426 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001427 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001429 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001430 }
1431
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001433 if (appWidgetId != -1) {
1434 // Deleting an app widget ID is a void call but writes to disk before returning
1435 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001436 new Thread("deleteAppWidgetId") {
1437 public void run() {
1438 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1439 }
1440 }.start();
1441 }
Winson Chung93eef082012-03-23 15:59:27 -07001442 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001443 return;
1444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001446 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001447 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1448 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001449 launcherInfo.spanX = spanXY[0];
1450 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001451 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1452 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001455 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
1457 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001458 if (hostView == null) {
1459 // Perform actual inflation because we're live
1460 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1461 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1462 } else {
1463 // The AppWidgetHostView has already been inflated and instantiated
1464 launcherInfo.hostView = hostView;
1465 }
Romain Guycbb89e42009-06-08 15:52:54 -07001466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001468 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001469 launcherInfo.notifyWidgetSizeChanged(this);
1470
Adam Cohendcd297f2013-06-18 13:13:40 -07001471 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001473
1474 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001476 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Adam Cohended9f8d2010-11-03 13:25:16 -07001479 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1480 @Override
1481 public void onReceive(Context context, Intent intent) {
1482 final String action = intent.getAction();
1483 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1484 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001485 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001486 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001487
Winson Chungc100e8e2011-08-09 16:02:43 -07001488 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001489 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001490 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001491 showWorkspaceAndExitOverviewMode(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001492 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1494 mUserPresent = true;
1495 updateRunning();
1496 }
1497 }
1498 };
1499
1500 @Override
1501 public void onAttachedToWindow() {
1502 super.onAttachedToWindow();
1503
1504 // Listen for broadcasts related to user-presence
1505 final IntentFilter filter = new IntentFilter();
1506 filter.addAction(Intent.ACTION_SCREEN_OFF);
1507 filter.addAction(Intent.ACTION_USER_PRESENT);
1508 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001509 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001510 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001511 mVisible = true;
1512 }
1513
1514 @Override
1515 public void onDetachedFromWindow() {
1516 super.onDetachedFromWindow();
1517 mVisible = false;
1518
Adam Cohend113e0c2010-11-11 10:48:05 -08001519 if (mAttached) {
1520 unregisterReceiver(mReceiver);
1521 mAttached = false;
1522 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001523 updateRunning();
1524 }
1525
1526 public void onWindowVisibilityChanged(int visibility) {
1527 mVisible = visibility == View.VISIBLE;
1528 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001529 // The following code used to be in onResume, but it turns out onResume is called when
1530 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1531 // is a more appropriate event to handle
1532 if (mVisible) {
1533 mAppsCustomizeTabHost.onWindowVisible();
1534 if (!mWorkspaceLoading) {
1535 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001536 // We want to let Launcher draw itself at least once before we force it to build
1537 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001538 // apps is nice and speedy.
1539 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001540 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001541 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001542 if (mStarted) return;
1543 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001544 // We delay the layer building a bit in order to give
1545 // other message processing a time to run. In particular
1546 // this avoids a delay in hiding the IME if it was
1547 // currently shown, because doing that may involve
1548 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001549 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001550 final ViewTreeObserver.OnDrawListener listener = this;
1551 mWorkspace.post(new Runnable() {
1552 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001553 if (mWorkspace != null &&
1554 mWorkspace.getViewTreeObserver() != null) {
1555 mWorkspace.getViewTreeObserver().
1556 removeOnDrawListener(listener);
1557 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001558 }
1559 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001560 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001561 }
1562 });
1563 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001564 // When Launcher comes back to foreground, a different Activity might be responsible for
1565 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001566 if (!DISABLE_MARKET_BUTTON) {
1567 updateAppMarketIcon();
1568 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001569 clearTypedText();
1570 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 }
1572
1573 private void sendAdvanceMessage(long delay) {
1574 mHandler.removeMessages(ADVANCE_MSG);
1575 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1576 mHandler.sendMessageDelayed(msg, delay);
1577 mAutoAdvanceSentTime = System.currentTimeMillis();
1578 }
1579
1580 private void updateRunning() {
1581 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1582 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1583 mAutoAdvanceRunning = autoAdvanceRunning;
1584 if (autoAdvanceRunning) {
1585 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1586 sendAdvanceMessage(delay);
1587 } else {
1588 if (!mWidgetsToAdvance.isEmpty()) {
1589 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1590 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1591 }
1592 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001593 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595 }
1596 }
1597
1598 private final Handler mHandler = new Handler() {
1599 @Override
1600 public void handleMessage(Message msg) {
1601 if (msg.what == ADVANCE_MSG) {
1602 int i = 0;
1603 for (View key: mWidgetsToAdvance.keySet()) {
1604 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1605 final int delay = mAdvanceStagger * i;
1606 if (v instanceof Advanceable) {
1607 postDelayed(new Runnable() {
1608 public void run() {
1609 ((Advanceable) v).advance();
1610 }
1611 }, delay);
1612 }
1613 i++;
1614 }
1615 sendAdvanceMessage(mAdvanceInterval);
1616 }
1617 }
1618 };
1619
1620 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001621 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1623 if (v instanceof Advanceable) {
1624 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001625 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 updateRunning();
1627 }
1628 }
1629
1630 void removeWidgetToAutoAdvance(View hostView) {
1631 if (mWidgetsToAdvance.containsKey(hostView)) {
1632 mWidgetsToAdvance.remove(hostView);
1633 updateRunning();
1634 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001635 }
1636
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001639 launcherInfo.hostView = null;
1640 }
1641
Winson Chung93eef082012-03-23 15:59:27 -07001642 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1643 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1644 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001647 public LauncherAppWidgetHost getAppWidgetHost() {
1648 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Winson Chunga9abd0e2010-10-27 17:18:37 -07001651 public LauncherModel getModel() {
1652 return mModel;
1653 }
1654
Bjorn Bringertc459e522013-06-07 19:36:01 +01001655 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001656 getWindow().closeAllPanels();
1657
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001658 // Whatever we were doing is hereby canceled.
1659 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 @Override
1663 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001664 long startTime = 0;
1665 if (DEBUG_RESUME_TIME) {
1666 startTime = System.currentTimeMillis();
1667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 super.onNewIntent(intent);
1669
1670 // Close the menu
1671 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001672 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001673 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001674
Jamie Genniscb222e82012-10-23 15:44:26 -07001675 final boolean alreadyOnHome =
1676 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001677 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001678
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 if (mWorkspace == null) {
1680 // Can be cases where mWorkspace is null, this prevents a NPE
1681 return;
1682 }
1683 Folder openFolder = mWorkspace.getOpenFolder();
1684 // In all these cases, only animate if we're already on home
1685 mWorkspace.exitWidgetResizeMode();
1686 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1687 openFolder == null) {
1688 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001689 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001690
Adam Cohen6fecd412013-10-02 17:41:50 -07001691 closeFolder();
1692 exitSpringLoadedDragMode();
1693
1694 // If we are already on home, then just animate back to the workspace,
1695 // otherwise, just wait until onResume to set the state back to Workspace
1696 if (alreadyOnHome) {
1697 showWorkspaceAndExitOverviewMode(true);
1698 } else {
1699 mOnResumeState = State.WORKSPACE;
1700 }
1701
1702 final View v = getWindow().peekDecorView();
1703 if (v != null && v.getWindowToken() != null) {
1704 InputMethodManager imm = (InputMethodManager)getSystemService(
1705 INPUT_METHOD_SERVICE);
1706 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1707 }
1708
1709 // Reset the apps customize page
1710 if (mAppsCustomizeTabHost != null) {
1711 mAppsCustomizeTabHost.reset();
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Michael Jurka447bf842013-05-15 14:52:15 +02001714 if (DEBUG_RESUME_TIME) {
1715 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
Adam Cohen040a5d22013-09-16 17:09:33 -07001719 protected void showWorkspaceAndExitOverviewMode(boolean animate) {
1720 showWorkspace(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001721 if (mWorkspace.isInOverviewMode()) {
Adam Cohen040a5d22013-09-16 17:09:33 -07001722 mWorkspace.exitOverviewMode(animate);
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001723 }
1724 }
Adam Cohen040a5d22013-09-16 17:09:33 -07001725 protected void showWorkspaceAndExitOverviewMode() {
Allan Wojciechowskifdc43022013-10-02 11:13:17 -04001726 showWorkspaceAndExitOverviewMode(false);
Adam Cohen040a5d22013-09-16 17:09:33 -07001727 }
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001730 public void onRestoreInstanceState(Bundle state) {
1731 super.onRestoreInstanceState(state);
1732 for (int page: mSynchronouslyBoundPages) {
1733 mWorkspace.restoreInstanceStateForChild(page);
1734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
1736
1737 @Override
1738 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001739 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001740 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741
Michael Jurka883f55b2010-10-21 15:47:14 -07001742 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001743 // We close any open folder since it will not be re-opened, and we need to make sure
1744 // this state is reflected.
1745 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746
Adam Cohendcd297f2013-06-18 13:13:40 -07001747 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001748 mWaitingForResult) {
1749 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001750 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001751 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1752 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001753 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1754 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1755 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757
1758 if (mFolderInfo != null && mWaitingForResult) {
1759 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1760 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1761 }
Michael Jurka946ad472010-07-09 18:05:18 -07001762
Winson Chung785d2eb2011-04-14 16:08:02 -07001763 // Save the current AppsCustomize tab
1764 if (mAppsCustomizeTabHost != null) {
1765 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1766 if (currentTabTag != null) {
1767 outState.putString("apps_customize_currentTab", currentTabTag);
1768 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001769 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1770 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773
1774 @Override
1775 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001777
Winson Chunge7a03942011-08-05 15:05:12 -07001778 // Remove all pending runnables
1779 mHandler.removeMessages(ADVANCE_MSG);
1780 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001781 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001782
Winson Chungcd2b0142011-06-08 16:02:26 -07001783 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001784 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001785 mModel.stopLoader();
1786 app.setLauncher(null);
1787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001789 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001791 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001793 mAppWidgetHost = null;
1794
1795 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796
1797 TextKeyListener.getInstance().release();
1798
Winson Chung81b52252012-08-27 15:34:29 -07001799 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1800 // to prevent leaking Launcher activities on orientation change.
1801 if (mModel != null) {
1802 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1803 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001804
1805 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001806 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001807
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001808 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001809 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1810 mWorkspace.removeAllViews();
1811 mWorkspace = null;
1812 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001813
Michael Jurka2ecf9952012-06-18 12:52:28 -07001814 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816
Adam Cohena9cf38f2011-05-02 15:36:58 -07001817 public DragController getDragController() {
1818 return mDragController;
1819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 @Override
1822 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001823 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 super.startActivityForResult(intent, requestCode);
1825 }
1826
Winson Chung70d72102011-08-12 11:24:35 -07001827 /**
1828 * Indicates that we want global search for this activity by setting the globalSearch
1829 * argument for {@link #startSearch} to true.
1830 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001832 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001834
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001835 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001836
Karl Rosaen138a0412009-04-23 19:00:21 -07001837 if (initialQuery == null) {
1838 // Use any text typed in the launcher as the initial query
1839 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 if (appSearchData == null) {
1842 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001843 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
Winson Chungadf0c182012-08-23 14:59:07 -07001845 Rect sourceBounds = new Rect();
1846 if (mSearchDropTargetBar != null) {
1847 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1848 }
Romain Guycbb89e42009-06-08 15:52:54 -07001849
Bjorn Bringertc459e522013-06-07 19:36:01 +01001850 startSearch(initialQuery, selectInitialQuery,
1851 appSearchData, sourceBounds);
1852 }
1853
1854 public void startSearch(String initialQuery,
1855 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001856 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001857 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001858 }
1859
1860 /**
1861 * Starts the global search activity. This code is a copied from SearchManager
1862 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001863 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001864 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001865 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001866 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1867 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1868 if (globalSearchActivity == null) {
1869 Log.w(TAG, "No global search activity found.");
1870 return;
1871 }
1872 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1873 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1874 intent.setComponent(globalSearchActivity);
1875 // Make sure that we have a Bundle to put source in
1876 if (appSearchData == null) {
1877 appSearchData = new Bundle();
1878 } else {
1879 appSearchData = new Bundle(appSearchData);
1880 }
1881 // Set source to package name of app that starts global search, if not set already.
1882 if (!appSearchData.containsKey("source")) {
1883 appSearchData.putString("source", getPackageName());
1884 }
1885 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1886 if (!TextUtils.isEmpty(initialQuery)) {
1887 intent.putExtra(SearchManager.QUERY, initialQuery);
1888 }
1889 if (selectInitialQuery) {
1890 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1891 }
1892 intent.setSourceBounds(sourceBounds);
1893 try {
1894 startActivity(intent);
1895 } catch (ActivityNotFoundException ex) {
1896 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
Winson Chung70d72102011-08-12 11:24:35 -07001900 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001901 public boolean onPrepareOptionsMenu(Menu menu) {
1902 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001903 if (!mWorkspace.isInOverviewMode()) {
1904 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001905 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001906 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001909 @Override
1910 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001911 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001912 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001913 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001914 }
1915
Joe Onorato9c1289c2009-08-17 11:03:03 -04001916 public boolean isWorkspaceLocked() {
1917 return mWorkspaceLoading || mWaitingForResult;
1918 }
1919
Michael Jurka0280c3b2010-09-17 15:00:07 -07001920 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1924 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001925 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001928
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001929 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1930 AppWidgetProviderInfo appWidgetInfo) {
1931 if (appWidgetInfo.configure != null) {
1932 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001933
Bjorn Bringert7984c942009-12-09 15:38:25 +00001934 // Launch over to configure widget, if needed
1935 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001936 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001937 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001938 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001940 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001942 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001943 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001944 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946 }
Romain Guycbb89e42009-06-08 15:52:54 -07001947
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001948 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001949 // Close any folders that may be open.
1950 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001951 mWorkspace.moveToCustomContentScreen(animate);
1952 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001953 /**
1954 * Process a shortcut drop.
1955 *
1956 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001958 * @param cell The cell it should be added to, optional
1959 * @param position The location on the screen where it was dropped, optional
1960 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001963 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001967
1968 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 mPendingAddInfo.cellX = cell[0];
1970 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001971 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001972
1973 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1974 createShortcutIntent.setComponent(componentName);
1975 processShortcut(createShortcutIntent);
1976 }
1977
Adam Cohenfbba09b2011-07-18 21:43:05 -07001978 /**
1979 * Process a widget drop.
1980 *
1981 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001983 * @param cell The cell it should be added to, optional
1984 * @param position The location on the screen where it was dropped, optional
1985 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001987 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001989 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001992 mPendingAddInfo.minSpanX = info.minSpanX;
1993 mPendingAddInfo.minSpanY = info.minSpanY;
1994
Adam Cohenfbba09b2011-07-18 21:43:05 -07001995 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 mPendingAddInfo.cellX = cell[0];
1997 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001998 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001999 if (span != null) {
2000 mPendingAddInfo.spanX = span[0];
2001 mPendingAddInfo.spanY = span[1];
2002 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002003
Adam Cohened66b2b2012-01-23 17:28:51 -08002004 AppWidgetHostView hostView = info.boundWidget;
2005 int appWidgetId;
2006 if (hostView != null) {
2007 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002008 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002009 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002010 // In this case, we either need to start an activity to get permission to bind
2011 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002012 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002013 Bundle options = info.bindOptions;
2014
2015 boolean success = false;
2016 if (options != null) {
2017 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2018 info.componentName, options);
2019 } else {
2020 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2021 info.componentName);
2022 }
2023 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002024 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002025 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002026 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002027 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2028 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2029 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002030 // TODO: we need to make sure that this accounts for the options bundle.
2031 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002032 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2033 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002034 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002035 }
2036
Joe Onoratodeb98af2010-02-19 14:59:39 -08002037 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002038 // Handle case where user selected "Applications"
2039 String applicationName = getResources().getString(R.string.group_applications);
2040 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002041
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002042 if (applicationName != null && applicationName.equals(shortcutName)) {
2043 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2044 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2047 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002048 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002049 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002050 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002051 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
2054
Winson Chung24ab2f12010-09-16 14:10:47 -07002055 void processWallpaper(Intent intent) {
2056 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2057 }
2058
Adam Cohendcd297f2013-06-18 13:13:40 -07002059 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002060 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002061 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 folderInfo.title = getText(R.string.folder_name);
2063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002066 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002067 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
2069 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002070 FolderIcon newFolder =
2071 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002072 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002073 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002074 // Force measure the new folder icon
2075 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2076 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002077 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
Romain Guycbb89e42009-06-08 15:52:54 -07002079
Joe Onorato9c1289c2009-08-17 11:03:03 -04002080 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002081 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 }
2083
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002084 protected void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002087 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002088 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002091 protected ComponentName getWallpaperPickerComponent() {
2092 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2093 }
2094
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002095 /**
2096 * Registers various content observers. The current implementation registers
2097 * only a favorites observer to keep track of the favorites applications.
2098 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002099 private void registerContentObservers() {
2100 ContentResolver resolver = getContentResolver();
2101 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2102 true, mWidgetObserver);
2103 }
2104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 @Override
2106 public boolean dispatchKeyEvent(KeyEvent event) {
2107 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2108 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002110 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002111 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002112 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002113 dumpState();
2114 return true;
2115 }
2116 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002117 }
2118 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2119 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002120 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 return true;
2122 }
2123 }
2124
2125 return super.dispatchKeyEvent(event);
2126 }
2127
Joe Onorato88ec0992009-11-19 13:16:06 -08002128 @Override
2129 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002130 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002131 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002132 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002133 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002134 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002135 Folder openFolder = mWorkspace.getOpenFolder();
2136 if (openFolder.isEditingName()) {
2137 openFolder.dismissEditingName();
2138 } else {
2139 closeFolder();
2140 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002141 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002142 mWorkspace.exitWidgetResizeMode();
2143
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002144 // Back button is a no-op here, but give at least some feedback for the button press
2145 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002146 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002147 }
2148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002150 * Re-listen when widgets are reset.
2151 */
2152 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002153 if (mAppWidgetHost != null) {
2154 mAppWidgetHost.startListening();
2155 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002156 }
2157
2158 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 * Launches the intent referred by the clicked shortcut.
2160 *
2161 * @param v The view representing the clicked shortcut.
2162 */
2163 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002164 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2165 // view has detached (it's possible for this to happen if the view is removed mid touch).
2166 if (v.getWindowToken() == null) {
2167 return;
2168 }
2169
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002170 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002171 return;
2172 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002173
Adam Cohen1697b792013-09-17 19:08:21 -07002174 if (v instanceof Workspace) {
2175 if (mWorkspace.isInOverviewMode()) {
2176 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002177 }
2178 return;
2179 }
2180
2181 if (v instanceof CellLayout) {
2182 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002183 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002184 }
2185 }
2186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002188 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002190 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2191 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002192
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002193 // Check for special shortcuts
2194 if (intent.getComponent() != null) {
2195 final String shortcutClass = intent.getComponent().getClassName();
2196
2197 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chungc58497e2013-09-03 17:48:37 -07002198 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002199 return;
2200 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2201 MemoryDumpActivity.startDump(this);
2202 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002203 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2204 toggleShowWeightWatcher();
2205 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002206 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002207 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002208
2209 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002210 int[] pos = new int[2];
2211 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002212 intent.setSourceBounds(new Rect(pos[0], pos[1],
2213 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002214
2215 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002216
Daniel Sandlerff02d492013-08-05 02:12:05 -04002217 mStats.recordLaunch(intent, shortcut);
2218
Michael Jurkaddd62e92011-02-16 17:49:14 -08002219 if (success && v instanceof BubbleTextView) {
2220 mWaitingForResume = (BubbleTextView) v;
2221 mWaitingForResume.setStayPressed(true);
2222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002224 if (v instanceof FolderIcon) {
2225 FolderIcon fi = (FolderIcon) v;
2226 handleFolderClick(fi);
2227 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002228 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002229 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002230 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002231 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002232 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 }
2235 }
2236
Michael Jurka0e260592010-06-30 17:07:39 -07002237 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002238 return false;
2239 }
2240
Michael Jurkaaf442092010-06-10 17:01:57 -07002241 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002242 * Event handler for the search button
2243 *
2244 * @param v The view that was clicked.
2245 */
2246 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002247 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2248
Amith Yamasania135ba82011-08-09 17:42:01 -07002249 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002250 }
2251
2252 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002253 * Event handler for the voice button
2254 *
2255 * @param v The view that was clicked.
2256 */
2257 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002258 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002259
Bjorn Bringertc459e522013-06-07 19:36:01 +01002260 startVoice();
2261 }
2262
2263 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002264 try {
2265 final SearchManager searchManager =
2266 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2267 ComponentName activityName = searchManager.getGlobalSearchActivity();
2268 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002269 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002270 if (activityName != null) {
2271 intent.setPackage(activityName.getPackageName());
2272 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002273 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002274 } catch (ActivityNotFoundException e) {
2275 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002276 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002277 startActivitySafely(null, intent, "onClickVoiceButton");
2278 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002279 }
2280
2281 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002282 * Event handler for the "grid" button that appears on the home screen, which
2283 * enters all apps mode.
2284 *
2285 * @param v The view that was clicked.
2286 */
2287 public void onClickAllAppsButton(View v) {
Winson Chungc58497e2013-09-03 17:48:37 -07002288 showAllApps(true, AppsCustomizePagedView.ContentType.Applications);
Michael Jurka5130e402011-10-13 04:55:35 -07002289 }
2290
2291 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002292 // Provide the same haptic feedback that the system offers for virtual keys.
2293 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002294 }
2295
Adam Cohen61f560d2013-09-30 15:58:20 -07002296 public void performHapticFeedbackOnTouchDown(View v) {
2297 // Provide the same haptic feedback that the system offers for virtual keys.
2298 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2299 }
2300
2301 public View.OnTouchListener getHapticFeedbackTouchListener() {
2302 if (mHapticFeedbackTouchListener == null) {
2303 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2304 @Override
2305 public boolean onTouch(View v, MotionEvent event) {
2306 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2307 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2308 }
2309 return false;
2310 }
2311 };
2312 }
2313 return mHapticFeedbackTouchListener;
2314 }
2315
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002316 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002317 if (!DISABLE_MARKET_BUTTON) {
2318 if (mAppMarketIntent != null) {
2319 startActivitySafely(v, mAppMarketIntent, "app market");
2320 } else {
2321 Log.e(TAG, "Invalid app market intent.");
2322 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002323 }
2324 }
2325
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002326 /**
2327 * Called when the user stops interacting with the launcher.
2328 * This implies that the user is now on the homescreen and is not doing housekeeping.
2329 */
2330 protected void onInteractionEnd() {}
2331
2332 /**
2333 * Called when the user starts interacting with the launcher.
2334 * The possible interactions are:
2335 * - open all apps
2336 * - reorder an app shortcut, or a widget
2337 * - open the overview mode.
2338 * This is a good time to stop doing things that only make sense
2339 * when the user is on the homescreen and not doing housekeeping.
2340 */
2341 protected void onInteractionBegin() {}
2342
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002343 void startApplicationDetailsActivity(ComponentName componentName) {
2344 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002345 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2346 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002347 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002348 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002349 }
2350
Michael Jurka1e2f4652013-07-08 18:03:46 -07002351 // returns true if the activity was started
2352 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002353 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002354 // System applications cannot be installed. For now, show a toast explaining that.
2355 // We may give them the option of disabling apps this way.
2356 int messageId = R.string.uninstall_system_app_text;
2357 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002358 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002359 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002360 String packageName = componentName.getPackageName();
2361 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002362 Intent intent = new Intent(
2363 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002364 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2365 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002366 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002367 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002368 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002369 }
2370
Michael Jurka86a720e2012-05-09 11:23:23 -07002371 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002375 // Only launch using the new animation if the shortcut has not opted out (this is a
2376 // private contract between launcher and may be ignored in the future).
2377 boolean useLaunchAnimation = (v != null) &&
2378 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2379 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002380 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2381 v.getMeasuredWidth(), v.getMeasuredHeight());
2382
2383 startActivity(intent, opts.toBundle());
2384 } else {
2385 startActivity(intent);
2386 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002387 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 } catch (SecurityException e) {
2389 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002390 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002392 "or use the exported attribute for this activity. "
2393 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002395 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002397
Michael Jurka86a720e2012-05-09 11:23:23 -07002398 boolean startActivitySafely(View v, Intent intent, Object tag) {
2399 boolean success = false;
2400 try {
2401 success = startActivity(v, intent, tag);
2402 } catch (ActivityNotFoundException e) {
2403 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2404 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2405 }
2406 return success;
2407 }
2408
Adam Cohena9cf38f2011-05-02 15:36:58 -07002409 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002410 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002411 Folder openFolder = mWorkspace.getFolderForTag(info);
2412
2413 // If the folder info reports that the associated folder is open, then verify that
2414 // it is actually opened. There have been a few instances where this gets out of sync.
2415 if (info.opened && openFolder == null) {
2416 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002417 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002418 info.opened = false;
2419 }
2420
Adam Cohenfb91f302012-06-11 15:45:18 -07002421 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 // Close any open folder
2423 closeFolder();
2424 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002425 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 } else {
2427 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 int folderScreen;
2429 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002430 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 // .. and close it
2432 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002433 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 // Close any folder open on the current screen
2435 closeFolder();
2436 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002437 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
2439 }
2440 }
2441 }
2442
Adam Cohen268c4752012-06-06 17:47:33 -07002443 /**
2444 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2445 * in the DragLayer in the exact absolute location of the original FolderIcon.
2446 */
2447 private void copyFolderIconToImage(FolderIcon fi) {
2448 final int width = fi.getMeasuredWidth();
2449 final int height = fi.getMeasuredHeight();
2450
2451 // Lazy load ImageView, Bitmap and Canvas
2452 if (mFolderIconImageView == null) {
2453 mFolderIconImageView = new ImageView(this);
2454 }
2455 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2456 mFolderIconBitmap.getHeight() != height) {
2457 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2458 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2459 }
2460
2461 DragLayer.LayoutParams lp;
2462 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2463 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2464 } else {
2465 lp = new DragLayer.LayoutParams(width, height);
2466 }
2467
Adam Cohen307fe232012-08-16 17:55:58 -07002468 // The layout from which the folder is being opened may be scaled, adjust the starting
2469 // view size by this scale factor.
2470 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002471 lp.customPosition = true;
2472 lp.x = mRectForFolderAnimation.left;
2473 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002474 lp.width = (int) (scale * width);
2475 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002476
2477 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2478 fi.draw(mFolderIconCanvas);
2479 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002480 if (fi.getFolder() != null) {
2481 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2482 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002483 }
Adam Cohen268c4752012-06-06 17:47:33 -07002484 // Just in case this image view is still in the drag layer from a previous animation,
2485 // we remove it and re-add it.
2486 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2487 mDragLayer.removeView(mFolderIconImageView);
2488 }
2489 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002490 if (fi.getFolder() != null) {
2491 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002492 }
Adam Cohen268c4752012-06-06 17:47:33 -07002493 }
2494
Adam Cohen2801caf2011-05-13 20:57:39 -07002495 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002496 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002497 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2498 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2499 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2500
Adam Cohenc51934b2011-07-26 21:07:43 -07002501 FolderInfo info = (FolderInfo) fi.getTag();
2502 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2503 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002504 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2505 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002506 }
2507
Adam Cohen268c4752012-06-06 17:47:33 -07002508 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2509 copyFolderIconToImage(fi);
2510 fi.setVisibility(View.INVISIBLE);
2511
Michael Jurka2ecf9952012-06-18 12:52:28 -07002512 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002513 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002514 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2515 oa.start();
2516 }
2517
Adam Cohen268c4752012-06-06 17:47:33 -07002518 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002519 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002520 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2521 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2522 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2523
Adam Cohen268c4752012-06-06 17:47:33 -07002524 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002525
Adam Cohen268c4752012-06-06 17:47:33 -07002526 // We remove and re-draw the FolderIcon in-case it has changed
2527 mDragLayer.removeView(mFolderIconImageView);
2528 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002529 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002530 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002531 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002532 oa.addListener(new AnimatorListenerAdapter() {
2533 @Override
2534 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002535 if (cl != null) {
2536 cl.clearFolderLeaveBehind();
2537 // Remove the ImageView copy of the FolderIcon and make the original visible.
2538 mDragLayer.removeView(mFolderIconImageView);
2539 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002540 }
2541 }
2542 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002543 oa.start();
2544 }
2545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002547 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 * is animated relative to the specified View. If the View is null, no animation
2549 * is played.
2550 *
2551 * @param folderInfo The FolderInfo describing the folder to open.
2552 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002553 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002554 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002555 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556
Adam Cohena9cf38f2011-05-02 15:36:58 -07002557 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558
Adam Cohen4554ee12011-08-03 16:13:21 -07002559 // Just verify that the folder hasn't already been added to the DragLayer.
2560 // There was a one-off crash where the folder had a parent already.
2561 if (folder.getParent() == null) {
2562 mDragLayer.addView(folder);
2563 mDragController.addDropTarget((DropTarget) folder);
2564 } else {
2565 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2566 folder.getParent() + ").");
2567 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002568 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002569 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002570
2571 // Notify the accessibility manager that this folder "window" has appeared and occluded
2572 // the workspace items
2573 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2574 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002575 }
2576
2577 public void closeFolder() {
2578 Folder folder = mWorkspace.getOpenFolder();
2579 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002580 if (folder.isEditingName()) {
2581 folder.dismissEditingName();
2582 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002583 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002584
2585 // Dismiss the folder cling
2586 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002587 }
2588 }
2589
2590 void closeFolder(Folder folder) {
2591 folder.getInfo().opened = false;
2592
2593 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2594 if (parent != null) {
2595 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2596 shrinkAndFadeInFolderIcon(fi);
2597 }
2598 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002599
2600 // Notify the accessibility manager that this folder "window" has disappeard and no
2601 // longer occludeds the workspace items
2602 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 }
2604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002606 if (!isDraggingEnabled()) return false;
2607 if (isWorkspaceLocked()) return false;
2608 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609
Adam Cohen1697b792013-09-17 19:08:21 -07002610 if (v instanceof Workspace) {
2611 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002612 if (mWorkspace.enterOverviewMode()) {
2613 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2614 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2615 return true;
2616 } else {
2617 return false;
2618 }
Adam Cohen1697b792013-09-17 19:08:21 -07002619 }
Adam Cohen1697b792013-09-17 19:08:21 -07002620 }
2621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002623 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 }
2625
Michael Jurka0280c3b2010-09-17 15:00:07 -07002626 resetAddInfo();
2627 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002629 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 return true;
2631 }
2632
Winson Chung3d503fb2011-07-13 17:25:49 -07002633 // The hotseat touch handling does not go through Workspace, and we always allow long press
2634 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002635 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002636 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2637 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002638 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002639 // User long pressed on empty space
2640 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2641 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2642 // Disabling reordering until we sort out some issues.
2643 if (mWorkspace.isInOverviewMode()) {
2644 mWorkspace.startReordering(v);
2645 } else {
2646 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002649 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002651 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652 }
2653 }
2654 }
2655 return true;
2656 }
2657
Winson Chung3d503fb2011-07-13 17:25:49 -07002658 boolean isHotseatLayout(View layout) {
2659 return mHotseat != null && layout != null &&
2660 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2661 }
2662 Hotseat getHotseat() {
2663 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002664 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002665 View getOverviewPanel() {
2666 return mOverviewPanel;
2667 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002668 SearchDropTargetBar getSearchBar() {
2669 return mSearchDropTargetBar;
2670 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002671
Winson Chung3d503fb2011-07-13 17:25:49 -07002672 /**
2673 * Returns the CellLayout of the specified container at the specified screen.
2674 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002675 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002676 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2677 if (mHotseat != null) {
2678 return mHotseat.getLayout();
2679 } else {
2680 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002681 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002682 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002683 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002684 }
2685 }
2686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 Workspace getWorkspace() {
2688 return mWorkspace;
2689 }
2690
Daniel Sandler843e8602010-06-07 14:59:01 -04002691 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002692 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002693 }
2694
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002695 /**
2696 * Helper method for the cameraZoomIn/cameraZoomOut animations
2697 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002698 * @param scaleFactor The scale factor used for the zoom
2699 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002700 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002702 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002703 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002704
Craig Mautner360310b2012-10-26 15:13:08 -07002705 private void setWorkspaceBackground(boolean workspace) {
2706 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002707 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002708 }
2709
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002710 void updateWallpaperVisibility(boolean visible) {
2711 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2712 int curflags = getWindow().getAttributes().flags
2713 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2714 if (wpflags != curflags) {
2715 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2716 }
Craig Mautner360310b2012-10-26 15:13:08 -07002717 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002718 }
2719
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002720 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2721 if (v instanceof LauncherTransitionable) {
2722 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2723 }
2724 }
2725
Michael Jurkabed61d22012-02-14 22:51:29 -08002726 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2727 if (v instanceof LauncherTransitionable) {
2728 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2729 }
Winson Chung70442722012-02-10 15:43:22 -08002730
2731 // Update the workspace transition step as well
2732 dispatchOnLauncherTransitionStep(v, 0f);
2733 }
2734
2735 private void dispatchOnLauncherTransitionStep(View v, float t) {
2736 if (v instanceof LauncherTransitionable) {
2737 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2738 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002739 }
2740
2741 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2742 if (v instanceof LauncherTransitionable) {
2743 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2744 }
Winson Chung70442722012-02-10 15:43:22 -08002745
2746 // Update the workspace transition step as well
2747 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002748 }
2749
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002751 * Things to test when changing the following seven functions.
2752 * - Home from workspace
2753 * - from center screen
2754 * - from other screens
2755 * - Home from all apps
2756 * - from center screen
2757 * - from other screens
2758 * - Back from all apps
2759 * - from center screen
2760 * - from other screens
2761 * - Launch app from workspace and quit
2762 * - with back
2763 * - with home
2764 * - Launch app from all apps and quit
2765 * - with back
2766 * - with home
2767 * - Go to a screen that's not the default, then all
2768 * apps, and launch and app, and go back
2769 * - with back
2770 * -with home
2771 * - On workspace, long press power and go back
2772 * - with back
2773 * - with home
2774 * - On all apps, long press power and go back
2775 * - with back
2776 * - with home
2777 * - On workspace, power off
2778 * - On all apps, power off
2779 * - Launch an app and turn off the screen while in that app
2780 * - Go back with home key
2781 * - Go back with back key TODO: make this not go to workspace
2782 * - From all apps
2783 * - From workspace
2784 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2785 * - From all apps
2786 * - From the center workspace
2787 * - From another workspace
2788 */
2789
2790 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002791 * Zoom the camera out from the workspace to reveal 'toView'.
2792 * Assumes that the view to show is anchored at either the very top or very bottom
2793 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002794 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002795 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002796 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2797 showAppsCustomizeHelper(animated, springLoaded, contentType);
2798 }
2799 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2800 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002801 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002802 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002803 mStateAnimation.cancel();
2804 mStateAnimation = null;
2805 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002806 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002807
Winson Chungf0ea4d32011-06-06 14:27:16 -07002808 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2809 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2810 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002811 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002812 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002813 final int startDelay =
2814 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002815
Michael Jurkab3e22d92011-10-31 15:58:33 -07002816 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002817
Michael Jurkad74c9842011-07-10 12:44:21 -07002818 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002819 Animator workspaceAnim =
2820 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002821 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2822 // Set the content type for the all apps space
2823 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2824 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002825
2826 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002827 toView.setScaleX(scale);
2828 toView.setScaleY(scale);
2829 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2830 scaleAnim.
2831 scaleX(1f).scaleY(1f).
2832 setDuration(duration).
2833 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002834
Winson Chungf0ea4d32011-06-06 14:27:16 -07002835 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002836 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002837 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002838 .ofFloat(toView, "alpha", 0f, 1f)
2839 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002840 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002841 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2842 @Override
2843 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002844 if (animation == null) {
2845 throw new RuntimeException("animation is null");
2846 }
Winson Chung70442722012-02-10 15:43:22 -08002847 float t = (Float) animation.getAnimatedValue();
2848 dispatchOnLauncherTransitionStep(fromView, t);
2849 dispatchOnLauncherTransitionStep(toView, t);
2850 }
2851 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002852
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002853 // toView should appear right at the end of the workspace shrink
2854 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002855 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002856 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002857 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002858
2859 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002860 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002861 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002862 // Prepare the position
2863 toView.setTranslationX(0.0f);
2864 toView.setTranslationY(0.0f);
2865 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002866 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002868 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002869 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002870 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2871 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002872
Winson Chungc7d2b602012-05-16 17:02:20 -07002873 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002874 if (mSearchDropTargetBar != null) {
2875 mSearchDropTargetBar.hideSearchBar(false);
2876 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002877 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002878 });
2879
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002880 if (workspaceAnim != null) {
2881 mStateAnimation.play(workspaceAnim);
2882 }
Michael Jurka1899a362011-11-03 13:50:45 -07002883
2884 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002885
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002886 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2887 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002888
2889 // If any of the objects being animated haven't been measured/laid out
2890 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002891 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002892 (mWorkspace.getMeasuredWidth() == 0) ||
2893 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002894 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002895 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002897 final AnimatorSet stateAnimation = mStateAnimation;
2898 final Runnable startAnimRunnable = new Runnable() {
2899 public void run() {
2900 // Check that mStateAnimation hasn't changed while
2901 // we waited for a layout/draw pass
2902 if (mStateAnimation != stateAnimation)
2903 return;
2904 setPivotsForZoom(toView, scale);
2905 dispatchOnLauncherTransitionStart(fromView, animated, false);
2906 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002907 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002908 }
2909 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002910 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002911 final ViewTreeObserver observer = toView.getViewTreeObserver();
2912 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2913 public void onGlobalLayout() {
2914 startAnimRunnable.run();
2915 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2916 }
2917 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002918 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002919 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002920 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002921 } else {
2922 toView.setTranslationX(0.0f);
2923 toView.setTranslationY(0.0f);
2924 toView.setScaleX(1.0f);
2925 toView.setScaleY(1.0f);
2926 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002927 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002928
Daniel Sandlere4f98912013-06-25 15:13:26 -04002929 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002930 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002931 if (mSearchDropTargetBar != null) {
2932 mSearchDropTargetBar.hideSearchBar(false);
2933 }
Michael Jurkaabded662011-03-04 12:06:57 -08002934 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002935 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002936 dispatchOnLauncherTransitionStart(fromView, animated, false);
2937 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002938 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002939 dispatchOnLauncherTransitionStart(toView, animated, false);
2940 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002941 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002942 }
2943
2944 /**
2945 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002946 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002947 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002948 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002949 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2950 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002951
Michael Jurkab3e22d92011-10-31 15:58:33 -07002952 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002953 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002954 mStateAnimation.cancel();
2955 mStateAnimation = null;
2956 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002957 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002958
Winson Chungf0ea4d32011-06-06 14:27:16 -07002959 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002960 final int fadeOutDuration =
2961 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002962 final float scaleFactor = (float)
2963 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2964 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002965 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002966 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002967 if (toState == State.WORKSPACE) {
2968 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2969 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002970 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002971 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2972 workspaceAnim = mWorkspace.getChangeStateAnimation(
2973 Workspace.State.SPRING_LOADED, animated);
2974 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002975
Michael Jurkab3e22d92011-10-31 15:58:33 -07002976 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002977 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002978 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002979 final LauncherViewPropertyAnimator scaleAnim =
2980 new LauncherViewPropertyAnimator(fromView);
2981 scaleAnim.
2982 scaleX(scaleFactor).scaleY(scaleFactor).
2983 setDuration(duration).
2984 setInterpolator(new Workspace.ZoomInInterpolator());
2985
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002986 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002987 .ofFloat(fromView, "alpha", 1f, 0f)
2988 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002989 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002990 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2991 @Override
2992 public void onAnimationUpdate(ValueAnimator animation) {
2993 float t = 1f - (Float) animation.getAnimatedValue();
2994 dispatchOnLauncherTransitionStep(fromView, t);
2995 dispatchOnLauncherTransitionStep(toView, t);
2996 }
2997 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002998
Michael Jurka2ecf9952012-06-18 12:52:28 -07002999 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003000
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003001 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3002 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07003003 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003004
3005 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003006 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003007 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003008 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003009 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3010 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003011 if (onCompleteRunnable != null) {
3012 onCompleteRunnable.run();
3013 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003014 mAppsCustomizeContent.updateCurrentPageScroll();
3015 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003016 }
3017 });
3018
Winson Chungf0ea4d32011-06-06 14:27:16 -07003019 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003020 if (workspaceAnim != null) {
3021 mStateAnimation.play(workspaceAnim);
3022 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003023 dispatchOnLauncherTransitionStart(fromView, animated, true);
3024 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003025 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003026 } else {
3027 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003028 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003029 dispatchOnLauncherTransitionStart(fromView, animated, true);
3030 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003031 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003032 dispatchOnLauncherTransitionStart(toView, animated, true);
3033 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003034 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003035 }
3036
Michael Jurkae326f182011-11-21 14:05:46 -08003037 @Override
3038 public void onTrimMemory(int level) {
3039 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003040 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003041 mAppsCustomizeTabHost.onTrimMemory();
3042 }
3043 }
3044
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003045 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003046 showWorkspace(animated, null);
3047 }
3048
3049 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003050 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003051 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003052 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003053 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003054
Winson Chungc7d2b602012-05-16 17:02:20 -07003055 // Show the search bar (only animate if we were showing the drop target bar in spring
3056 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003057 if (mSearchDropTargetBar != null) {
3058 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3059 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003060
Michael Jurkab3e22d92011-10-31 15:58:33 -07003061 // Set focus to the AppsCustomize button
3062 if (mAllAppsButton != null) {
3063 mAllAppsButton.requestFocus();
3064 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003065 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003066
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003067 // Change the state *after* we've called all the transition code
3068 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003069
Winson Chung5fb63472011-02-02 17:03:37 -08003070 // Resume the auto-advance of widgets
3071 mUserPresent = true;
3072 updateRunning();
3073
alanv1d4fde62012-10-17 13:15:47 -07003074 // Send an accessibility event to announce the context change
3075 getWindow().getDecorView()
3076 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003077
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003078 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003079 }
3080
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003081 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003082 }
3083
Winson Chungc58497e2013-09-03 17:48:37 -07003084 void showAllApps(boolean animated,
3085 AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003086 if (mState != State.WORKSPACE) return;
3087
Winson Chungc58497e2013-09-03 17:48:37 -07003088 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003089 mAppsCustomizeTabHost.requestFocus();
3090
Michael Jurkab3e22d92011-10-31 15:58:33 -07003091 // Change the state *after* we've called all the transition code
3092 mState = State.APPS_CUSTOMIZE;
3093
3094 // Pause the auto-advance of widgets until we are out of AllApps
3095 mUserPresent = false;
3096 updateRunning();
3097 closeFolder();
3098
3099 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003100 getWindow().getDecorView()
3101 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003102 }
3103
Adam Cohen7777d962011-08-18 18:58:38 -07003104 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003105 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003106 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003107 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003108 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003109 }
Adam Cohen7777d962011-08-18 18:58:38 -07003110
Adam Cohened66b2b2012-01-23 17:28:51 -08003111 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3112 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003113 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3114
Winson Chunge7a03942011-08-05 15:05:12 -07003115 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003116 @Override
3117 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003118 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003119 // Before we show workspace, hide all apps again because
3120 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3121 // clean up our state transition functions
3122 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003123 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003124 } else {
3125 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003126 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003127 }
3128 }, (extendedDelay ?
3129 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3130 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3131 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003132
Michael Jurkad3ef3062010-11-23 16:23:58 -08003133 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003134 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003135 final boolean animated = true;
3136 final boolean springLoaded = true;
3137 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003138 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003139 }
3140 // Otherwise, we are not in spring loaded mode, so don't do anything.
3141 }
3142
Michael Jurkab3e22d92011-10-31 15:58:33 -07003143 void lockAllApps() {
3144 // TODO
3145 }
3146
3147 void unlockAllApps() {
3148 // TODO
3149 }
3150
Winson Chungf0ea4d32011-06-06 14:27:16 -07003151 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003153 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003155 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003156 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003157 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003158 int duration = 0;
3159 if (mSearchDropTargetBar != null) {
3160 duration = mSearchDropTargetBar.getTransitionInDuration();
3161 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003162 mHotseat.animate().alpha(1f).setDuration(duration);
3163 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 }
3167 }
3168 }
3169
3170 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003172 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003174 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003175 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003176 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003177 int duration = 0;
3178 if (mSearchDropTargetBar != null) {
3179 duration = mSearchDropTargetBar.getTransitionOutDuration();
3180 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003181 mHotseat.animate().alpha(0f).setDuration(duration);
3182 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003183 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003185 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003186 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003187 }
3188
Patrick Dubroy5f445422011-02-18 14:35:21 -08003189 /**
3190 * Add an item from all apps or customize onto the given workspace screen.
3191 * If layout is null, add to the current screen.
3192 */
3193 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003194 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003195 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003196 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003197 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003198
Winson Chungdff8ebb2011-09-08 17:25:31 -07003199 /** Maps the current orientation to an index for referencing orientation correct global icons */
3200 private int getCurrentOrientationIndexForGlobalIcons() {
3201 // default - 0, landscape - 1
3202 switch (getResources().getConfiguration().orientation) {
3203 case Configuration.ORIENTATION_LANDSCAPE:
3204 return 1;
3205 default:
3206 return 0;
3207 }
3208 }
3209
Michael Jurkaae65ee32012-04-30 11:45:54 -07003210 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003211 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003212 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003213 // Look for the toolbar icon specified in the activity meta-data
3214 Bundle metaData = packageManager.getActivityInfo(
3215 activityName, PackageManager.GET_META_DATA).metaData;
3216 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003217 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003218 if (iconResId != 0) {
3219 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003220 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003221 }
3222 }
3223 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003224 // This can happen if the activity defines an invalid drawable
3225 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3226 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003227 } catch (Resources.NotFoundException nfe) {
3228 // This can happen if the activity defines an invalid drawable
3229 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3230 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003231 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232 return null;
3233 }
3234
3235 // if successful in getting icon, return it; otherwise, set button to use default drawable
3236 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003237 int buttonId, ComponentName activityName, int fallbackDrawableId,
3238 String toolbarResourceName) {
3239 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003240 Resources r = getResources();
3241 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3242 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003243
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003244 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003245 // If we were unable to find the icon via the meta-data, use a generic one
3246 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003247 toolbarIcon = r.getDrawable(fallbackDrawableId);
3248 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003249 if (button != null) {
3250 button.setCompoundDrawables(toolbarIcon, null, null, null);
3251 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003252 return null;
3253 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003254 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003255 if (button != null) {
3256 button.setCompoundDrawables(toolbarIcon, null, null, null);
3257 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003258 return toolbarIcon.getConstantState();
3259 }
3260 }
3261
3262 // if successful in getting icon, return it; otherwise, set button to use default drawable
3263 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003264 int buttonId, ComponentName activityName, int fallbackDrawableId,
3265 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003266 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003267 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003268
Michael Jurka19e0fc52011-07-22 18:00:21 -07003269 if (button != null) {
3270 // If we were unable to find the icon via the meta-data, use a
3271 // generic one
3272 if (toolbarIcon == null) {
3273 button.setImageResource(fallbackDrawableId);
3274 } else {
3275 button.setImageDrawable(toolbarIcon);
3276 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003277 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003278
3279 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3280
Michael Jurka0423dcf2010-10-05 14:56:18 -07003281 }
3282
Winson Chung1b884092012-06-08 17:13:02 -07003283 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003284 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003285 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003286 }
3287
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003288 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003289 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003290 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003291 }
3292
Winson Chungbb185bd2011-11-21 12:31:42 -08003293 private void invalidatePressedFocusedStates(View container, View button) {
3294 if (container instanceof HolographicLinearLayout) {
3295 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3296 layout.invalidatePressedFocusedStates();
3297 } else if (button instanceof HolographicImageView) {
3298 HolographicImageView view = (HolographicImageView) button;
3299 view.invalidatePressedFocusedStates();
3300 }
3301 }
3302
Cristina Stancu476493b2013-08-07 17:20:14 +01003303 public View getQsbBar() {
3304 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003305 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3306 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003307 }
3308 return mQsbBar;
3309 }
3310
3311 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003312 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003313 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003315 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003316
Winson Chung1cad91e2011-05-25 17:41:01 -07003317 final SearchManager searchManager =
3318 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3319 ComponentName activityName = searchManager.getGlobalSearchActivity();
3320 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003321 int coi = getCurrentOrientationIndexForGlobalIcons();
3322 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003323 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3324 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3325 if (sGlobalSearchIcon[coi] == null) {
3326 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3327 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3328 TOOLBAR_ICON_METADATA_NAME);
3329 }
3330
Winson Chungc51db6a2011-10-05 11:44:49 -07003331 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3332 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003333 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003334 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003335 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003336 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003337 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3338 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003339 if (searchButton != null) searchButton.setVisibility(View.GONE);
3340 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003341 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003342 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003343 }
3344 }
3345
Cristina Stancu476493b2013-08-07 17:20:14 +01003346 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003347 final View searchButtonContainer = findViewById(R.id.search_button_container);
3348 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003349 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003350 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003351 }
3352
Cristina Stancu476493b2013-08-07 17:20:14 +01003353 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003354 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003355 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003356
Winson Chungc51db6a2011-10-05 11:44:49 -07003357 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003358 final SearchManager searchManager =
3359 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3360 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3361
3362 ComponentName activityName = null;
3363 if (globalSearchActivity != null) {
3364 // Check if the global search activity handles voice search
3365 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3366 intent.setPackage(globalSearchActivity.getPackageName());
3367 activityName = intent.resolveActivity(getPackageManager());
3368 }
3369
3370 if (activityName == null) {
3371 // Fallback: check if an activity other than the global search activity
3372 // resolves this
3373 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3374 activityName = intent.resolveActivity(getPackageManager());
3375 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003376 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003377 int coi = getCurrentOrientationIndexForGlobalIcons();
3378 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003379 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3380 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3381 if (sVoiceSearchIcon[coi] == null) {
3382 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3383 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3384 TOOLBAR_ICON_METADATA_NAME);
3385 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003386 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003387 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003388 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003389 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003390 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003391 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003392 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003393 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003394 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003395 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003396 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003397 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003398
Cristina Stancu476493b2013-08-07 17:20:14 +01003399 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003400 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3401 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003402 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003403 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003404 }
3405
Winson Chung5841efa2013-09-30 18:06:44 -07003406 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003407 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3408 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003409 boolean visible = !forceDisableVoiceButtonProxy &&
3410 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003411 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003412 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003413 }
3414 }
Winson Chung5841efa2013-09-30 18:06:44 -07003415
3416 /**
3417 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3418 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3419 */
3420 public void disableVoiceButtonProxy(boolean disabled) {
3421 updateVoiceButtonProxyVisible(disabled);
3422 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003423 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003424 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003425 */
3426 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003427 if (!DISABLE_MARKET_BUTTON) {
3428 final View marketButton = findViewById(R.id.market_button);
3429 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3430 // Find the app market activity by resolving an intent.
3431 // (If multiple app markets are installed, it will return the ResolverActivity.)
3432 ComponentName activityName = intent.resolveActivity(getPackageManager());
3433 if (activityName != null) {
3434 int coi = getCurrentOrientationIndexForGlobalIcons();
3435 mAppMarketIntent = intent;
3436 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3437 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3438 TOOLBAR_ICON_METADATA_NAME);
3439 marketButton.setVisibility(View.VISIBLE);
3440 } else {
3441 // We should hide and disable the view so that we don't try and restore the visibility
3442 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3443 marketButton.setVisibility(View.GONE);
3444 marketButton.setEnabled(false);
3445 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003446 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003447 }
3448
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003449 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003450 if (!DISABLE_MARKET_BUTTON) {
3451 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3452 Resources r = getResources();
3453 Drawable marketIconDrawable = d.newDrawable(r);
3454 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3455 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3456 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003457
Winson Chungd64a6662013-09-30 11:06:59 -07003458 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3459 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003460 }
3461
Michael Jurkad7c28052012-04-27 15:43:36 -07003462 @Override
3463 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003464 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003465 final List<CharSequence> text = event.getText();
3466 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003467 // Populate event with a fake title based on the current state.
3468 if (mState == State.APPS_CUSTOMIZE) {
3469 text.add(getString(R.string.all_apps_button_label));
3470 } else {
3471 text.add(getString(R.string.all_apps_home_button_label));
3472 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003473 return result;
3474 }
3475
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003476 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003477 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003478 */
3479 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3480 @Override
3481 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003482 closeSystemDialogs();
3483 }
3484 }
3485
3486 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003487 * Receives notifications whenever the appwidgets are reset.
3488 */
3489 private class AppWidgetResetObserver extends ContentObserver {
3490 public AppWidgetResetObserver() {
3491 super(new Handler());
3492 }
3493
3494 @Override
3495 public void onChange(boolean selfChange) {
3496 onAppWidgetReset();
3497 }
3498 }
3499
3500 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003501 * If the activity is currently paused, signal that we need to run the passed Runnable
3502 * in onResume.
3503 *
3504 * This needs to be called from incoming places where resources might have been loaded
3505 * while we are paused. That is becaues the Configuration might be wrong
3506 * when we're not running, and if it comes back to what it was when we
3507 * were paused, we are not restarted.
3508 *
3509 * Implementation of the method from LauncherModel.Callbacks.
3510 *
3511 * @return true if we are currently paused. The caller might be able to
3512 * skip some work in that case since we will come back again.
3513 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003514 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003515 if (mPaused) {
3516 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003517 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003518 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003519 }
3520 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003521 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 return true;
3523 } else {
3524 return false;
3525 }
3526 }
3527
Michael Jurkac402cd92013-05-20 15:49:32 +02003528 private boolean waitUntilResume(Runnable run) {
3529 return waitUntilResume(run, false);
3530 }
3531
Michael Jurka1e2f4652013-07-08 18:03:46 -07003532 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003533 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 }
3535
Michael Jurka7607c2f2013-04-03 14:33:19 -07003536 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003537 * If the activity is currently paused, signal that we need to re-run the loader
3538 * in onResume.
3539 *
3540 * This needs to be called from incoming places where resources might have been loaded
3541 * while we are paused. That is becaues the Configuration might be wrong
3542 * when we're not running, and if it comes back to what it was when we
3543 * were paused, we are not restarted.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 *
3547 * @return true if we are currently paused. The caller might be able to
3548 * skip some work in that case since we will come back again.
3549 */
3550 public boolean setLoadOnResume() {
3551 if (mPaused) {
3552 Log.i(TAG, "setLoadOnResume");
3553 mOnResumeNeedsLoad = true;
3554 return true;
3555 } else {
3556 return false;
3557 }
3558 }
3559
3560 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003562 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003564 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003565 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003566 } else {
3567 return SCREEN_COUNT / 2;
3568 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003570
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 /**
3572 * Refreshes the shortcuts shown on the workspace.
3573 *
3574 * Implementation of the method from LauncherModel.Callbacks.
3575 */
3576 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003577 // If we're starting binding all over again, clear any bind calls we'd postponed in
3578 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3579 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003580 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003581
Winson Chungd64d1762013-08-20 14:37:16 -07003582 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003583 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003584 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003585
Michael Jurka05bf644e2011-11-30 20:28:53 -08003586 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003587 if (mHotseat != null) {
3588 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003589 }
3590 }
3591
Adam Cohendcd297f2013-06-18 13:13:40 -07003592 @Override
3593 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003594 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003595
Adam Cohen5084cba2013-09-03 12:01:16 -07003596 // If there are no screens, we need to have an empty screen
3597 if (orderedScreenIds.size() == 0) {
3598 mWorkspace.addExtraEmptyScreen();
3599 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003600
3601 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003602 // setCurrentPage() so ensure that all pages are added before calling this).
3603 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003604 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3605 mWorkspace.createCustomContentPage();
3606 }
Winson Chung64359a52013-07-08 17:17:08 -07003607 }
3608
3609 @Override
3610 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003611 int count = orderedScreenIds.size();
3612 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003613 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003614 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003615 }
3616
Adam Cohen39a06042013-07-19 14:30:12 -07003617 private boolean shouldShowWeightWatcher() {
3618 String spKey = LauncherAppState.getSharedPreferencesKey();
3619 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003620 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003621
3622 return show;
3623 }
3624
3625 private void toggleShowWeightWatcher() {
3626 String spKey = LauncherAppState.getSharedPreferencesKey();
3627 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3628 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3629
3630 show = !show;
3631
3632 SharedPreferences.Editor editor = sp.edit();
3633 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3634 editor.commit();
3635
3636 if (mWeightWatcher != null) {
3637 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3638 }
3639 }
3640
Winson Chungd64d1762013-08-20 14:37:16 -07003641 public void bindAppsAdded(final ArrayList<Long> newScreens,
3642 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003643 final ArrayList<ItemInfo> addAnimated,
3644 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003645 Runnable r = new Runnable() {
3646 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003647 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003648 }
3649 };
3650 if (waitUntilResume(r)) {
3651 return;
3652 }
3653
Winson Chungd64d1762013-08-20 14:37:16 -07003654 // Add the new screens
3655 bindAddScreens(newScreens);
3656
3657 // We add the items without animation on non-visible pages, and with
3658 // animations on the new page (which we will try and snap to).
3659 if (!addNotAnimated.isEmpty()) {
3660 bindItems(addNotAnimated, 0,
3661 addNotAnimated.size(), false);
3662 }
3663 if (!addAnimated.isEmpty()) {
3664 bindItems(addAnimated, 0,
3665 addAnimated.size(), true);
3666 }
Winson Chungc58497e2013-09-03 17:48:37 -07003667
Winson Chung87412982013-10-03 18:34:14 -07003668 // Remove the extra empty screen
3669 mWorkspace.removeExtraEmptyScreen();
3670
Winson Chungc58497e2013-09-03 17:48:37 -07003671 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3672 addedApps != null && mAppsCustomizeContent != null) {
3673 mAppsCustomizeContent.addApps(addedApps);
3674 }
Winson Chungd64d1762013-08-20 14:37:16 -07003675 }
3676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003677 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003678 * Bind the items start-end from the list.
3679 *
3680 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003681 */
Winson Chung64359a52013-07-08 17:17:08 -07003682 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3683 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003684 Runnable r = new Runnable() {
3685 public void run() {
3686 bindItems(shortcuts, start, end, forceAnimateIcons);
3687 }
3688 };
3689 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 return;
3691 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003692
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003694 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3695 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003696 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003697 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003698 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003699 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003701
3702 // Short circuit if we are loading dock items for a configuration which has no dock
3703 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3704 mHotseat == null) {
3705 continue;
3706 }
3707
Joe Onorato9c1289c2009-08-17 11:03:03 -04003708 switch (item.itemType) {
3709 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3710 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003712 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003713
Winson Chung64359a52013-07-08 17:17:08 -07003714 /*
3715 * TODO: FIX collision case
3716 */
Winson Chungce376632013-07-11 16:12:41 -07003717 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3718 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3719 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3720 throw new RuntimeException("OCCUPIED");
3721 }
Winson Chung64359a52013-07-08 17:17:08 -07003722 }
3723
Adam Cohendcd297f2013-06-18 13:13:40 -07003724 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3725 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003726 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003727 // Animate all the applications up now
3728 shortcut.setAlpha(0f);
3729 shortcut.setScaleX(0f);
3730 shortcut.setScaleY(0f);
3731 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003732 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003735 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003736 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003737 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003738 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003739 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3740 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003742 default:
3743 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003745 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003746
Winson Chung997a9232013-07-24 15:33:46 -07003747 if (animateIcons) {
3748 // Animate to the correct page
3749 if (newShortcutsScreenId > -1) {
3750 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003751 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003752 final Runnable startBounceAnimRunnable = new Runnable() {
3753 public void run() {
3754 anim.playTogether(bounceAnims);
3755 anim.start();
3756 }
3757 };
Winson Chung997a9232013-07-24 15:33:46 -07003758 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003759 // We post the animation slightly delayed to prevent slowdowns
3760 // when we are loading right after we return to launcher.
3761 mWorkspace.postDelayed(new Runnable() {
3762 public void run() {
3763 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003764 mWorkspace.postDelayed(startBounceAnimRunnable,
3765 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003766 }
3767 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003768 } else {
3769 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003770 }
3771 }
Winson Chung64359a52013-07-08 17:17:08 -07003772 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003774 }
3775
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 /**
3777 * Implementation of the method from LauncherModel.Callbacks.
3778 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003779 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003780 Runnable r = new Runnable() {
3781 public void run() {
3782 bindFolders(folders);
3783 }
3784 };
3785 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003786 return;
3787 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003788 sFolders.clear();
3789 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791
3792 /**
3793 * Add the views for a widget to the workspace.
3794 *
3795 * Implementation of the method from LauncherModel.Callbacks.
3796 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003797 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003798 Runnable r = new Runnable() {
3799 public void run() {
3800 bindAppWidget(item);
3801 }
3802 };
3803 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003804 return;
3805 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003806
Daniel Sandler843e8602010-06-07 14:59:01 -04003807 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3808 if (DEBUG_WIDGETS) {
3809 Log.d(TAG, "bindAppWidget: " + item);
3810 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 final Workspace workspace = mWorkspace;
3812
3813 final int appWidgetId = item.appWidgetId;
3814 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003815 if (DEBUG_WIDGETS) {
3816 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3817 }
3818
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3820
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003822 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823
Adam Cohendcd297f2013-06-18 13:13:40 -07003824 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003826 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3827
Joe Onorato9c1289c2009-08-17 11:03:03 -04003828 workspace.requestLayout();
3829
Daniel Sandler843e8602010-06-07 14:59:01 -04003830 if (DEBUG_WIDGETS) {
3831 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3832 + (SystemClock.uptimeMillis()-start) + "ms");
3833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 }
3835
Adam Cohen1462de32012-07-24 22:34:36 -07003836 public void onPageBoundSynchronously(int page) {
3837 mSynchronouslyBoundPages.add(page);
3838 }
3839
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 /**
3841 * Callback saying that there aren't any more items to bind.
3842 *
3843 * Implementation of the method from LauncherModel.Callbacks.
3844 */
Adam Cohene25af792013-06-06 23:08:25 -07003845 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003846 Runnable r = new Runnable() {
3847 public void run() {
3848 finishBindingItems(upgradePath);
3849 }
3850 };
3851 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003852 return;
3853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 if (mSavedState != null) {
3855 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003856 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003858 mSavedState = null;
3859 }
3860
Adam Cohen1462de32012-07-24 22:34:36 -07003861 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003863 // If we received the result of any pending adds while the loader was running (e.g. the
3864 // widget configuration forced an orientation change), process them now.
3865 for (int i = 0; i < sPendingAddList.size(); i++) {
3866 completeAdd(sPendingAddList.get(i));
3867 }
3868 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869
Winson Chungc51db6a2011-10-05 11:44:49 -07003870 // Update the market app icon as necessary (the other icons will be managed in response to
3871 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003872 if (!DISABLE_MARKET_BUTTON) {
3873 updateAppMarketIcon();
3874 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003875
Winson Chungf0c6ae02012-03-21 16:10:31 -07003876 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003877 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003878 mWorkspace.getUniqueComponents(true, null);
3879 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003880 }
Adam Cohen99894d92013-06-14 11:22:59 -07003881
Adam Cohen66a01fd2013-06-11 12:48:00 -07003882 mWorkspace.post(new Runnable() {
3883 @Override
3884 public void run() {
3885 onFinishBindingItems();
3886 }
3887 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003888 }
3889
Winson Chunga0b7e862013-09-05 16:03:15 -07003890 public boolean isAllAppsButtonRank(int rank) {
3891 if (mHotseat != null) {
3892 return mHotseat.isAllAppsButtonRank(rank);
3893 }
3894 return false;
3895 }
3896
Winson Chunga2413752012-04-03 14:22:34 -07003897 private boolean canRunNewAppsAnimation() {
3898 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3899 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3900 }
3901
Winson Chungc9168342013-06-26 14:54:55 -07003902 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3903 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3904 PropertyValuesHolder.ofFloat("alpha", 1f),
3905 PropertyValuesHolder.ofFloat("scaleX", 1f),
3906 PropertyValuesHolder.ofFloat("scaleY", 1f));
3907 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3908 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3909 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3910 return bounceAnim;
3911 }
3912
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003913 @Override
3914 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003915 boolean searchVisible = updateGlobalSearchIcon();
3916 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003917 if (mSearchDropTargetBar != null) {
3918 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3919 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003920 }
3921
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003922 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 * Add the icons for all apps.
3924 *
3925 * Implementation of the method from LauncherModel.Callbacks.
3926 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003927 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003928 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3929 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3930 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3931 getHotseat().addAllAppsFolder(mIconCache, apps,
3932 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3933 }
3934 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003935 }
Winson Chungc58497e2013-09-03 17:48:37 -07003936 } else {
3937 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3938 mAppsCustomizeContent != null) {
3939 mAppsCustomizeContent.setApps(apps);
3940 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 }
3943
3944 /**
3945 * A package was updated.
3946 *
3947 * Implementation of the method from LauncherModel.Callbacks.
3948 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003949 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003950 Runnable r = new Runnable() {
3951 public void run() {
3952 bindAppsUpdated(apps);
3953 }
3954 };
3955 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003956 return;
3957 }
3958
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003959 if (mWorkspace != null) {
3960 mWorkspace.updateShortcuts(apps);
3961 }
Winson Chungc58497e2013-09-03 17:48:37 -07003962
3963 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3964 mAppsCustomizeContent != null) {
3965 mAppsCustomizeContent.updateApps(apps);
3966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 }
3968
3969 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003970 * A package was uninstalled. We take both the super set of packageNames
3971 * in addition to specific applications to remove, the reason being that
3972 * this can be called when a package is updated as well. In that scenario,
3973 * we only remove specific components from the workspace, where as
3974 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 *
3976 * Implementation of the method from LauncherModel.Callbacks.
3977 */
Winson Chung83892cc2013-05-01 16:53:33 -07003978 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003979 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003980 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003981 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003982 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003983 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003984 }
Winson Chungd64d1762013-08-20 14:37:16 -07003985 };
3986 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003987 return;
3988 }
3989
Winson Chung64359a52013-07-08 17:17:08 -07003990 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003991 mWorkspace.removeItemsByPackageName(packageNames);
3992 } else {
3993 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003994 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003995
Winson Chunga1820962011-10-03 16:31:06 -07003996 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003997 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003998
3999 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4000 mAppsCustomizeContent != null) {
4001 mAppsCustomizeContent.removeApps(appInfos);
4002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 }
Joe Onoratobe386092009-11-17 17:32:16 -08004004
4005 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004006 * A number of packages were updated.
4007 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004008 private ArrayList<Object> mWidgetsAndShortcuts;
4009 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004010 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004011 bindPackagesUpdated(mWidgetsAndShortcuts);
4012 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004013 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004014 };
4015
4016 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4017 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4018 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004019 return;
4020 }
4021
Winson Chung64359a52013-07-08 17:17:08 -07004022 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004023 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4024 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004025 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004026 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004027 }
4028
Winson Chung400438b2011-01-16 17:53:48 -08004029 private int mapConfigurationOriActivityInfoOri(int configOri) {
4030 final Display d = getWindowManager().getDefaultDisplay();
4031 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4032 switch (d.getRotation()) {
4033 case Surface.ROTATION_0:
4034 case Surface.ROTATION_180:
4035 // We are currently in the same basic orientation as the natural orientation
4036 naturalOri = configOri;
4037 break;
4038 case Surface.ROTATION_90:
4039 case Surface.ROTATION_270:
4040 // We are currently in the other basic orientation to the natural orientation
4041 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4042 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4043 break;
4044 }
4045
4046 int[] oriMap = {
4047 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4048 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4049 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4050 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4051 };
4052 // Since the map starts at portrait, we need to offset if this device's natural orientation
4053 // is landscape.
4054 int indexOffset = 0;
4055 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4056 indexOffset = 1;
4057 }
4058 return oriMap[(d.getRotation() + indexOffset) % 4];
4059 }
Adam Cohen446e9402011-09-15 18:21:21 -07004060
Winson Chung4b919f82012-05-01 10:44:08 -07004061 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004062 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004063 getResources().getBoolean(R.bool.allow_rotation);
4064 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004065 }
Winson Chung4b919f82012-05-01 10:44:08 -07004066 public void lockScreenOrientation() {
4067 if (isRotationEnabled()) {
4068 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4069 .getConfiguration().orientation));
4070 }
4071 }
4072 public void unlockScreenOrientation(boolean immediate) {
4073 if (isRotationEnabled()) {
4074 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004075 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004076 } else {
4077 mHandler.postDelayed(new Runnable() {
4078 public void run() {
4079 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4080 }
4081 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004082 }
Winson Chung4b919f82012-05-01 10:44:08 -07004083 }
Winson Chung400438b2011-01-16 17:53:48 -08004084 }
4085
Winson Chung82f55532011-08-09 14:14:23 -07004086 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004087 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004088 if (DISABLE_CLINGS) {
4089 return false;
4090 }
4091
Brett Chabot2a9e2282011-08-23 15:03:13 -07004092 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004094
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004095 // Disable clings for accessibility when explore by touch is enabled
4096 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4097 ACCESSIBILITY_SERVICE);
4098 if (a11yManager.isTouchExplorationEnabled()) {
4099 return false;
4100 }
4101
Michael Jurkae233a8b2013-03-19 13:49:20 +01004102 // Restricted secondary users (child mode) will potentially have very few apps
4103 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004104// boolean supportsLimitedUsers =
4105// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4106// Account[] accounts = AccountManager.get(this).getAccounts();
4107// if (supportsLimitedUsers && accounts.length == 0) {
4108// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4109// Bundle restrictions = um.getUserRestrictions();
4110// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4111// return false;
4112// }
4113// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004115 }
Michael Jurka22143132012-10-04 17:42:38 +02004116
Winson Chungfa545132013-09-26 17:45:32 -07004117 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004118 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004119 Cling cling = (Cling) findViewById(clingId);
4120 View scrim = null;
4121 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004122 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004123 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004125 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004126 cling.show(animate, SHOW_CLING_DURATION);
4127
4128 if (dimNavBarVisibilty) {
4129 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4130 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004131 }
4132 }
4133 return cling;
4134 }
Michael Jurka22143132012-10-04 17:42:38 +02004135
Winson Chungaf40f202013-09-18 18:26:31 -07004136 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4137 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004138 // To catch cases where siblings of top-level views are made invisible, just check whether
4139 // the cling is directly set to GONE before dismissing it.
4140 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004141 final Runnable cleanUpClingCb = new Runnable() {
4142 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004143 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004144 // We should update the shared preferences on a background thread
4145 new Thread("dismissClingThread") {
4146 public void run() {
4147 SharedPreferences.Editor editor = mSharedPrefs.edit();
4148 editor.putBoolean(flag, true);
4149 editor.commit();
4150 }
4151 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004152 if (postAnimationCb != null) {
4153 postAnimationCb.run();
4154 }
4155 }
4156 };
4157 if (duration <= 0) {
4158 cleanUpClingCb.run();
4159 } else {
4160 cling.hide(duration, cleanUpClingCb);
4161 }
Michael Jurka22143132012-10-04 17:42:38 +02004162 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004163
4164 if (restoreNavBarVisibilty) {
4165 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4166 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4167 }
Winson Chung82f55532011-08-09 14:14:23 -07004168 }
4169 }
Michael Jurka22143132012-10-04 17:42:38 +02004170
Winson Chung9d9d74f2011-09-19 11:49:12 -07004171 private void removeCling(int id) {
4172 final View cling = findViewById(id);
4173 if (cling != null) {
4174 final ViewGroup parent = (ViewGroup) cling.getParent();
4175 parent.post(new Runnable() {
4176 @Override
4177 public void run() {
4178 parent.removeView(cling);
4179 }
4180 });
Michael Jurka22143132012-10-04 17:42:38 +02004181 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004182 }
4183 }
Michael Jurka974c3862012-05-22 22:00:31 -07004184
4185 private boolean skipCustomClingIfNoAccounts() {
4186 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4187 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4188 if (customCling) {
4189 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004190 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004191 Account[] accounts = am.getAccountsByType("com.google");
4192 return accounts.length == 0;
4193 }
4194 return false;
4195 }
4196
Winson Chungaf40f202013-09-18 18:26:31 -07004197 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004198 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004199 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004200 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004201 // If we're not using the default workspace layout, replace workspace cling
4202 // with a custom workspace cling (usually specified in an overlay)
4203 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004204 if (!DISABLE_CUSTOM_CLINGS) {
4205 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4206 getResources().getBoolean(R.bool.config_useCustomClings)) {
4207 // Use a custom cling
4208 View cling = findViewById(R.id.workspace_cling);
4209 ViewGroup clingParent = (ViewGroup) cling.getParent();
4210 int clingIndex = clingParent.indexOfChild(cling);
4211 clingParent.removeViewAt(clingIndex);
4212 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4213 clingParent.addView(customCling, clingIndex);
4214 customCling.setId(R.id.workspace_cling);
4215 }
4216 }
4217 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4218 if (cling != null) {
4219 String sbHintStr = getFirstRunClingSearchBarHint();
4220 String ccHintStr = getFirstRunCustomContentHint();
4221 if (!sbHintStr.isEmpty()) {
4222 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4223 sbHint.setText(sbHintStr);
4224 sbHint.setVisibility(View.VISIBLE);
4225 }
4226 if (!ccHintStr.isEmpty()) {
4227 TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4228 ccHint.setText(ccHintStr);
4229 ccHint.setVisibility(View.VISIBLE);
4230 }
Michael Jurka45355c42012-10-08 13:21:35 +02004231 }
Winson Chungfa545132013-09-26 17:45:32 -07004232 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004233 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004234 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004235 }
4236 }
Winson Chungaf40f202013-09-18 18:26:31 -07004237
Winson Chunge6eabff2013-09-24 11:01:23 -07004238 protected String getFirstRunClingSearchBarHint() {
4239 return "";
4240 }
4241 protected String getFirstRunCustomContentHint() {
4242 return "";
4243 }
4244
Winson Chungaf40f202013-09-18 18:26:31 -07004245 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004246 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004247 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004248 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004249 initCling(R.id.workspace_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004250 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004251 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004252 }
4253 }
4254 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004255 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004256 if (isClingsEnabled() &&
4257 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004258 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4259 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004260 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004261 } else {
4262 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004263 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004264 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004265 }
Michael Jurka104c4562013-07-08 18:03:46 -07004266 protected SharedPreferences getSharedPrefs() {
4267 return mSharedPrefs;
4268 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004269 public boolean isFolderClingVisible() {
4270 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004271 if (cling != null) {
4272 return cling.getVisibility() == View.VISIBLE;
4273 }
4274 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004275 }
Winson Chungaf40f202013-09-18 18:26:31 -07004276 public void dismissFirstRunCling(View v) {
4277 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4278 Runnable cb = new Runnable() {
4279 public void run() {
4280 // Show the workspace cling next
4281 showFirstRunWorkspaceCling();
4282 }
4283 };
4284 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4285 DISMISS_CLING_DURATION, false);
4286 }
Winson Chung82f55532011-08-09 14:14:23 -07004287 public void dismissWorkspaceCling(View v) {
4288 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004289 Runnable cb = null;
4290 if (v == null) {
4291 cb = new Runnable() {
4292 public void run() {
4293 mWorkspace.enterOverviewMode();
4294 }
4295 };
4296 }
4297 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4298 DISMISS_CLING_DURATION, true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004299 }
4300 public void dismissFolderCling(View v) {
4301 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004302 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4303 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004304 }
4305
Winson Chung80baf5a2010-08-09 16:03:15 -07004306 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004307 * Prints out out state for debugging.
4308 */
4309 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004310 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004311 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004312 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4313 Log.d(TAG, "mRestoring=" + mRestoring);
4314 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4315 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004316 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004317 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004318
Winson Chung785d2eb2011-04-14 16:08:02 -07004319 if (mAppsCustomizeContent != null) {
4320 mAppsCustomizeContent.dumpState();
4321 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004322 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004323 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004324
4325 @Override
4326 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4327 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004328 synchronized (sDumpLogs) {
4329 writer.println(" ");
4330 writer.println("Debug logs: ");
4331 for (int i = 0; i < sDumpLogs.size(); i++) {
4332 writer.println(" " + sDumpLogs.get(i));
4333 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004334 }
4335 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004336
4337 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004338 if (DEBUG_DUMP_LOG) {
4339 synchronized (sDumpLogs) {
4340 Log.d(TAG, "");
4341 Log.d(TAG, "*********************");
4342 Log.d(TAG, "Launcher debug logs: ");
4343 for (int i = 0; i < sDumpLogs.size(); i++) {
4344 Log.d(TAG, " " + sDumpLogs.get(i));
4345 }
4346 Log.d(TAG, "*********************");
4347 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004348 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004349 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004350 }
4351
4352 public static void addDumpLog(String tag, String log, boolean debugLog) {
4353 if (debugLog) {
4354 Log.d(tag, log);
4355 }
Winson Chungede41292013-09-19 16:27:36 -07004356 if (DEBUG_DUMP_LOG) {
4357 sDateStamp.setTime(System.currentTimeMillis());
4358 synchronized (sDumpLogs) {
4359 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004360 }
Winson Chungede41292013-09-19 16:27:36 -07004361 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004362 }
4363
Winson Chungede41292013-09-19 16:27:36 -07004364 public void dumpLogsToLocalData() {
4365 if (DEBUG_DUMP_LOG) {
4366 new Thread("DumpLogsToLocalData") {
4367 @Override
4368 public void run() {
4369 boolean success = false;
4370 sDateStamp.setTime(sRunStart);
4371 String FILENAME = sDateStamp.getMonth() + "-"
4372 + sDateStamp.getDay() + "_"
4373 + sDateStamp.getHours() + "-"
4374 + sDateStamp.getMinutes() + "_"
4375 + sDateStamp.getSeconds() + ".txt";
4376
4377 FileOutputStream fos = null;
4378 File outFile = null;
4379 try {
4380 outFile = new File(getFilesDir(), FILENAME);
4381 outFile.createNewFile();
4382 fos = new FileOutputStream(outFile);
4383 } catch (Exception e) {
4384 e.printStackTrace();
4385 }
4386 if (fos != null) {
4387 PrintWriter writer = new PrintWriter(fos);
4388
4389 writer.println(" ");
4390 writer.println("Debug logs: ");
4391 synchronized (sDumpLogs) {
4392 for (int i = 0; i < sDumpLogs.size(); i++) {
4393 writer.println(" " + sDumpLogs.get(i));
4394 }
4395 }
4396 writer.close();
4397 }
4398 try {
4399 if (fos != null) {
4400 fos.close();
4401 success = true;
4402 }
4403 } catch (IOException e) {
4404 e.printStackTrace();
4405 }
4406 }
4407 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004408 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004410}
Michael Jurka2763be32011-02-24 11:19:57 -08004411
Michael Jurkaabded662011-03-04 12:06:57 -08004412interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004413 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004414 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004415 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004416 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004417 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004418}