blob: afaf2234af6cdee1d123637a450c51abf10ae381 [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 Sandler6053b802013-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;
Adam Cohened307df2013-10-02 09:37:31 -0700273 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700275 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700276 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700278 private static LocaleConfiguration sLocaleConfiguration = null;
279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
Adam Cohen61f560d2013-09-30 15:58:20 -0700282 private View.OnTouchListener mHapticFeedbackTouchListener;
283
Adam Cohended9f8d2010-11-03 13:25:16 -0700284 // Related to the auto-advancing of widgets
285 private final int ADVANCE_MSG = 1;
286 private final int mAdvanceInterval = 20000;
287 private final int mAdvanceStagger = 250;
288 private long mAutoAdvanceSentTime;
289 private long mAutoAdvanceTimeLeft = -1;
290 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
291 new HashMap<View, AppWidgetProviderInfo>();
292
Winson Chung400438b2011-01-16 17:53:48 -0800293 // Determines how long to wait after a rotation before restoring the screen orientation to
294 // match the sensor state.
295 private final int mRestoreScreenOrientationDelay = 500;
296
Michael Jurka4ef207b2010-11-29 17:05:45 -0800297 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700298 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800301
Winson Chungd64a6662013-09-30 11:06:59 -0700302 private Intent mAppMarketIntent = null;
303 private static final boolean DISABLE_MARKET_BUTTON = true;
304
Craig Mautner360310b2012-10-26 15:13:08 -0700305 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700306
Adam Cohen1462de32012-07-24 22:34:36 -0700307 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
308
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700309 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700310 static Date sDateStamp = new Date();
311 static DateFormat sDateFormat =
312 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
313 static long sRunStart = System.currentTimeMillis();
314 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700315
Winson Chung46353de2012-02-16 14:05:10 -0800316 // We only want to get the SharedPreferences once since it does an FS stat each time we get
317 // it from the context.
318 private SharedPreferences mSharedPrefs;
319
Adam Cohene25af792013-06-06 23:08:25 -0700320 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
321
Winson Chungf0c6ae02012-03-21 16:10:31 -0700322 // Holds the page that we need to animate to, and the icon views that we need to animate up
323 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700324 private ImageView mFolderIconImageView;
325 private Bitmap mFolderIconBitmap;
326 private Canvas mFolderIconCanvas;
327 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700328
Michael Jurkaddd62e92011-02-16 17:49:14 -0800329 private BubbleTextView mWaitingForResume;
330
Michael Jurka22143132012-10-04 17:42:38 +0200331 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
332 = new HideFromAccessibilityHelper();
333
Michael Jurkac1f5d262011-09-30 19:32:27 -0700334 private Runnable mBuildLayersRunnable = new Runnable() {
335 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700336 if (mWorkspace != null) {
337 mWorkspace.buildPageHardwareLayers();
338 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700339 }
340 };
341
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800342 private static ArrayList<PendingAddArguments> sPendingAddList
343 = new ArrayList<PendingAddArguments>();
344
Michael Jurka7267fa52013-09-26 15:29:57 -0700345 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800346
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800347 private static class PendingAddArguments {
348 int requestCode;
349 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700350 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700351 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800352 int cellX;
353 int cellY;
354 }
355
Daniel Sandlerff02d492013-08-05 02:12:05 -0400356 private Stats mStats;
357
Michael Jurka0a457bf2012-11-19 14:05:05 -0800358 private static boolean isPropertyEnabled(String propertyName) {
359 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700360 }
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 @Override
363 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700364 if (DEBUG_STRICT_MODE) {
365 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
366 .detectDiskReads()
367 .detectDiskWrites()
368 .detectNetwork() // or .detectAll() for all detectable problems
369 .penaltyLog()
370 .build());
371 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
372 .detectLeakedSqlLiteObjects()
373 .detectLeakedClosableObjects()
374 .penaltyLog()
375 .penaltyDeath()
376 .build());
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400380
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400381 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400382 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700383
384 // Determine the dynamic grid properties
385 Point smallestSize = new Point();
386 Point largestSize = new Point();
387 Point realSize = new Point();
388 Display display = getWindowManager().getDefaultDisplay();
389 display.getCurrentSizeRange(smallestSize, largestSize);
390 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700391 DisplayMetrics dm = new DisplayMetrics();
392 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700393 // Lazy-initialize the dynamic grid
394 DeviceProfile grid = app.initDynamicGrid(this,
395 Math.min(smallestSize.x, smallestSize.y),
396 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700397 realSize.x, realSize.y,
398 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700399
400 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400401 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700402 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 mModel = app.setLauncher(this);
404 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400405 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700407
Daniel Sandlerff02d492013-08-05 02:12:05 -0400408 mStats = new Stats(this);
409
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700411
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
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100430 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800432 registerContentObservers();
433
Joe Onorato7c312c12009-08-13 21:36:53 -0700434 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mSavedState = savedInstanceState;
437 restoreState(mSavedState);
438
Winson Chunga12a2502010-12-20 14:41:35 -0800439 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700440 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200441 mAppsCustomizeContent.onPackagesUpdated(
442 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700443 }
Winson Chunga12a2502010-12-20 14:41:35 -0800444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 if (PROFILE_STARTUP) {
446 android.os.Debug.stopMethodTracing();
447 }
448
449 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700450 if (sPausedFromUserAction) {
451 // If the user leaves launcher, then we should just load items asynchronously when
452 // they return.
453 mModel.startLoader(true, -1);
454 } else {
455 // We only load the page synchronously if the user rotates (or triggers a
456 // configuration change) while launcher is in the foreground
457 mModel.startLoader(true, mWorkspace.getCurrentPage());
458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460
461 // For handling default keys
462 mDefaultKeySsb = new SpannableStringBuilder();
463 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800464
465 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
466 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467
Adam Cohenf9426d52012-06-04 17:26:21 -0700468 updateGlobalIcons();
469
470 // On large interfaces, we want the screen to auto-rotate based on the current orientation
471 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700472
473 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700474 }
475
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 protected void onUserLeaveHint() {
477 super.onUserLeaveHint();
478 sPausedFromUserAction = true;
479 }
480
Winson Chung98ca0f72013-07-29 12:58:51 -0700481 /** To be overriden by subclasses to hint to Launcher that we have custom content */
482 protected boolean hasCustomContentToLeft() {
483 return false;
484 }
485
Dave Hawkeya8881582013-09-17 15:55:33 -0600486 /**
487 * To be overridden by subclasses to create the custom content and call
488 * {@link #addToCustomContentPage}. This will only be invoked if
489 * {@link #hasCustomContentToLeft()} is {@code true}.
490 */
491 protected void addCustomContentToLeft() {
492 }
493
494 /**
495 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
496 * ensure the custom content page is added or removed if necessary.
497 */
498 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600499 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600500 // Not bound yet, wait for bindScreens to be called.
501 return;
502 }
503
504 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
505 // Create the custom content page and call the subclass to populate it.
506 mWorkspace.createCustomContentPage();
507 addCustomContentToLeft();
508 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
509 mWorkspace.removeCustomContentPage();
510 }
511 }
512
Adam Cohenf9426d52012-06-04 17:26:21 -0700513 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700514 boolean searchVisible = false;
515 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800516 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700517 int coi = getCurrentOrientationIndexForGlobalIcons();
518 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
519 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700520 if (!DISABLE_MARKET_BUTTON) {
521 updateAppMarketIcon();
522 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700523 searchVisible = updateGlobalSearchIcon();
524 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700525 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700526 if (sGlobalSearchIcon[coi] != null) {
527 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700528 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700529 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700530 if (sVoiceSearchIcon[coi] != null) {
531 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700532 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700533 }
Winson Chungd64a6662013-09-30 11:06:59 -0700534 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700535 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800536 }
Winson Chungadf0c182012-08-23 14:59:07 -0700537 if (mSearchDropTargetBar != null) {
538 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
Romain Guycbb89e42009-06-08 15:52:54 -0700541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700543 if (sLocaleConfiguration == null) {
544 new AsyncTask<Void, Void, LocaleConfiguration>() {
545 @Override
546 protected LocaleConfiguration doInBackground(Void... unused) {
547 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
548 readConfiguration(Launcher.this, localeConfiguration);
549 return localeConfiguration;
550 }
551
552 @Override
553 protected void onPostExecute(LocaleConfiguration result) {
554 sLocaleConfiguration = result;
555 checkForLocaleChange(); // recursive, but now with a locale configuration
556 }
557 }.execute();
558 return;
559 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 final Configuration configuration = getResources().getConfiguration();
562
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 final String locale = configuration.locale.toString();
565
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int mcc = configuration.mcc;
568
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700569 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 final int mnc = configuration.mnc;
571
Romain Guy84f296c2009-11-04 15:00:44 -0800572 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
Romain Guy84f296c2009-11-04 15:00:44 -0800574 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700575 sLocaleConfiguration.locale = locale;
576 sLocaleConfiguration.mcc = mcc;
577 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700578
Joe Onorato0589f0f2010-02-08 13:44:00 -0800579 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580
581 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
582 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700583 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 public void run() {
585 writeConfiguration(Launcher.this, localeConfiguration);
586 }
587 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700588 }
589 }
590
591 private static class LocaleConfiguration {
592 public String locale;
593 public int mcc = -1;
594 public int mnc = -1;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
Romain Guy98d01652009-06-30 16:21:04 -0700597 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
598 DataInputStream in = null;
599 try {
600 in = new DataInputStream(context.openFileInput(PREFERENCES));
601 configuration.locale = in.readUTF();
602 configuration.mcc = in.readInt();
603 configuration.mnc = in.readInt();
604 } catch (FileNotFoundException e) {
605 // Ignore
606 } catch (IOException e) {
607 // Ignore
608 } finally {
609 if (in != null) {
610 try {
611 in.close();
612 } catch (IOException e) {
613 // Ignore
614 }
615 }
616 }
617 }
618
619 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
620 DataOutputStream out = null;
621 try {
622 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
623 out.writeUTF(configuration.locale);
624 out.writeInt(configuration.mcc);
625 out.writeInt(configuration.mnc);
626 out.flush();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 //noinspection ResultOfMethodCallIgnored
631 context.getFileStreamPath(PREFERENCES).delete();
632 } finally {
633 if (out != null) {
634 try {
635 out.close();
636 } catch (IOException e) {
637 // Ignore
638 }
639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641 }
642
Anton Hanssona2f665f2013-09-26 12:18:32 +0100643 public Stats getStats() {
644 return mStats;
645 }
646
Bjorn Bringertc459e522013-06-07 19:36:01 +0100647 public LayoutInflater getInflater() {
648 return mInflater;
649 }
650
Adam Cohen716b51e2011-06-30 12:09:54 -0700651 public DragLayer getDragLayer() {
652 return mDragLayer;
653 }
654
Winson Chung36a62fe2012-05-06 18:04:42 -0700655 boolean isDraggingEnabled() {
656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
658 return !mModel.isLoadingWorkspace();
659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 static int getScreen() {
662 synchronized (sLock) {
663 return sScreen;
664 }
665 }
666
667 static void setScreen(int screen) {
668 synchronized (sLock) {
669 sScreen = screen;
670 }
671 }
672
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 /**
674 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
675 * a configuration step, this allows the proper animations to run after other transitions.
676 */
677 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700678 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 switch (args.requestCode) {
680 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800683 break;
684 case REQUEST_PICK_SHORTCUT:
685 processShortcut(args.intent);
686 break;
687 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700689 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700690 result = true;
691 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 case REQUEST_CREATE_APPWIDGET:
693 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700695 result = true;
696 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800697 }
Michael Jurka27614d22012-04-02 06:40:22 -0700698 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
699 // if you turned the screen off and then back while in All Apps, Launcher would not
700 // return to the workspace. Clearing mAddInfo.container here fixes this issue
701 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700702 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700706 protected void onActivityResult(
707 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700708 // Reset the startActivity waiting flag
709 mWaitingForResult = false;
710
Michael Jurka8b805b12012-04-18 14:23:14 -0700711 if (requestCode == REQUEST_BIND_APPWIDGET) {
712 int appWidgetId = data != null ?
713 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
714 if (resultCode == RESULT_CANCELED) {
715 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
716 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700717 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700718 }
719 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200720 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
721 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
722 mWorkspace.exitOverviewMode(false);
723 }
724 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700725 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200726
Winson Chung557d6ed2011-07-08 15:34:52 -0700727 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
729 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700730
Adam Cohened66b2b2012-01-23 17:28:51 -0800731 // We have special handling for widgets
732 if (isWidgetDrop) {
733 int appWidgetId = data != null ?
734 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700735 if (appWidgetId < 0) {
736 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
737 "widget configuration activity.");
738 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
739 } else {
740 completeTwoStageWidgetDrop(resultCode, appWidgetId);
741 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 return;
743 }
744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 // The pattern used here is that a user PICKs a specific application,
746 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
749 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700750 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 final PendingAddArguments args = new PendingAddArguments();
752 args.requestCode = requestCode;
753 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700755 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 args.cellX = mPendingAddInfo.cellX;
757 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 if (isWorkspaceLocked()) {
759 sPendingAddList.add(args);
760 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700761 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700765 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800766 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
767 null);
768 }
769
770 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 Runnable onCompleteRunnable = null;
774 int animationType = 0;
775
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700776 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800777 if (resultCode == RESULT_OK) {
778 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
779 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700780 mPendingAddWidgetInfo);
781 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 onCompleteRunnable = new Runnable() {
783 @Override
784 public void run() {
785 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
788 null);
789 }
790 };
791 } else if (resultCode == RESULT_CANCELED) {
792 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
793 onCompleteRunnable = new Runnable() {
794 @Override
795 public void run() {
796 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
797 null);
798 }
799 };
800 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700801 if (mDragLayer.getAnimatedView() != null) {
802 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
803 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
804 animationType, boundWidget, true);
805 } else {
806 // The animated view may be null in the case of a rotation during widget configuration
807 onCompleteRunnable.run();
808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 }
810
811 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700812 protected void onStop() {
813 super.onStop();
814 FirstFrameAnimatorHelper.setIsVisible(false);
815 }
816
817 @Override
818 protected void onStart() {
819 super.onStart();
820 FirstFrameAnimatorHelper.setIsVisible(true);
821 }
822
823 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200825 long startTime = 0;
826 if (DEBUG_RESUME_TIME) {
827 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400828 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700831
Winson Chung4a2afa32012-07-19 14:53:05 -0700832 // Restore the previous launcher state
833 if (mOnResumeState == State.WORKSPACE) {
834 showWorkspace(false);
835 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700836 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700837 }
838 mOnResumeState = State.NONE;
839
Craig Mautner360310b2012-10-26 15:13:08 -0700840 // Background was set to gradient in onPause(), restore to black if in all apps.
841 setWorkspaceBackground(mState == State.WORKSPACE);
842
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800843 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700844 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700845 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700847 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700849 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700851 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200852 // We might have postponed some bind calls until onResume (see waitUntilResume) --
853 // execute them here
854 long startTimeCallbacks = 0;
855 if (DEBUG_RESUME_TIME) {
856 startTimeCallbacks = System.currentTimeMillis();
857 }
858
859 if (mAppsCustomizeContent != null) {
860 mAppsCustomizeContent.setBulkBind(true);
861 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700862 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
863 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200864 }
865 if (mAppsCustomizeContent != null) {
866 mAppsCustomizeContent.setBulkBind(false);
867 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700868 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200869 if (DEBUG_RESUME_TIME) {
870 Log.d(TAG, "Time spent processing callbacks in onResume: " +
871 (System.currentTimeMillis() - startTimeCallbacks));
872 }
Michael Jurka447bf842013-05-15 14:52:15 +0200873 }
Michael Jurka54554252013-08-01 12:52:23 +0200874 if (mOnResumeCallbacks.size() > 0) {
875 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
876 mOnResumeCallbacks.get(i).run();
877 }
878 mOnResumeCallbacks.clear();
879 }
Winson Chunge4e50662012-01-23 14:45:13 -0800880
881 // Reset the pressed state of icons that were locked in the press state while activities
882 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800883 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800884 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800885 mWaitingForResume.setStayPressed(false);
886 }
Winson Chunge4e50662012-01-23 14:45:13 -0800887 if (mAppsCustomizeContent != null) {
888 // Resets the previous all apps icon press state
889 mAppsCustomizeContent.resetDrawableState();
890 }
Winson Chung82963d52013-10-09 11:20:57 -0700891
Adam Cohen06dff352012-06-01 17:17:08 -0700892 // It is possible that widgets can receive updates while launcher is not in the foreground.
893 // Consequently, the widgets will be inflated in the orientation of the foreground activity
894 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
895 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700896 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700897
Winson Chung780fe592013-09-26 14:48:44 -0700898 // Process any items that were added while Launcher was away.
899 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
900
Winson Chung5841efa2013-09-30 18:06:44 -0700901 // Update the voice search button proxy
902 updateVoiceButtonProxyVisible(false);
903
Adam Cohenf9426d52012-06-04 17:26:21 -0700904 // Again, as with the above scenario, it's possible that one or more of the global icons
905 // were updated in the wrong orientation.
906 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200907 if (DEBUG_RESUME_TIME) {
908 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
909 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700910
911 if (mWorkspace.getCustomContentCallbacks() != null) {
912 // If we are resuming and the custom content is the current page, we call onShow().
913 // It is also poassible that onShow will instead be called slightly after first layout
914 // if PagedView#setRestorePage was set to the custom content page in onCreate().
915 if (mWorkspace.isOnOrMovingToCustomContent()) {
916 mWorkspace.getCustomContentCallbacks().onShow();
917 }
918 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700919 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700920 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700921 }
922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700925 // Ensure that items added to Launcher are queued until Launcher returns
926 InstallShortcutReceiver.enableInstallQueue();
927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700929 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800930 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700931 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700932
933 // We call onHide() aggressively. The custom content callbacks should be able to
934 // debounce excess onHide calls.
935 if (mWorkspace.getCustomContentCallbacks() != null) {
936 mWorkspace.getCustomContentCallbacks().onHide();
937 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938 }
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Adam Cohen66a01fd2013-06-11 12:48:00 -0700940 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700941 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600942 addCustomContentToLeft();
943 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700944 }
945
Adam Cohenbffe7452013-07-22 18:21:45 -0700946 QSBScroller mQsbScroller = new QSBScroller() {
947 int scrollY = 0;
948
949 @Override
950 public void setScrollY(int scroll) {
951 scrollY = scroll;
952
953 if (mWorkspace.isOnOrMovingToCustomContent()) {
954 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100955 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700956 }
957 }
958 };
959
960 public void resetQSBScroll() {
961 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100962 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700963 }
964
965 public interface CustomContentCallbacks {
966 // Custom content is completely shown
967 public void onShow();
968
969 // Custom content is completely hidden
970 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700971
972 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
973 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700974 }
975
Adam Cohen30bacb22013-08-29 14:25:25 -0700976 protected void startSettings() {
977 }
978
Adam Cohenbffe7452013-07-22 18:21:45 -0700979 public interface QSBScroller {
980 public void setScrollY(int scrollY);
981 }
982
Winson Chung98ca0f72013-07-29 12:58:51 -0700983 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -0700984 CustomContentCallbacks callbacks, String description) {
985 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -0700986 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700987 }
988
Adam Cohenedb40762013-07-18 16:45:45 -0700989 // The custom content needs to offset its content to account for the QSB
990 public int getTopOffsetForCustomContent() {
991 return mWorkspace.getPaddingTop();
992 }
993
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700994 @Override
995 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 // Flag the loader to stop early before switching
997 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700998 if (mAppsCustomizeContent != null) {
999 mAppsCustomizeContent.surrender();
1000 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001001 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001002 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001003
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001004 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001005 @Override
1006 public void onWindowFocusChanged(boolean hasFocus) {
1007 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001008 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001009 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 private boolean acceptFilter() {
1012 final InputMethodManager inputManager = (InputMethodManager)
1013 getSystemService(Context.INPUT_METHOD_SERVICE);
1014 return !inputManager.isFullscreenMode();
1015 }
1016
1017 @Override
1018 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001019 final int uniChar = event.getUnicodeChar();
1020 final boolean handled = super.onKeyDown(keyCode, event);
1021 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1022 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1024 keyCode, event);
1025 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001026 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001027 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001028 // showSearchDialog()
1029 // If there are multiple keystrokes before the search dialog takes focus,
1030 // onSearchRequested() will be called for every keystroke,
1031 // but it is idempotent, so it's fine.
1032 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
1034 }
1035
Joe Onorato8a9625e2010-01-28 15:55:35 -08001036 // Eat the long press event so the keyboard doesn't come up.
1037 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1038 return true;
1039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 return handled;
1042 }
1043
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 private String getTypedText() {
1045 return mDefaultKeySsb.toString();
1046 }
1047
1048 private void clearTypedText() {
1049 mDefaultKeySsb.clear();
1050 mDefaultKeySsb.clearSpans();
1051 Selection.setSelection(mDefaultKeySsb, 0);
1052 }
1053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001055 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1056 * State
1057 */
1058 private static State intToState(int stateOrdinal) {
1059 State state = State.WORKSPACE;
1060 final State[] stateValues = State.values();
1061 for (int i = 0; i < stateValues.length; i++) {
1062 if (stateValues[i].ordinal() == stateOrdinal) {
1063 state = stateValues[i];
1064 break;
1065 }
1066 }
1067 return state;
1068 }
1069
1070 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 * Restores the previous state, if it exists.
1072 *
1073 * @param savedState The previous state.
1074 */
1075 private void restoreState(Bundle savedState) {
1076 if (savedState == null) {
1077 return;
1078 }
1079
Michael Jurka883f55b2010-10-21 15:47:14 -07001080 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001081 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001082 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001083 }
1084
Adam Cohen21cd0022013-10-09 18:57:02 -07001085 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1086 PagedView.INVALID_RESTORE_PAGE);
1087 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001088 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
1090
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093
Winson Chung3d503fb2011-07-13 17:25:49 -07001094 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1095 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001097 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1098 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001099 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1100 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1101 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001102 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 mRestoring = true;
1104 }
1105
1106 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1107 if (renameFolder) {
1108 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001109 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 mRestoring = true;
1111 }
Winson Chunga12a2502010-12-20 14:41:35 -08001112
Winson Chung785d2eb2011-04-14 16:08:02 -07001113 // Restore the AppsCustomize tab
1114 if (mAppsCustomizeTabHost != null) {
1115 String curTab = savedState.getString("apps_customize_currentTab");
1116 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001117 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001118 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001119 mAppsCustomizeContent.loadAssociatedPages(
1120 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001121 }
1122
Winson Chung5afbf7b2011-07-25 11:53:08 -07001123 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1124 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127
1128 /**
1129 * Finds all the views we need and configure them properly.
1130 */
1131 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001132 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001133
Craig Mautner360310b2012-10-26 15:13:08 -07001134 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001135 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1136 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001137
John Spurlock77e1f472013-09-11 10:09:51 -04001138 mLauncherView.setSystemUiVisibility(
1139 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001140 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141
Winson Chung4c98d922011-05-31 16:50:48 -07001142 // Setup the drag layer
1143 mDragLayer.setup(this, dragController);
1144
Winson Chung3d503fb2011-07-13 17:25:49 -07001145 // Setup the hotseat
1146 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1147 if (mHotseat != null) {
1148 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001149 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 }
1151
Adam Cohenf358a4b2013-07-23 16:47:31 -07001152 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001153 View widgetButton = findViewById(R.id.widget_button);
1154 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001155 @Override
1156 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001157 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001158 }
1159 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001160 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1161
1162 View wallpaperButton = findViewById(R.id.wallpaper_button);
1163 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001164 @Override
1165 public void onClick(View arg0) {
1166 startWallpaper();
1167 }
1168 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001169 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1170
1171 View settingsButton = findViewById(R.id.settings_button);
1172 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001173 @Override
1174 public void onClick(View arg0) {
1175 startSettings();
1176 }
1177 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001178 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001179 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001180
Winson Chung4c98d922011-05-31 16:50:48 -07001181 // Setup the workspace
1182 mWorkspace.setHapticFeedbackEnabled(false);
1183 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001184 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001185 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001186
Winson Chungf0ea4d32011-06-06 14:27:16 -07001187 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001188 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001189
Winson Chungf0ea4d32011-06-06 14:27:16 -07001190 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001191 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001192 mAppsCustomizeContent = (AppsCustomizePagedView)
1193 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1194 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001195
Winson Chung3d503fb2011-07-13 17:25:49 -07001196 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001197 dragController.setDragScoller(mWorkspace);
1198 dragController.setScrollView(mDragLayer);
1199 dragController.setMoveTarget(mWorkspace);
1200 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001201 if (mSearchDropTargetBar != null) {
1202 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001203 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001204
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001205 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001206 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001207 mWeightWatcher = new WeightWatcher(this);
1208 mWeightWatcher.setAlpha(0.5f);
1209 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001210 new FrameLayout.LayoutParams(
1211 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001212 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001213 Gravity.BOTTOM)
1214 );
Adam Cohen39a06042013-07-19 14:30:12 -07001215
1216 boolean show = shouldShowWeightWatcher();
1217 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
1220
1221 /**
1222 * Creates a view representing a shortcut.
1223 *
1224 * @param info The data structure describing the shortcut.
1225 *
1226 * @return A View inflated from R.layout.application.
1227 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001228 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001230 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232
1233 /**
1234 * Creates a view representing a shortcut inflated from the specified resource.
1235 *
1236 * @param layoutResId The id of the XML layout used to create the shortcut.
1237 * @param parent The group the shortcut belongs to.
1238 * @param info The data structure describing the shortcut.
1239 *
1240 * @return A View inflated from layoutResId.
1241 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001242 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001243 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1244 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 return favorite;
1247 }
1248
1249 /**
1250 * Add an application shortcut to the workspace.
1251 *
1252 * @param data The intent describing the application.
1253 * @param cellInfo The position on screen where to create the shortcut.
1254 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001255 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001257 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258
Adam Cohenfbba09b2011-07-18 21:43:05 -07001259 // First we check if we already know the exact location where we want to add this item.
1260 if (cellX >= 0 && cellY >= 0) {
1261 cellXY[0] = cellX;
1262 cellXY[1] = cellY;
1263 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001264 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265 return;
1266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001268 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001269
Romain Guy73b979d2009-06-09 12:57:21 -07001270 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001271 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001273 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001274 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001275 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001276 } else {
1277 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
Romain Guycbb89e42009-06-08 15:52:54 -07001280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 /**
1282 * Add a shortcut to the workspace.
1283 *
1284 * @param data The intent describing the shortcut.
1285 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001287 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001288 int cellY) {
1289 int[] cellXY = mTmpAddItemCellCoordinates;
1290 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001291 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Michael Jurkad3ef3062010-11-23 16:23:58 -08001293 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001294
Adam Cohen558baaf2011-08-15 15:22:57 -07001295 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001296 if (info == null) {
1297 return;
1298 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001299 final View view = createShortcut(info);
1300
Adam Cohenfbba09b2011-07-18 21:43:05 -07001301 // First we check if we already know the exact location where we want to add this item.
1302 if (cellX >= 0 && cellY >= 0) {
1303 cellXY[0] = cellX;
1304 cellXY[1] = cellY;
1305 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001306
1307 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001308 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001309 true, null,null)) {
1310 return;
1311 }
1312 DragObject dragObject = new DragObject();
1313 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001314 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1315 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001316 return;
1317 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001318 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001319 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001321 foundCellSpan = (result != null);
1322 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001323 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001324 }
1325
1326 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001327 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001328 return;
1329 }
1330
Adam Cohendcd297f2013-06-18 13:13:40 -07001331 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332
1333 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001335 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337 }
1338
Adam Cohen2f093b62012-04-30 18:59:53 -07001339 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1340 int minHeight) {
1341 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001342 // We want to account for the extra amount of padding that we are adding to the widget
1343 // to ensure that it gets the full amount of space that it has requested
1344 int requiredWidth = minWidth + padding.left + padding.right;
1345 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001346 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001347 }
1348
Adam Cohen2f093b62012-04-30 18:59:53 -07001349 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1350 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001351 }
1352
Adam Cohen2f093b62012-04-30 18:59:53 -07001353 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1354 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001355 }
1356
Adam Cohen2f093b62012-04-30 18:59:53 -07001357 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1358 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001359 }
1360
Adam Cohen2f093b62012-04-30 18:59:53 -07001361 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1362 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001363 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001364 }
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001369 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001373 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1374 if (appWidgetInfo == null) {
1375 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1376 }
Romain Guycbb89e42009-06-08 15:52:54 -07001377
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001378 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001379 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001380
Adam Cohen2f093b62012-04-30 18:59:53 -07001381 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1382 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001385 // We have saved the position to which the widget was dragged-- this really only matters
1386 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 int[] cellXY = mTmpAddItemCellCoordinates;
1388 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001389 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001390 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1392 cellXY[0] = mPendingAddInfo.cellX;
1393 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001394 spanXY[0] = mPendingAddInfo.spanX;
1395 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001396 foundCellSpan = true;
1397 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001398 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1401 spanXY[1], cellXY, finalSpan);
1402 spanXY[0] = finalSpan[0];
1403 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001404 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001406 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001407 }
1408
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001410 if (appWidgetId != -1) {
1411 // Deleting an app widget ID is a void call but writes to disk before returning
1412 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001413 new Thread("deleteAppWidgetId") {
1414 public void run() {
1415 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1416 }
1417 }.start();
1418 }
Winson Chung93eef082012-03-23 15:59:27 -07001419 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001420 return;
1421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001423 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001424 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1425 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 launcherInfo.spanX = spanXY[0];
1427 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001428 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1429 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001432 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433
1434 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001435 if (hostView == null) {
1436 // Perform actual inflation because we're live
1437 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1438 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1439 } else {
1440 // The AppWidgetHostView has already been inflated and instantiated
1441 launcherInfo.hostView = hostView;
1442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001445 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001446 launcherInfo.notifyWidgetSizeChanged(this);
1447
Adam Cohendcd297f2013-06-18 13:13:40 -07001448 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001450
1451 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001453 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Adam Cohended9f8d2010-11-03 13:25:16 -07001456 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1457 @Override
1458 public void onReceive(Context context, Intent intent) {
1459 final String action = intent.getAction();
1460 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1461 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001462 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001463 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001464
Winson Chungc100e8e2011-08-09 16:02:43 -07001465 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001466 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001467 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001468 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001469 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001470 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1471 mUserPresent = true;
1472 updateRunning();
1473 }
1474 }
1475 };
1476
1477 @Override
1478 public void onAttachedToWindow() {
1479 super.onAttachedToWindow();
1480
1481 // Listen for broadcasts related to user-presence
1482 final IntentFilter filter = new IntentFilter();
1483 filter.addAction(Intent.ACTION_SCREEN_OFF);
1484 filter.addAction(Intent.ACTION_USER_PRESENT);
1485 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001486 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001487 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001488 mVisible = true;
1489 }
1490
1491 @Override
1492 public void onDetachedFromWindow() {
1493 super.onDetachedFromWindow();
1494 mVisible = false;
1495
Adam Cohend113e0c2010-11-11 10:48:05 -08001496 if (mAttached) {
1497 unregisterReceiver(mReceiver);
1498 mAttached = false;
1499 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001500 updateRunning();
1501 }
1502
1503 public void onWindowVisibilityChanged(int visibility) {
1504 mVisible = visibility == View.VISIBLE;
1505 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001506 // The following code used to be in onResume, but it turns out onResume is called when
1507 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1508 // is a more appropriate event to handle
1509 if (mVisible) {
1510 mAppsCustomizeTabHost.onWindowVisible();
1511 if (!mWorkspaceLoading) {
1512 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001513 // We want to let Launcher draw itself at least once before we force it to build
1514 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001515 // apps is nice and speedy.
1516 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001517 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001518 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001519 if (mStarted) return;
1520 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001521 // We delay the layer building a bit in order to give
1522 // other message processing a time to run. In particular
1523 // this avoids a delay in hiding the IME if it was
1524 // currently shown, because doing that may involve
1525 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001526 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001527 final ViewTreeObserver.OnDrawListener listener = this;
1528 mWorkspace.post(new Runnable() {
1529 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001530 if (mWorkspace != null &&
1531 mWorkspace.getViewTreeObserver() != null) {
1532 mWorkspace.getViewTreeObserver().
1533 removeOnDrawListener(listener);
1534 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001535 }
1536 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001537 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001538 }
1539 });
1540 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001541 // When Launcher comes back to foreground, a different Activity might be responsible for
1542 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001543 if (!DISABLE_MARKET_BUTTON) {
1544 updateAppMarketIcon();
1545 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001546 clearTypedText();
1547 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001548 }
1549
1550 private void sendAdvanceMessage(long delay) {
1551 mHandler.removeMessages(ADVANCE_MSG);
1552 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1553 mHandler.sendMessageDelayed(msg, delay);
1554 mAutoAdvanceSentTime = System.currentTimeMillis();
1555 }
1556
1557 private void updateRunning() {
1558 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1559 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1560 mAutoAdvanceRunning = autoAdvanceRunning;
1561 if (autoAdvanceRunning) {
1562 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1563 sendAdvanceMessage(delay);
1564 } else {
1565 if (!mWidgetsToAdvance.isEmpty()) {
1566 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1567 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1568 }
1569 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001570 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 }
1572 }
1573 }
1574
1575 private final Handler mHandler = new Handler() {
1576 @Override
1577 public void handleMessage(Message msg) {
1578 if (msg.what == ADVANCE_MSG) {
1579 int i = 0;
1580 for (View key: mWidgetsToAdvance.keySet()) {
1581 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1582 final int delay = mAdvanceStagger * i;
1583 if (v instanceof Advanceable) {
1584 postDelayed(new Runnable() {
1585 public void run() {
1586 ((Advanceable) v).advance();
1587 }
1588 }, delay);
1589 }
1590 i++;
1591 }
1592 sendAdvanceMessage(mAdvanceInterval);
1593 }
1594 }
1595 };
1596
1597 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001598 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001599 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1600 if (v instanceof Advanceable) {
1601 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001602 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 updateRunning();
1604 }
1605 }
1606
1607 void removeWidgetToAutoAdvance(View hostView) {
1608 if (mWidgetsToAdvance.containsKey(hostView)) {
1609 mWidgetsToAdvance.remove(hostView);
1610 updateRunning();
1611 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001612 }
1613
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 launcherInfo.hostView = null;
1617 }
1618
Winson Chung93eef082012-03-23 15:59:27 -07001619 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1620 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1621 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 }
1623
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001624 public LauncherAppWidgetHost getAppWidgetHost() {
1625 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Winson Chunga9abd0e2010-10-27 17:18:37 -07001628 public LauncherModel getModel() {
1629 return mModel;
1630 }
1631
Bjorn Bringertc459e522013-06-07 19:36:01 +01001632 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001633 getWindow().closeAllPanels();
1634
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001635 // Whatever we were doing is hereby canceled.
1636 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001637 }
1638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 @Override
1640 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001641 long startTime = 0;
1642 if (DEBUG_RESUME_TIME) {
1643 startTime = System.currentTimeMillis();
1644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 super.onNewIntent(intent);
1646
1647 // Close the menu
1648 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001649 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001650 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001651
Adam Cohened307df2013-10-02 09:37:31 -07001652 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1653 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1654 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001655
Adam Cohen6fecd412013-10-02 17:41:50 -07001656 if (mWorkspace == null) {
1657 // Can be cases where mWorkspace is null, this prevents a NPE
1658 return;
1659 }
1660 Folder openFolder = mWorkspace.getOpenFolder();
1661 // In all these cases, only animate if we're already on home
1662 mWorkspace.exitWidgetResizeMode();
1663 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1664 openFolder == null) {
1665 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001666 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001667
Adam Cohen6fecd412013-10-02 17:41:50 -07001668 closeFolder();
1669 exitSpringLoadedDragMode();
1670
1671 // If we are already on home, then just animate back to the workspace,
1672 // otherwise, just wait until onResume to set the state back to Workspace
1673 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001674 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001675 } else {
1676 mOnResumeState = State.WORKSPACE;
1677 }
1678
1679 final View v = getWindow().peekDecorView();
1680 if (v != null && v.getWindowToken() != null) {
1681 InputMethodManager imm = (InputMethodManager)getSystemService(
1682 INPUT_METHOD_SERVICE);
1683 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1684 }
1685
1686 // Reset the apps customize page
1687 if (mAppsCustomizeTabHost != null) {
1688 mAppsCustomizeTabHost.reset();
1689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
Adam Cohened307df2013-10-02 09:37:31 -07001691
Michael Jurka447bf842013-05-15 14:52:15 +02001692 if (DEBUG_RESUME_TIME) {
1693 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
1697 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001698 public void onRestoreInstanceState(Bundle state) {
1699 super.onRestoreInstanceState(state);
1700 for (int page: mSynchronouslyBoundPages) {
1701 mWorkspace.restoreInstanceStateForChild(page);
1702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 }
1704
1705 @Override
1706 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001707 if (mWorkspace.getChildCount() > 0) {
1708 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1709 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001710 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711
Michael Jurka883f55b2010-10-21 15:47:14 -07001712 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001713 // We close any open folder since it will not be re-opened, and we need to make sure
1714 // this state is reflected.
1715 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001718 mWaitingForResult) {
1719 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001721 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1722 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001723 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1724 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1725 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727
1728 if (mFolderInfo != null && mWaitingForResult) {
1729 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1730 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1731 }
Michael Jurka946ad472010-07-09 18:05:18 -07001732
Winson Chung785d2eb2011-04-14 16:08:02 -07001733 // Save the current AppsCustomize tab
1734 if (mAppsCustomizeTabHost != null) {
1735 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1736 if (currentTabTag != null) {
1737 outState.putString("apps_customize_currentTab", currentTabTag);
1738 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001739 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1740 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 }
1743
1744 @Override
1745 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001747
Winson Chunge7a03942011-08-05 15:05:12 -07001748 // Remove all pending runnables
1749 mHandler.removeMessages(ADVANCE_MSG);
1750 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001751 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001752
Winson Chungcd2b0142011-06-08 16:02:26 -07001753 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001754 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001755 mModel.stopLoader();
1756 app.setLauncher(null);
1757
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001759 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001761 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001763 mAppWidgetHost = null;
1764
1765 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766
1767 TextKeyListener.getInstance().release();
1768
Winson Chung81b52252012-08-27 15:34:29 -07001769 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1770 // to prevent leaking Launcher activities on orientation change.
1771 if (mModel != null) {
1772 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1773 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001774
1775 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001776 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001777
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001778 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001779 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1780 mWorkspace.removeAllViews();
1781 mWorkspace = null;
1782 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001783
Michael Jurka2ecf9952012-06-18 12:52:28 -07001784 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786
Adam Cohena9cf38f2011-05-02 15:36:58 -07001787 public DragController getDragController() {
1788 return mDragController;
1789 }
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 @Override
1792 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001793 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 super.startActivityForResult(intent, requestCode);
1795 }
1796
Winson Chung70d72102011-08-12 11:24:35 -07001797 /**
1798 * Indicates that we want global search for this activity by setting the globalSearch
1799 * argument for {@link #startSearch} to true.
1800 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001802 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001804
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001805 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Karl Rosaen138a0412009-04-23 19:00:21 -07001807 if (initialQuery == null) {
1808 // Use any text typed in the launcher as the initial query
1809 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 if (appSearchData == null) {
1812 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001813 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Winson Chungadf0c182012-08-23 14:59:07 -07001815 Rect sourceBounds = new Rect();
1816 if (mSearchDropTargetBar != null) {
1817 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1818 }
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Bjorn Bringertc459e522013-06-07 19:36:01 +01001820 startSearch(initialQuery, selectInitialQuery,
1821 appSearchData, sourceBounds);
1822 }
1823
1824 public void startSearch(String initialQuery,
1825 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001826 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001827 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001828 }
1829
1830 /**
1831 * Starts the global search activity. This code is a copied from SearchManager
1832 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001833 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001834 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001835 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001836 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1837 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1838 if (globalSearchActivity == null) {
1839 Log.w(TAG, "No global search activity found.");
1840 return;
1841 }
1842 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1843 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1844 intent.setComponent(globalSearchActivity);
1845 // Make sure that we have a Bundle to put source in
1846 if (appSearchData == null) {
1847 appSearchData = new Bundle();
1848 } else {
1849 appSearchData = new Bundle(appSearchData);
1850 }
1851 // Set source to package name of app that starts global search, if not set already.
1852 if (!appSearchData.containsKey("source")) {
1853 appSearchData.putString("source", getPackageName());
1854 }
1855 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1856 if (!TextUtils.isEmpty(initialQuery)) {
1857 intent.putExtra(SearchManager.QUERY, initialQuery);
1858 }
1859 if (selectInitialQuery) {
1860 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1861 }
1862 intent.setSourceBounds(sourceBounds);
1863 try {
1864 startActivity(intent);
1865 } catch (ActivityNotFoundException ex) {
1866 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
1869
Winson Chung70d72102011-08-12 11:24:35 -07001870 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001871 public boolean onPrepareOptionsMenu(Menu menu) {
1872 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001873 if (!mWorkspace.isInOverviewMode()) {
1874 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001875 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001876 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001879 @Override
1880 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001881 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001882 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001883 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001884 }
1885
Joe Onorato9c1289c2009-08-17 11:03:03 -04001886 public boolean isWorkspaceLocked() {
1887 return mWorkspaceLoading || mWaitingForResult;
1888 }
1889
Michael Jurka0280c3b2010-09-17 15:00:07 -07001890 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001891 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001892 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1894 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001895 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001897 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001898
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001899 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1900 AppWidgetProviderInfo appWidgetInfo) {
1901 if (appWidgetInfo.configure != null) {
1902 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001903
Bjorn Bringert7984c942009-12-09 15:38:25 +00001904 // Launch over to configure widget, if needed
1905 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001906 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001907 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001908 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001910 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001912 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001913 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001914 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916 }
Romain Guycbb89e42009-06-08 15:52:54 -07001917
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001918 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001919 // Close any folders that may be open.
1920 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001921 mWorkspace.moveToCustomContentScreen(animate);
1922 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001923 /**
1924 * Process a shortcut drop.
1925 *
1926 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001928 * @param cell The cell it should be added to, optional
1929 * @param position The location on the screen where it was dropped, optional
1930 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001933 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001937
1938 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 mPendingAddInfo.cellX = cell[0];
1940 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001941 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001942
1943 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1944 createShortcutIntent.setComponent(componentName);
1945 processShortcut(createShortcutIntent);
1946 }
1947
Adam Cohenfbba09b2011-07-18 21:43:05 -07001948 /**
1949 * Process a widget drop.
1950 *
1951 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001953 * @param cell The cell it should be added to, optional
1954 * @param position The location on the screen where it was dropped, optional
1955 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001957 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001958 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001959 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001961 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001962 mPendingAddInfo.minSpanX = info.minSpanX;
1963 mPendingAddInfo.minSpanY = info.minSpanY;
1964
Adam Cohenfbba09b2011-07-18 21:43:05 -07001965 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 mPendingAddInfo.cellX = cell[0];
1967 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001968 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001969 if (span != null) {
1970 mPendingAddInfo.spanX = span[0];
1971 mPendingAddInfo.spanY = span[1];
1972 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001973
Adam Cohened66b2b2012-01-23 17:28:51 -08001974 AppWidgetHostView hostView = info.boundWidget;
1975 int appWidgetId;
1976 if (hostView != null) {
1977 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001978 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001979 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001980 // In this case, we either need to start an activity to get permission to bind
1981 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001982 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001983 Bundle options = info.bindOptions;
1984
1985 boolean success = false;
1986 if (options != null) {
1987 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1988 info.componentName, options);
1989 } else {
1990 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1991 info.componentName);
1992 }
1993 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001994 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001995 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001996 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001997 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1998 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1999 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002000 // TODO: we need to make sure that this accounts for the options bundle.
2001 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002002 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2003 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002004 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002005 }
2006
Joe Onoratodeb98af2010-02-19 14:59:39 -08002007 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002008 // Handle case where user selected "Applications"
2009 String applicationName = getResources().getString(R.string.group_applications);
2010 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002011
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002012 if (applicationName != null && applicationName.equals(shortcutName)) {
2013 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2014 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2017 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002018 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002019 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002021 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
2024
Winson Chung24ab2f12010-09-16 14:10:47 -07002025 void processWallpaper(Intent intent) {
2026 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2027 }
2028
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002031 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 folderInfo.title = getText(R.string.folder_name);
2033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002035 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002036 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002037 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038
2039 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002040 FolderIcon newFolder =
2041 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002042 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002043 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002044 // Force measure the new folder icon
2045 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2046 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002047 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 }
Romain Guycbb89e42009-06-08 15:52:54 -07002049
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002051 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 }
2053
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002054 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002056 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002057 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
2059
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002060 protected ComponentName getWallpaperPickerComponent() {
2061 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2062 }
2063
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002064 /**
2065 * Registers various content observers. The current implementation registers
2066 * only a favorites observer to keep track of the favorites applications.
2067 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002068 private void registerContentObservers() {
2069 ContentResolver resolver = getContentResolver();
2070 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2071 true, mWidgetObserver);
2072 }
2073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 @Override
2075 public boolean dispatchKeyEvent(KeyEvent event) {
2076 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2077 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002079 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002080 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002081 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002082 dumpState();
2083 return true;
2084 }
2085 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002086 }
2087 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2088 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002089 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 return true;
2091 }
2092 }
2093
2094 return super.dispatchKeyEvent(event);
2095 }
2096
Joe Onorato88ec0992009-11-19 13:16:06 -08002097 @Override
2098 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002099 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002100 if (mAppsCustomizeContent.getContentType() ==
2101 AppsCustomizePagedView.ContentType.Applications) {
2102 showWorkspace(true);
2103 } else {
2104 showOverviewMode(true);
2105 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002106 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002107 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002108 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002109 Folder openFolder = mWorkspace.getOpenFolder();
2110 if (openFolder.isEditingName()) {
2111 openFolder.dismissEditingName();
2112 } else {
2113 closeFolder();
2114 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002115 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002116 mWorkspace.exitWidgetResizeMode();
2117
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002118 // Back button is a no-op here, but give at least some feedback for the button press
2119 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002120 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002121 }
2122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002124 * Re-listen when widgets are reset.
2125 */
2126 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002127 if (mAppWidgetHost != null) {
2128 mAppWidgetHost.startListening();
2129 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002130 }
2131
2132 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 * Launches the intent referred by the clicked shortcut.
2134 *
2135 * @param v The view representing the clicked shortcut.
2136 */
2137 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002138 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2139 // view has detached (it's possible for this to happen if the view is removed mid touch).
2140 if (v.getWindowToken() == null) {
2141 return;
2142 }
2143
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002144 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002145 return;
2146 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002147
Adam Cohen1697b792013-09-17 19:08:21 -07002148 if (v instanceof Workspace) {
2149 if (mWorkspace.isInOverviewMode()) {
2150 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002151 }
2152 return;
2153 }
2154
2155 if (v instanceof CellLayout) {
2156 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002157 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002158 }
2159 }
2160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002162 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002164 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2165 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002166
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002167 // Check for special shortcuts
2168 if (intent.getComponent() != null) {
2169 final String shortcutClass = intent.getComponent().getClassName();
2170
2171 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002172 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002173 return;
2174 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2175 MemoryDumpActivity.startDump(this);
2176 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002177 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2178 toggleShowWeightWatcher();
2179 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002180 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002181 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002182
2183 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002184 int[] pos = new int[2];
2185 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002186 intent.setSourceBounds(new Rect(pos[0], pos[1],
2187 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002188
2189 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002190
Daniel Sandlerff02d492013-08-05 02:12:05 -04002191 mStats.recordLaunch(intent, shortcut);
2192
Michael Jurkaddd62e92011-02-16 17:49:14 -08002193 if (success && v instanceof BubbleTextView) {
2194 mWaitingForResume = (BubbleTextView) v;
2195 mWaitingForResume.setStayPressed(true);
2196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002198 if (v instanceof FolderIcon) {
2199 FolderIcon fi = (FolderIcon) v;
2200 handleFolderClick(fi);
2201 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002202 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002203 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002204 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002205 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002206 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 }
2209 }
2210
Michael Jurka0e260592010-06-30 17:07:39 -07002211 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002212 return false;
2213 }
2214
Michael Jurkaaf442092010-06-10 17:01:57 -07002215 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002216 * Event handler for the search button
2217 *
2218 * @param v The view that was clicked.
2219 */
2220 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002221 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2222
Amith Yamasania135ba82011-08-09 17:42:01 -07002223 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002224 }
2225
2226 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002227 * Event handler for the voice button
2228 *
2229 * @param v The view that was clicked.
2230 */
2231 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002232 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002233
Bjorn Bringertc459e522013-06-07 19:36:01 +01002234 startVoice();
2235 }
2236
2237 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002238 try {
2239 final SearchManager searchManager =
2240 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2241 ComponentName activityName = searchManager.getGlobalSearchActivity();
2242 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002243 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002244 if (activityName != null) {
2245 intent.setPackage(activityName.getPackageName());
2246 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002247 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002248 } catch (ActivityNotFoundException e) {
2249 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002250 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002251 startActivitySafely(null, intent, "onClickVoiceButton");
2252 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002253 }
2254
2255 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002256 * Event handler for the "grid" button that appears on the home screen, which
2257 * enters all apps mode.
2258 *
2259 * @param v The view that was clicked.
2260 */
2261 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002262 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002263 }
2264
2265 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002266 // Provide the same haptic feedback that the system offers for virtual keys.
2267 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002268 }
2269
Adam Cohen61f560d2013-09-30 15:58:20 -07002270 public void performHapticFeedbackOnTouchDown(View v) {
2271 // Provide the same haptic feedback that the system offers for virtual keys.
2272 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2273 }
2274
2275 public View.OnTouchListener getHapticFeedbackTouchListener() {
2276 if (mHapticFeedbackTouchListener == null) {
2277 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2278 @Override
2279 public boolean onTouch(View v, MotionEvent event) {
2280 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2281 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2282 }
2283 return false;
2284 }
2285 };
2286 }
2287 return mHapticFeedbackTouchListener;
2288 }
2289
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002290 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002291 if (!DISABLE_MARKET_BUTTON) {
2292 if (mAppMarketIntent != null) {
2293 startActivitySafely(v, mAppMarketIntent, "app market");
2294 } else {
2295 Log.e(TAG, "Invalid app market intent.");
2296 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002297 }
2298 }
2299
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002300 /**
2301 * Called when the user stops interacting with the launcher.
2302 * This implies that the user is now on the homescreen and is not doing housekeeping.
2303 */
2304 protected void onInteractionEnd() {}
2305
2306 /**
2307 * Called when the user starts interacting with the launcher.
2308 * The possible interactions are:
2309 * - open all apps
2310 * - reorder an app shortcut, or a widget
2311 * - open the overview mode.
2312 * This is a good time to stop doing things that only make sense
2313 * when the user is on the homescreen and not doing housekeeping.
2314 */
2315 protected void onInteractionBegin() {}
2316
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002317 void startApplicationDetailsActivity(ComponentName componentName) {
2318 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002319 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2320 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002321 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002322 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002323 }
2324
Michael Jurka1e2f4652013-07-08 18:03:46 -07002325 // returns true if the activity was started
2326 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002327 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002328 // System applications cannot be installed. For now, show a toast explaining that.
2329 // We may give them the option of disabling apps this way.
2330 int messageId = R.string.uninstall_system_app_text;
2331 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002332 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002333 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002334 String packageName = componentName.getPackageName();
2335 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002336 Intent intent = new Intent(
2337 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002338 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2339 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002340 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002341 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002342 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002343 }
2344
Michael Jurka86a720e2012-05-09 11:23:23 -07002345 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002349 // Only launch using the new animation if the shortcut has not opted out (this is a
2350 // private contract between launcher and may be ignored in the future).
2351 boolean useLaunchAnimation = (v != null) &&
2352 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2353 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002354 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2355 v.getMeasuredWidth(), v.getMeasuredHeight());
2356
2357 startActivity(intent, opts.toBundle());
2358 } else {
2359 startActivity(intent);
2360 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002361 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 } catch (SecurityException e) {
2363 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002364 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002366 "or use the exported attribute for this activity. "
2367 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002369 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002371
Michael Jurka86a720e2012-05-09 11:23:23 -07002372 boolean startActivitySafely(View v, Intent intent, Object tag) {
2373 boolean success = false;
2374 try {
2375 success = startActivity(v, intent, tag);
2376 } catch (ActivityNotFoundException e) {
2377 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2378 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2379 }
2380 return success;
2381 }
2382
Adam Cohena9cf38f2011-05-02 15:36:58 -07002383 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002384 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002385 Folder openFolder = mWorkspace.getFolderForTag(info);
2386
2387 // If the folder info reports that the associated folder is open, then verify that
2388 // it is actually opened. There have been a few instances where this gets out of sync.
2389 if (info.opened && openFolder == null) {
2390 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002391 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002392 info.opened = false;
2393 }
2394
Adam Cohenfb91f302012-06-11 15:45:18 -07002395 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 // Close any open folder
2397 closeFolder();
2398 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002399 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 } else {
2401 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 int folderScreen;
2403 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002404 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // .. and close it
2406 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002407 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 // Close any folder open on the current screen
2409 closeFolder();
2410 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002411 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 }
2413 }
2414 }
2415 }
2416
Adam Cohen268c4752012-06-06 17:47:33 -07002417 /**
2418 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2419 * in the DragLayer in the exact absolute location of the original FolderIcon.
2420 */
2421 private void copyFolderIconToImage(FolderIcon fi) {
2422 final int width = fi.getMeasuredWidth();
2423 final int height = fi.getMeasuredHeight();
2424
2425 // Lazy load ImageView, Bitmap and Canvas
2426 if (mFolderIconImageView == null) {
2427 mFolderIconImageView = new ImageView(this);
2428 }
2429 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2430 mFolderIconBitmap.getHeight() != height) {
2431 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2432 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2433 }
2434
2435 DragLayer.LayoutParams lp;
2436 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2437 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2438 } else {
2439 lp = new DragLayer.LayoutParams(width, height);
2440 }
2441
Adam Cohen307fe232012-08-16 17:55:58 -07002442 // The layout from which the folder is being opened may be scaled, adjust the starting
2443 // view size by this scale factor.
2444 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002445 lp.customPosition = true;
2446 lp.x = mRectForFolderAnimation.left;
2447 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002448 lp.width = (int) (scale * width);
2449 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002450
2451 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2452 fi.draw(mFolderIconCanvas);
2453 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002454 if (fi.getFolder() != null) {
2455 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2456 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002457 }
Adam Cohen268c4752012-06-06 17:47:33 -07002458 // Just in case this image view is still in the drag layer from a previous animation,
2459 // we remove it and re-add it.
2460 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2461 mDragLayer.removeView(mFolderIconImageView);
2462 }
2463 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002464 if (fi.getFolder() != null) {
2465 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002466 }
Adam Cohen268c4752012-06-06 17:47:33 -07002467 }
2468
Adam Cohen2801caf2011-05-13 20:57:39 -07002469 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002470 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002471 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2472 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2473 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2474
Adam Cohenc51934b2011-07-26 21:07:43 -07002475 FolderInfo info = (FolderInfo) fi.getTag();
2476 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2477 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002478 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2479 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002480 }
2481
Adam Cohen268c4752012-06-06 17:47:33 -07002482 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2483 copyFolderIconToImage(fi);
2484 fi.setVisibility(View.INVISIBLE);
2485
Michael Jurka2ecf9952012-06-18 12:52:28 -07002486 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002487 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002488 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2489 oa.start();
2490 }
2491
Adam Cohen268c4752012-06-06 17:47:33 -07002492 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002493 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002494 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2495 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2496 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2497
Adam Cohen268c4752012-06-06 17:47:33 -07002498 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002499
Adam Cohen268c4752012-06-06 17:47:33 -07002500 // We remove and re-draw the FolderIcon in-case it has changed
2501 mDragLayer.removeView(mFolderIconImageView);
2502 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002503 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002504 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002505 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002506 oa.addListener(new AnimatorListenerAdapter() {
2507 @Override
2508 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002509 if (cl != null) {
2510 cl.clearFolderLeaveBehind();
2511 // Remove the ImageView copy of the FolderIcon and make the original visible.
2512 mDragLayer.removeView(mFolderIconImageView);
2513 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002514 }
2515 }
2516 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002517 oa.start();
2518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002521 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 * is animated relative to the specified View. If the View is null, no animation
2523 * is played.
2524 *
2525 * @param folderInfo The FolderInfo describing the folder to open.
2526 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002528 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530
Adam Cohena9cf38f2011-05-02 15:36:58 -07002531 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532
Adam Cohen4554ee12011-08-03 16:13:21 -07002533 // Just verify that the folder hasn't already been added to the DragLayer.
2534 // There was a one-off crash where the folder had a parent already.
2535 if (folder.getParent() == null) {
2536 mDragLayer.addView(folder);
2537 mDragController.addDropTarget((DropTarget) folder);
2538 } else {
2539 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2540 folder.getParent() + ").");
2541 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002543 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002544
2545 // Notify the accessibility manager that this folder "window" has appeared and occluded
2546 // the workspace items
2547 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2548 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002549 }
2550
2551 public void closeFolder() {
2552 Folder folder = mWorkspace.getOpenFolder();
2553 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002554 if (folder.isEditingName()) {
2555 folder.dismissEditingName();
2556 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002557 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002558
2559 // Dismiss the folder cling
2560 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002561 }
2562 }
2563
2564 void closeFolder(Folder folder) {
2565 folder.getInfo().opened = false;
2566
2567 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2568 if (parent != null) {
2569 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2570 shrinkAndFadeInFolderIcon(fi);
2571 }
2572 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002573
2574 // Notify the accessibility manager that this folder "window" has disappeard and no
2575 // longer occludeds the workspace items
2576 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 }
2578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002580 if (!isDraggingEnabled()) return false;
2581 if (isWorkspaceLocked()) return false;
2582 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583
Adam Cohen1697b792013-09-17 19:08:21 -07002584 if (v instanceof Workspace) {
2585 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002586 if (mWorkspace.enterOverviewMode()) {
2587 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2588 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2589 return true;
2590 } else {
2591 return false;
2592 }
Adam Cohen1697b792013-09-17 19:08:21 -07002593 }
Adam Cohen1697b792013-09-17 19:08:21 -07002594 }
2595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002597 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 }
2599
Michael Jurka0280c3b2010-09-17 15:00:07 -07002600 resetAddInfo();
2601 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002603 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 return true;
2605 }
2606
Winson Chung3d503fb2011-07-13 17:25:49 -07002607 // The hotseat touch handling does not go through Workspace, and we always allow long press
2608 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002609 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002610 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2611 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002612 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002613 // User long pressed on empty space
2614 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2615 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2616 // Disabling reordering until we sort out some issues.
2617 if (mWorkspace.isInOverviewMode()) {
2618 mWorkspace.startReordering(v);
2619 } else {
2620 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002623 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002625 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 }
2627 }
2628 }
2629 return true;
2630 }
2631
Winson Chung3d503fb2011-07-13 17:25:49 -07002632 boolean isHotseatLayout(View layout) {
2633 return mHotseat != null && layout != null &&
2634 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2635 }
2636 Hotseat getHotseat() {
2637 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002638 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002639 View getOverviewPanel() {
2640 return mOverviewPanel;
2641 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002642 SearchDropTargetBar getSearchBar() {
2643 return mSearchDropTargetBar;
2644 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002645
Winson Chung3d503fb2011-07-13 17:25:49 -07002646 /**
2647 * Returns the CellLayout of the specified container at the specified screen.
2648 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002649 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002650 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2651 if (mHotseat != null) {
2652 return mHotseat.getLayout();
2653 } else {
2654 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002655 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002656 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002657 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002658 }
2659 }
2660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 Workspace getWorkspace() {
2662 return mWorkspace;
2663 }
2664
Daniel Sandler843e8602010-06-07 14:59:01 -04002665 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002666 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002667 }
2668
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 /**
2670 * Helper method for the cameraZoomIn/cameraZoomOut animations
2671 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 * @param scaleFactor The scale factor used for the zoom
2673 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002674 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002676 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002678
Craig Mautner360310b2012-10-26 15:13:08 -07002679 private void setWorkspaceBackground(boolean workspace) {
2680 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002681 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002682 }
2683
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002684 void updateWallpaperVisibility(boolean visible) {
2685 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2686 int curflags = getWindow().getAttributes().flags
2687 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2688 if (wpflags != curflags) {
2689 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2690 }
Craig Mautner360310b2012-10-26 15:13:08 -07002691 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002692 }
2693
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002694 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2695 if (v instanceof LauncherTransitionable) {
2696 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2697 }
2698 }
2699
Michael Jurkabed61d22012-02-14 22:51:29 -08002700 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2701 if (v instanceof LauncherTransitionable) {
2702 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2703 }
Winson Chung70442722012-02-10 15:43:22 -08002704
2705 // Update the workspace transition step as well
2706 dispatchOnLauncherTransitionStep(v, 0f);
2707 }
2708
2709 private void dispatchOnLauncherTransitionStep(View v, float t) {
2710 if (v instanceof LauncherTransitionable) {
2711 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2712 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002713 }
2714
2715 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2716 if (v instanceof LauncherTransitionable) {
2717 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2718 }
Winson Chung70442722012-02-10 15:43:22 -08002719
2720 // Update the workspace transition step as well
2721 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002722 }
2723
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002724 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002725 * Things to test when changing the following seven functions.
2726 * - Home from workspace
2727 * - from center screen
2728 * - from other screens
2729 * - Home from all apps
2730 * - from center screen
2731 * - from other screens
2732 * - Back from all apps
2733 * - from center screen
2734 * - from other screens
2735 * - Launch app from workspace and quit
2736 * - with back
2737 * - with home
2738 * - Launch app from all apps and quit
2739 * - with back
2740 * - with home
2741 * - Go to a screen that's not the default, then all
2742 * apps, and launch and app, and go back
2743 * - with back
2744 * -with home
2745 * - On workspace, long press power and go back
2746 * - with back
2747 * - with home
2748 * - On all apps, long press power and go back
2749 * - with back
2750 * - with home
2751 * - On workspace, power off
2752 * - On all apps, power off
2753 * - Launch an app and turn off the screen while in that app
2754 * - Go back with home key
2755 * - Go back with back key TODO: make this not go to workspace
2756 * - From all apps
2757 * - From workspace
2758 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2759 * - From all apps
2760 * - From the center workspace
2761 * - From another workspace
2762 */
2763
2764 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 * Zoom the camera out from the workspace to reveal 'toView'.
2766 * Assumes that the view to show is anchored at either the very top or very bottom
2767 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002769 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002770 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2771 showAppsCustomizeHelper(animated, springLoaded, contentType);
2772 }
2773 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2774 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 mStateAnimation.cancel();
2778 mStateAnimation = null;
2779 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002781
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2783 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2784 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002785 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002786 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002787 final int startDelay =
2788 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002789
Michael Jurkab3e22d92011-10-31 15:58:33 -07002790 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002791
Michael Jurkad74c9842011-07-10 12:44:21 -07002792 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002793 Animator workspaceAnim =
2794 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002795 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2796 // Set the content type for the all apps space
2797 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2798 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799
2800 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002801 toView.setScaleX(scale);
2802 toView.setScaleY(scale);
2803 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2804 scaleAnim.
2805 scaleX(1f).scaleY(1f).
2806 setDuration(duration).
2807 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002808
Winson Chungf0ea4d32011-06-06 14:27:16 -07002809 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002810 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002811 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002812 .ofFloat(toView, "alpha", 0f, 1f)
2813 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002814 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002815 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2816 @Override
2817 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002818 if (animation == null) {
2819 throw new RuntimeException("animation is null");
2820 }
Winson Chung70442722012-02-10 15:43:22 -08002821 float t = (Float) animation.getAnimatedValue();
2822 dispatchOnLauncherTransitionStep(fromView, t);
2823 dispatchOnLauncherTransitionStep(toView, t);
2824 }
2825 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002826
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827 // toView should appear right at the end of the workspace shrink
2828 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002829 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002830 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002831 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002832
2833 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002834 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002835 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 // Prepare the position
2837 toView.setTranslationX(0.0f);
2838 toView.setTranslationY(0.0f);
2839 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002840 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002842 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002843 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002844 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2845 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002846
Winson Chungc7d2b602012-05-16 17:02:20 -07002847 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002848 if (mSearchDropTargetBar != null) {
2849 mSearchDropTargetBar.hideSearchBar(false);
2850 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002851 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 });
2853
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002854 if (workspaceAnim != null) {
2855 mStateAnimation.play(workspaceAnim);
2856 }
Michael Jurka1899a362011-11-03 13:50:45 -07002857
2858 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002859
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002860 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2861 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002862
2863 // If any of the objects being animated haven't been measured/laid out
2864 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002865 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002866 (mWorkspace.getMeasuredWidth() == 0) ||
2867 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002868 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002869 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002870
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002871 final AnimatorSet stateAnimation = mStateAnimation;
2872 final Runnable startAnimRunnable = new Runnable() {
2873 public void run() {
2874 // Check that mStateAnimation hasn't changed while
2875 // we waited for a layout/draw pass
2876 if (mStateAnimation != stateAnimation)
2877 return;
2878 setPivotsForZoom(toView, scale);
2879 dispatchOnLauncherTransitionStart(fromView, animated, false);
2880 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002881 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002882 }
2883 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002884 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002885 final ViewTreeObserver observer = toView.getViewTreeObserver();
2886 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2887 public void onGlobalLayout() {
2888 startAnimRunnable.run();
2889 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2890 }
2891 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002892 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002893 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002894 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 } else {
2896 toView.setTranslationX(0.0f);
2897 toView.setTranslationY(0.0f);
2898 toView.setScaleX(1.0f);
2899 toView.setScaleY(1.0f);
2900 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002901 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002902
Daniel Sandlere4f98912013-06-25 15:13:26 -04002903 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002904 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002905 if (mSearchDropTargetBar != null) {
2906 mSearchDropTargetBar.hideSearchBar(false);
2907 }
Michael Jurkaabded662011-03-04 12:06:57 -08002908 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002909 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 dispatchOnLauncherTransitionStart(fromView, animated, false);
2911 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002912 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002913 dispatchOnLauncherTransitionStart(toView, animated, false);
2914 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002915 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002916 }
2917
2918 /**
2919 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002920 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002921 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002922 */
Adam Cohened307df2013-10-02 09:37:31 -07002923 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002924 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002925
Michael Jurkab3e22d92011-10-31 15:58:33 -07002926 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002927 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002928 mStateAnimation.cancel();
2929 mStateAnimation = null;
2930 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002931 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002932
Winson Chungf0ea4d32011-06-06 14:27:16 -07002933 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002934 final int fadeOutDuration =
2935 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002936 final float scaleFactor = (float)
2937 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2938 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002939 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002940 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002941 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002942 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2943 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002944 toState, animated, stagger, -1);
2945 } else if (toState == Workspace.State.SPRING_LOADED ||
2946 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002947 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002948 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002949 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002950
Michael Jurkab3e22d92011-10-31 15:58:33 -07002951 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002952 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002953 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002954 final LauncherViewPropertyAnimator scaleAnim =
2955 new LauncherViewPropertyAnimator(fromView);
2956 scaleAnim.
2957 scaleX(scaleFactor).scaleY(scaleFactor).
2958 setDuration(duration).
2959 setInterpolator(new Workspace.ZoomInInterpolator());
2960
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002961 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002962 .ofFloat(fromView, "alpha", 1f, 0f)
2963 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002964 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002965 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2966 @Override
2967 public void onAnimationUpdate(ValueAnimator animation) {
2968 float t = 1f - (Float) animation.getAnimatedValue();
2969 dispatchOnLauncherTransitionStep(fromView, t);
2970 dispatchOnLauncherTransitionStep(toView, t);
2971 }
2972 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002973
Michael Jurka2ecf9952012-06-18 12:52:28 -07002974 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002975
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002976 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2977 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002978 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002979
2980 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002981 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002982 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002983 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002984 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2985 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002986 if (onCompleteRunnable != null) {
2987 onCompleteRunnable.run();
2988 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002989 mAppsCustomizeContent.updateCurrentPageScroll();
2990 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002991 }
2992 });
2993
Winson Chungf0ea4d32011-06-06 14:27:16 -07002994 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002995 if (workspaceAnim != null) {
2996 mStateAnimation.play(workspaceAnim);
2997 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002998 dispatchOnLauncherTransitionStart(fromView, animated, true);
2999 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003000 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003001 } else {
3002 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003003 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003004 dispatchOnLauncherTransitionStart(fromView, animated, true);
3005 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003006 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003007 dispatchOnLauncherTransitionStart(toView, animated, true);
3008 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003009 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003010 }
3011
Michael Jurkae326f182011-11-21 14:05:46 -08003012 @Override
3013 public void onTrimMemory(int level) {
3014 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003015 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003016 mAppsCustomizeTabHost.onTrimMemory();
3017 }
3018 }
3019
Adam Cohened307df2013-10-02 09:37:31 -07003020 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003021 showWorkspace(animated, null);
3022 }
3023
Adam Cohened307df2013-10-02 09:37:31 -07003024 protected void showWorkspace() {
3025 showWorkspace(true);
3026 }
3027
Adam Cohened66b2b2012-01-23 17:28:51 -08003028 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003029 if (mWorkspace.isInOverviewMode()) {
3030 mWorkspace.exitOverviewMode(animated);
3031 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003032 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003033 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003034 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003035 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036
Winson Chungc7d2b602012-05-16 17:02:20 -07003037 // Show the search bar (only animate if we were showing the drop target bar in spring
3038 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003039 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003040 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003041 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003042
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 // Set focus to the AppsCustomize button
3044 if (mAllAppsButton != null) {
3045 mAllAppsButton.requestFocus();
3046 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003047 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003048
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003049 // Change the state *after* we've called all the transition code
3050 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003051
Winson Chung5fb63472011-02-02 17:03:37 -08003052 // Resume the auto-advance of widgets
3053 mUserPresent = true;
3054 updateRunning();
3055
alanv1d4fde62012-10-17 13:15:47 -07003056 // Send an accessibility event to announce the context change
3057 getWindow().getDecorView()
3058 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003059
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003060 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003061 }
3062
Adam Cohened307df2013-10-02 09:37:31 -07003063 void showOverviewMode(boolean animated) {
3064 mWorkspace.setVisibility(View.VISIBLE);
3065 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3066 mState = State.WORKSPACE;
3067 onWorkspaceShown(animated);
3068 }
3069
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003070 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003071 }
3072
Winson Chung82963d52013-10-09 11:20:57 -07003073 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3074 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003075 if (mState != State.WORKSPACE) return;
3076
Winson Chung82963d52013-10-09 11:20:57 -07003077 if (resetPageToZero) {
3078 mAppsCustomizeTabHost.reset();
3079 }
Winson Chungc58497e2013-09-03 17:48:37 -07003080 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003081 mAppsCustomizeTabHost.requestFocus();
3082
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 // Change the state *after* we've called all the transition code
3084 mState = State.APPS_CUSTOMIZE;
3085
3086 // Pause the auto-advance of widgets until we are out of AllApps
3087 mUserPresent = false;
3088 updateRunning();
3089 closeFolder();
3090
3091 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003092 getWindow().getDecorView()
3093 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003094 }
3095
Adam Cohen7777d962011-08-18 18:58:38 -07003096 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003097 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003098 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003099 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003100 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003101 }
Adam Cohen7777d962011-08-18 18:58:38 -07003102
Adam Cohened66b2b2012-01-23 17:28:51 -08003103 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3104 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003105 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3106
Winson Chunge7a03942011-08-05 15:05:12 -07003107 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003108 @Override
3109 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003110 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003111 // Before we show workspace, hide all apps again because
3112 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3113 // clean up our state transition functions
3114 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003115 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003116 } else {
3117 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003118 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003119 }
3120 }, (extendedDelay ?
3121 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3122 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3123 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003124
Michael Jurkad3ef3062010-11-23 16:23:58 -08003125 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003126 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003127 final boolean animated = true;
3128 final boolean springLoaded = true;
3129 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003130 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 }
3132 // Otherwise, we are not in spring loaded mode, so don't do anything.
3133 }
3134
Michael Jurkab3e22d92011-10-31 15:58:33 -07003135 void lockAllApps() {
3136 // TODO
3137 }
3138
3139 void unlockAllApps() {
3140 // TODO
3141 }
3142
Winson Chungf0ea4d32011-06-06 14:27:16 -07003143 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003145 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003146 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003147 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003148 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003149 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003150 int duration = 0;
3151 if (mSearchDropTargetBar != null) {
3152 duration = mSearchDropTargetBar.getTransitionInDuration();
3153 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003154 mHotseat.animate().alpha(1f).setDuration(duration);
3155 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003156 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003157 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003158 }
3159 }
3160 }
3161
3162 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003163 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003166 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003167 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003168 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003169 int duration = 0;
3170 if (mSearchDropTargetBar != null) {
3171 duration = mSearchDropTargetBar.getTransitionOutDuration();
3172 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003173 mHotseat.animate().alpha(0f).setDuration(duration);
3174 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003175 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003176 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003177 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003178 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003179 }
3180
Patrick Dubroy5f445422011-02-18 14:35:21 -08003181 /**
3182 * Add an item from all apps or customize onto the given workspace screen.
3183 * If layout is null, add to the current screen.
3184 */
3185 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003186 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003187 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003188 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003189 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003190
Winson Chungdff8ebb2011-09-08 17:25:31 -07003191 /** Maps the current orientation to an index for referencing orientation correct global icons */
3192 private int getCurrentOrientationIndexForGlobalIcons() {
3193 // default - 0, landscape - 1
3194 switch (getResources().getConfiguration().orientation) {
3195 case Configuration.ORIENTATION_LANDSCAPE:
3196 return 1;
3197 default:
3198 return 0;
3199 }
3200 }
3201
Michael Jurkaae65ee32012-04-30 11:45:54 -07003202 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003204 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003205 // Look for the toolbar icon specified in the activity meta-data
3206 Bundle metaData = packageManager.getActivityInfo(
3207 activityName, PackageManager.GET_META_DATA).metaData;
3208 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003209 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003210 if (iconResId != 0) {
3211 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003212 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003213 }
3214 }
3215 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003216 // This can happen if the activity defines an invalid drawable
3217 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3218 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003219 } catch (Resources.NotFoundException nfe) {
3220 // This can happen if the activity defines an invalid drawable
3221 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3222 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003223 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003224 return null;
3225 }
3226
3227 // if successful in getting icon, return it; otherwise, set button to use default drawable
3228 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003229 int buttonId, ComponentName activityName, int fallbackDrawableId,
3230 String toolbarResourceName) {
3231 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003232 Resources r = getResources();
3233 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3234 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003235
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003236 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003237 // If we were unable to find the icon via the meta-data, use a generic one
3238 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003239 toolbarIcon = r.getDrawable(fallbackDrawableId);
3240 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003241 if (button != null) {
3242 button.setCompoundDrawables(toolbarIcon, null, null, null);
3243 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003244 return null;
3245 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003246 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003247 if (button != null) {
3248 button.setCompoundDrawables(toolbarIcon, null, null, null);
3249 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003250 return toolbarIcon.getConstantState();
3251 }
3252 }
3253
3254 // if successful in getting icon, return it; otherwise, set button to use default drawable
3255 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003256 int buttonId, ComponentName activityName, int fallbackDrawableId,
3257 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003258 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003259 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003260
Michael Jurka19e0fc52011-07-22 18:00:21 -07003261 if (button != null) {
3262 // If we were unable to find the icon via the meta-data, use a
3263 // generic one
3264 if (toolbarIcon == null) {
3265 button.setImageResource(fallbackDrawableId);
3266 } else {
3267 button.setImageDrawable(toolbarIcon);
3268 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003269 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003270
3271 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3272
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273 }
3274
Winson Chung1b884092012-06-08 17:13:02 -07003275 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003276 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003277 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003278 }
3279
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003280 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003281 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003282 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003283 }
3284
Winson Chungbb185bd2011-11-21 12:31:42 -08003285 private void invalidatePressedFocusedStates(View container, View button) {
3286 if (container instanceof HolographicLinearLayout) {
3287 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3288 layout.invalidatePressedFocusedStates();
3289 } else if (button instanceof HolographicImageView) {
3290 HolographicImageView view = (HolographicImageView) button;
3291 view.invalidatePressedFocusedStates();
3292 }
3293 }
3294
Cristina Stancu476493b2013-08-07 17:20:14 +01003295 public View getQsbBar() {
3296 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003297 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3298 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003299 }
3300 return mQsbBar;
3301 }
3302
3303 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003305 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003306 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003307 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003308
Winson Chung1cad91e2011-05-25 17:41:01 -07003309 final SearchManager searchManager =
3310 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3311 ComponentName activityName = searchManager.getGlobalSearchActivity();
3312 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003313 int coi = getCurrentOrientationIndexForGlobalIcons();
3314 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003315 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3316 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3317 if (sGlobalSearchIcon[coi] == null) {
3318 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3319 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3320 TOOLBAR_ICON_METADATA_NAME);
3321 }
3322
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3324 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003325 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003327 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003328 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003329 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3330 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003331 if (searchButton != null) searchButton.setVisibility(View.GONE);
3332 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003333 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003334 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003335 }
3336 }
3337
Cristina Stancu476493b2013-08-07 17:20:14 +01003338 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003339 final View searchButtonContainer = findViewById(R.id.search_button_container);
3340 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003341 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003342 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003343 }
3344
Cristina Stancu476493b2013-08-07 17:20:14 +01003345 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003346 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003347 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003348
Winson Chungc51db6a2011-10-05 11:44:49 -07003349 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003350 final SearchManager searchManager =
3351 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3352 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3353
3354 ComponentName activityName = null;
3355 if (globalSearchActivity != null) {
3356 // Check if the global search activity handles voice search
3357 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3358 intent.setPackage(globalSearchActivity.getPackageName());
3359 activityName = intent.resolveActivity(getPackageManager());
3360 }
3361
3362 if (activityName == null) {
3363 // Fallback: check if an activity other than the global search activity
3364 // resolves this
3365 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3366 activityName = intent.resolveActivity(getPackageManager());
3367 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003368 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003369 int coi = getCurrentOrientationIndexForGlobalIcons();
3370 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003371 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3372 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3373 if (sVoiceSearchIcon[coi] == null) {
3374 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3375 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3376 TOOLBAR_ICON_METADATA_NAME);
3377 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003378 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003379 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003380 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003381 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003382 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003383 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003385 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003386 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003387 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003388 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003389 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003390
Cristina Stancu476493b2013-08-07 17:20:14 +01003391 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003392 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3393 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003394 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003395 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003396 }
3397
Winson Chung5841efa2013-09-30 18:06:44 -07003398 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003399 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3400 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003401 boolean visible = !forceDisableVoiceButtonProxy &&
3402 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003403 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003404 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003405 }
3406 }
Winson Chung5841efa2013-09-30 18:06:44 -07003407
3408 /**
3409 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3410 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3411 */
3412 public void disableVoiceButtonProxy(boolean disabled) {
3413 updateVoiceButtonProxyVisible(disabled);
3414 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003415 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003416 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003417 */
3418 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003419 if (!DISABLE_MARKET_BUTTON) {
3420 final View marketButton = findViewById(R.id.market_button);
3421 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3422 // Find the app market activity by resolving an intent.
3423 // (If multiple app markets are installed, it will return the ResolverActivity.)
3424 ComponentName activityName = intent.resolveActivity(getPackageManager());
3425 if (activityName != null) {
3426 int coi = getCurrentOrientationIndexForGlobalIcons();
3427 mAppMarketIntent = intent;
3428 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3429 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3430 TOOLBAR_ICON_METADATA_NAME);
3431 marketButton.setVisibility(View.VISIBLE);
3432 } else {
3433 // We should hide and disable the view so that we don't try and restore the visibility
3434 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3435 marketButton.setVisibility(View.GONE);
3436 marketButton.setEnabled(false);
3437 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003438 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003439 }
3440
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003441 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003442 if (!DISABLE_MARKET_BUTTON) {
3443 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3444 Resources r = getResources();
3445 Drawable marketIconDrawable = d.newDrawable(r);
3446 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3447 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3448 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003449
Winson Chungd64a6662013-09-30 11:06:59 -07003450 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3451 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003452 }
3453
Michael Jurkad7c28052012-04-27 15:43:36 -07003454 @Override
3455 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003456 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003457 final List<CharSequence> text = event.getText();
3458 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003459 // Populate event with a fake title based on the current state.
3460 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003461 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003462 } else {
3463 text.add(getString(R.string.all_apps_home_button_label));
3464 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003465 return result;
3466 }
3467
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003468 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003469 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003470 */
3471 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3472 @Override
3473 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003474 closeSystemDialogs();
3475 }
3476 }
3477
3478 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003479 * Receives notifications whenever the appwidgets are reset.
3480 */
3481 private class AppWidgetResetObserver extends ContentObserver {
3482 public AppWidgetResetObserver() {
3483 super(new Handler());
3484 }
3485
3486 @Override
3487 public void onChange(boolean selfChange) {
3488 onAppWidgetReset();
3489 }
3490 }
3491
3492 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003493 * If the activity is currently paused, signal that we need to run the passed Runnable
3494 * in onResume.
3495 *
3496 * This needs to be called from incoming places where resources might have been loaded
3497 * while we are paused. That is becaues the Configuration might be wrong
3498 * when we're not running, and if it comes back to what it was when we
3499 * were paused, we are not restarted.
3500 *
3501 * Implementation of the method from LauncherModel.Callbacks.
3502 *
3503 * @return true if we are currently paused. The caller might be able to
3504 * skip some work in that case since we will come back again.
3505 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003506 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003507 if (mPaused) {
3508 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003509 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003510 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003511 }
3512 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003513 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003514 return true;
3515 } else {
3516 return false;
3517 }
3518 }
3519
Michael Jurkac402cd92013-05-20 15:49:32 +02003520 private boolean waitUntilResume(Runnable run) {
3521 return waitUntilResume(run, false);
3522 }
3523
Michael Jurka1e2f4652013-07-08 18:03:46 -07003524 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003525 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003526 }
3527
Michael Jurka7607c2f2013-04-03 14:33:19 -07003528 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003529 * If the activity is currently paused, signal that we need to re-run the loader
3530 * in onResume.
3531 *
3532 * This needs to be called from incoming places where resources might have been loaded
3533 * while we are paused. That is becaues the Configuration might be wrong
3534 * when we're not running, and if it comes back to what it was when we
3535 * were paused, we are not restarted.
3536 *
3537 * Implementation of the method from LauncherModel.Callbacks.
3538 *
3539 * @return true if we are currently paused. The caller might be able to
3540 * skip some work in that case since we will come back again.
3541 */
3542 public boolean setLoadOnResume() {
3543 if (mPaused) {
3544 Log.i(TAG, "setLoadOnResume");
3545 mOnResumeNeedsLoad = true;
3546 return true;
3547 } else {
3548 return false;
3549 }
3550 }
3551
3552 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003556 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003557 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003558 } else {
3559 return SCREEN_COUNT / 2;
3560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003562
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 /**
3564 * Refreshes the shortcuts shown on the workspace.
3565 *
3566 * Implementation of the method from LauncherModel.Callbacks.
3567 */
3568 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003569 // If we're starting binding all over again, clear any bind calls we'd postponed in
3570 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3571 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003572 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003573
Winson Chungd64d1762013-08-20 14:37:16 -07003574 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003575 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003576 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003577
Michael Jurka05bf6442011-11-30 20:28:53 -08003578 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003579 if (mHotseat != null) {
3580 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003581 }
3582 }
3583
Adam Cohendcd297f2013-06-18 13:13:40 -07003584 @Override
3585 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003586 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003587
Adam Cohen5084cba2013-09-03 12:01:16 -07003588 // If there are no screens, we need to have an empty screen
3589 if (orderedScreenIds.size() == 0) {
3590 mWorkspace.addExtraEmptyScreen();
3591 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003592
3593 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003594 // setCurrentPage() so ensure that all pages are added before calling this).
3595 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003596 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3597 mWorkspace.createCustomContentPage();
3598 }
Winson Chung64359a52013-07-08 17:17:08 -07003599 }
3600
3601 @Override
3602 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003603 int count = orderedScreenIds.size();
3604 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003605 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003606 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003607 }
3608
Adam Cohen39a06042013-07-19 14:30:12 -07003609 private boolean shouldShowWeightWatcher() {
3610 String spKey = LauncherAppState.getSharedPreferencesKey();
3611 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003612 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003613
3614 return show;
3615 }
3616
3617 private void toggleShowWeightWatcher() {
3618 String spKey = LauncherAppState.getSharedPreferencesKey();
3619 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3620 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3621
3622 show = !show;
3623
3624 SharedPreferences.Editor editor = sp.edit();
3625 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3626 editor.commit();
3627
3628 if (mWeightWatcher != null) {
3629 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3630 }
3631 }
3632
Winson Chungd64d1762013-08-20 14:37:16 -07003633 public void bindAppsAdded(final ArrayList<Long> newScreens,
3634 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003635 final ArrayList<ItemInfo> addAnimated,
3636 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003637 Runnable r = new Runnable() {
3638 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003639 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003640 }
3641 };
3642 if (waitUntilResume(r)) {
3643 return;
3644 }
3645
Winson Chungd64d1762013-08-20 14:37:16 -07003646 // Add the new screens
3647 bindAddScreens(newScreens);
3648
3649 // We add the items without animation on non-visible pages, and with
3650 // animations on the new page (which we will try and snap to).
3651 if (!addNotAnimated.isEmpty()) {
3652 bindItems(addNotAnimated, 0,
3653 addNotAnimated.size(), false);
3654 }
3655 if (!addAnimated.isEmpty()) {
3656 bindItems(addAnimated, 0,
3657 addAnimated.size(), true);
3658 }
Winson Chungc58497e2013-09-03 17:48:37 -07003659
Winson Chung87412982013-10-03 18:34:14 -07003660 // Remove the extra empty screen
3661 mWorkspace.removeExtraEmptyScreen();
3662
Winson Chungc58497e2013-09-03 17:48:37 -07003663 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3664 addedApps != null && mAppsCustomizeContent != null) {
3665 mAppsCustomizeContent.addApps(addedApps);
3666 }
Winson Chungd64d1762013-08-20 14:37:16 -07003667 }
3668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003669 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003670 * Bind the items start-end from the list.
3671 *
3672 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003673 */
Winson Chung64359a52013-07-08 17:17:08 -07003674 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3675 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003676 Runnable r = new Runnable() {
3677 public void run() {
3678 bindItems(shortcuts, start, end, forceAnimateIcons);
3679 }
3680 };
3681 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 return;
3683 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003684
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003686 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3687 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003688 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003690 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003693
3694 // Short circuit if we are loading dock items for a configuration which has no dock
3695 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3696 mHotseat == null) {
3697 continue;
3698 }
3699
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 switch (item.itemType) {
3701 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3702 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003703 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003705
Winson Chung64359a52013-07-08 17:17:08 -07003706 /*
3707 * TODO: FIX collision case
3708 */
Winson Chungce376632013-07-11 16:12:41 -07003709 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3710 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3711 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3712 throw new RuntimeException("OCCUPIED");
3713 }
Winson Chung64359a52013-07-08 17:17:08 -07003714 }
3715
Adam Cohendcd297f2013-06-18 13:13:40 -07003716 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3717 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003718 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003719 // Animate all the applications up now
3720 shortcut.setAlpha(0f);
3721 shortcut.setScaleX(0f);
3722 shortcut.setScaleY(0f);
3723 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003724 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003727 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003728 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003729 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003730 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003731 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3732 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003734 default:
3735 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003737 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003738
Winson Chung997a9232013-07-24 15:33:46 -07003739 if (animateIcons) {
3740 // Animate to the correct page
3741 if (newShortcutsScreenId > -1) {
3742 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003743 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003744 final Runnable startBounceAnimRunnable = new Runnable() {
3745 public void run() {
3746 anim.playTogether(bounceAnims);
3747 anim.start();
3748 }
3749 };
Winson Chung997a9232013-07-24 15:33:46 -07003750 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003751 // We post the animation slightly delayed to prevent slowdowns
3752 // when we are loading right after we return to launcher.
3753 mWorkspace.postDelayed(new Runnable() {
3754 public void run() {
3755 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003756 mWorkspace.postDelayed(startBounceAnimRunnable,
3757 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003758 }
3759 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003760 } else {
3761 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003762 }
3763 }
Winson Chung64359a52013-07-08 17:17:08 -07003764 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003766 }
3767
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 /**
3769 * Implementation of the method from LauncherModel.Callbacks.
3770 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003771 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003772 Runnable r = new Runnable() {
3773 public void run() {
3774 bindFolders(folders);
3775 }
3776 };
3777 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003778 return;
3779 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003780 sFolders.clear();
3781 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783
3784 /**
3785 * Add the views for a widget to the workspace.
3786 *
3787 * Implementation of the method from LauncherModel.Callbacks.
3788 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003789 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003790 Runnable r = new Runnable() {
3791 public void run() {
3792 bindAppWidget(item);
3793 }
3794 };
3795 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 return;
3797 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003798
Daniel Sandler843e8602010-06-07 14:59:01 -04003799 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3800 if (DEBUG_WIDGETS) {
3801 Log.d(TAG, "bindAppWidget: " + item);
3802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 final Workspace workspace = mWorkspace;
3804
3805 final int appWidgetId = item.appWidgetId;
3806 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003807 if (DEBUG_WIDGETS) {
3808 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3809 }
3810
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3812
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003814 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815
Adam Cohendcd297f2013-06-18 13:13:40 -07003816 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003818 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3819
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 workspace.requestLayout();
3821
Daniel Sandler843e8602010-06-07 14:59:01 -04003822 if (DEBUG_WIDGETS) {
3823 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3824 + (SystemClock.uptimeMillis()-start) + "ms");
3825 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 }
3827
Adam Cohen1462de32012-07-24 22:34:36 -07003828 public void onPageBoundSynchronously(int page) {
3829 mSynchronouslyBoundPages.add(page);
3830 }
3831
Joe Onorato9c1289c2009-08-17 11:03:03 -04003832 /**
3833 * Callback saying that there aren't any more items to bind.
3834 *
3835 * Implementation of the method from LauncherModel.Callbacks.
3836 */
Adam Cohene25af792013-06-06 23:08:25 -07003837 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003838 Runnable r = new Runnable() {
3839 public void run() {
3840 finishBindingItems(upgradePath);
3841 }
3842 };
3843 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003844 return;
3845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 if (mSavedState != null) {
3847 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003848 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 mSavedState = null;
3851 }
3852
Adam Cohen1462de32012-07-24 22:34:36 -07003853 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003855 // If we received the result of any pending adds while the loader was running (e.g. the
3856 // widget configuration forced an orientation change), process them now.
3857 for (int i = 0; i < sPendingAddList.size(); i++) {
3858 completeAdd(sPendingAddList.get(i));
3859 }
3860 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861
Winson Chungc51db6a2011-10-05 11:44:49 -07003862 // Update the market app icon as necessary (the other icons will be managed in response to
3863 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003864 if (!DISABLE_MARKET_BUTTON) {
3865 updateAppMarketIcon();
3866 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003867
Winson Chungf0c6ae02012-03-21 16:10:31 -07003868 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003869 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003870 mWorkspace.getUniqueComponents(true, null);
3871 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003872 }
Adam Cohen99894d92013-06-14 11:22:59 -07003873
Adam Cohen66a01fd2013-06-11 12:48:00 -07003874 mWorkspace.post(new Runnable() {
3875 @Override
3876 public void run() {
3877 onFinishBindingItems();
3878 }
3879 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003880 }
3881
Winson Chunga0b7e862013-09-05 16:03:15 -07003882 public boolean isAllAppsButtonRank(int rank) {
3883 if (mHotseat != null) {
3884 return mHotseat.isAllAppsButtonRank(rank);
3885 }
3886 return false;
3887 }
3888
Winson Chunga2413752012-04-03 14:22:34 -07003889 private boolean canRunNewAppsAnimation() {
3890 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3891 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3892 }
3893
Winson Chungc9168342013-06-26 14:54:55 -07003894 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3895 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3896 PropertyValuesHolder.ofFloat("alpha", 1f),
3897 PropertyValuesHolder.ofFloat("scaleX", 1f),
3898 PropertyValuesHolder.ofFloat("scaleY", 1f));
3899 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3900 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3901 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3902 return bounceAnim;
3903 }
3904
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003905 @Override
3906 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003907 boolean searchVisible = updateGlobalSearchIcon();
3908 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003909 if (mSearchDropTargetBar != null) {
3910 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3911 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003912 }
3913
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003914 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 * Add the icons for all apps.
3916 *
3917 * Implementation of the method from LauncherModel.Callbacks.
3918 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003919 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003920 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3921 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3922 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3923 getHotseat().addAllAppsFolder(mIconCache, apps,
3924 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3925 }
3926 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003927 }
Winson Chungc58497e2013-09-03 17:48:37 -07003928 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003929 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003930 mAppsCustomizeContent.setApps(apps);
3931 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 }
3934
3935 /**
3936 * A package was updated.
3937 *
3938 * Implementation of the method from LauncherModel.Callbacks.
3939 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003940 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003941 Runnable r = new Runnable() {
3942 public void run() {
3943 bindAppsUpdated(apps);
3944 }
3945 };
3946 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003947 return;
3948 }
3949
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003950 if (mWorkspace != null) {
3951 mWorkspace.updateShortcuts(apps);
3952 }
Winson Chungc58497e2013-09-03 17:48:37 -07003953
3954 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3955 mAppsCustomizeContent != null) {
3956 mAppsCustomizeContent.updateApps(apps);
3957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003958 }
3959
3960 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003961 * A package was uninstalled. We take both the super set of packageNames
3962 * in addition to specific applications to remove, the reason being that
3963 * this can be called when a package is updated as well. In that scenario,
3964 * we only remove specific components from the workspace, where as
3965 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 *
3967 * Implementation of the method from LauncherModel.Callbacks.
3968 */
Winson Chung83892cc2013-05-01 16:53:33 -07003969 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003970 final ArrayList<AppInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003971 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003972 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003973 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003974 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003975 }
Winson Chungd64d1762013-08-20 14:37:16 -07003976 };
3977 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003978 return;
3979 }
3980
Winson Chung64359a52013-07-08 17:17:08 -07003981 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003982 mWorkspace.removeItemsByPackageName(packageNames);
3983 } else {
3984 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003985 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003986
Winson Chunga1820962011-10-03 16:31:06 -07003987 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003988 mDragController.onAppsRemoved(appInfos, this);
Winson Chungc58497e2013-09-03 17:48:37 -07003989
3990 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3991 mAppsCustomizeContent != null) {
3992 mAppsCustomizeContent.removeApps(appInfos);
3993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 }
Joe Onoratobe386092009-11-17 17:32:16 -08003995
3996 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003997 * A number of packages were updated.
3998 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003999 private ArrayList<Object> mWidgetsAndShortcuts;
4000 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004001 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004002 bindPackagesUpdated(mWidgetsAndShortcuts);
4003 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004004 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004005 };
4006
4007 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4008 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4009 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004010 return;
4011 }
4012
Winson Chung64359a52013-07-08 17:17:08 -07004013 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004014 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4015 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004016 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004017 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004018 }
4019
Winson Chung400438b2011-01-16 17:53:48 -08004020 private int mapConfigurationOriActivityInfoOri(int configOri) {
4021 final Display d = getWindowManager().getDefaultDisplay();
4022 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4023 switch (d.getRotation()) {
4024 case Surface.ROTATION_0:
4025 case Surface.ROTATION_180:
4026 // We are currently in the same basic orientation as the natural orientation
4027 naturalOri = configOri;
4028 break;
4029 case Surface.ROTATION_90:
4030 case Surface.ROTATION_270:
4031 // We are currently in the other basic orientation to the natural orientation
4032 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4033 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4034 break;
4035 }
4036
4037 int[] oriMap = {
4038 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4039 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4040 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4041 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4042 };
4043 // Since the map starts at portrait, we need to offset if this device's natural orientation
4044 // is landscape.
4045 int indexOffset = 0;
4046 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4047 indexOffset = 1;
4048 }
4049 return oriMap[(d.getRotation() + indexOffset) % 4];
4050 }
Adam Cohen446e9402011-09-15 18:21:21 -07004051
Winson Chung4b919f82012-05-01 10:44:08 -07004052 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004053 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004054 getResources().getBoolean(R.bool.allow_rotation);
4055 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004056 }
Winson Chung4b919f82012-05-01 10:44:08 -07004057 public void lockScreenOrientation() {
4058 if (isRotationEnabled()) {
4059 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4060 .getConfiguration().orientation));
4061 }
4062 }
4063 public void unlockScreenOrientation(boolean immediate) {
4064 if (isRotationEnabled()) {
4065 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004066 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004067 } else {
4068 mHandler.postDelayed(new Runnable() {
4069 public void run() {
4070 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4071 }
4072 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004073 }
Winson Chung4b919f82012-05-01 10:44:08 -07004074 }
Winson Chung400438b2011-01-16 17:53:48 -08004075 }
4076
Winson Chung82f55532011-08-09 14:14:23 -07004077 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004079 if (DISABLE_CLINGS) {
4080 return false;
4081 }
4082
Brett Chabot2a9e2282011-08-23 15:03:13 -07004083 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004084 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004085
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004086 // Disable clings for accessibility when explore by touch is enabled
4087 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4088 ACCESSIBILITY_SERVICE);
4089 if (a11yManager.isTouchExplorationEnabled()) {
4090 return false;
4091 }
4092
Michael Jurkae233a8b2013-03-19 13:49:20 +01004093 // Restricted secondary users (child mode) will potentially have very few apps
4094 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004095// boolean supportsLimitedUsers =
4096// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4097// Account[] accounts = AccountManager.get(this).getAccounts();
4098// if (supportsLimitedUsers && accounts.length == 0) {
4099// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4100// Bundle restrictions = um.getUserRestrictions();
4101// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4102// return false;
4103// }
4104// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004105 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004106 }
Michael Jurka22143132012-10-04 17:42:38 +02004107
Winson Chungfa545132013-09-26 17:45:32 -07004108 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004109 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004110 Cling cling = (Cling) findViewById(clingId);
4111 View scrim = null;
4112 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004113 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004114 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004116 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004117 cling.show(animate, SHOW_CLING_DURATION);
4118
4119 if (dimNavBarVisibilty) {
4120 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4121 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004122 }
4123 }
4124 return cling;
4125 }
Michael Jurka22143132012-10-04 17:42:38 +02004126
Winson Chungaf40f202013-09-18 18:26:31 -07004127 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4128 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004129 // To catch cases where siblings of top-level views are made invisible, just check whether
4130 // the cling is directly set to GONE before dismissing it.
4131 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004132 final Runnable cleanUpClingCb = new Runnable() {
4133 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004134 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004135 // We should update the shared preferences on a background thread
4136 new Thread("dismissClingThread") {
4137 public void run() {
4138 SharedPreferences.Editor editor = mSharedPrefs.edit();
4139 editor.putBoolean(flag, true);
4140 editor.commit();
4141 }
4142 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004143 if (postAnimationCb != null) {
4144 postAnimationCb.run();
4145 }
4146 }
4147 };
4148 if (duration <= 0) {
4149 cleanUpClingCb.run();
4150 } else {
4151 cling.hide(duration, cleanUpClingCb);
4152 }
Michael Jurka22143132012-10-04 17:42:38 +02004153 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004154
4155 if (restoreNavBarVisibilty) {
4156 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4157 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4158 }
Winson Chung82f55532011-08-09 14:14:23 -07004159 }
4160 }
Michael Jurka22143132012-10-04 17:42:38 +02004161
Winson Chung9d9d74f2011-09-19 11:49:12 -07004162 private void removeCling(int id) {
4163 final View cling = findViewById(id);
4164 if (cling != null) {
4165 final ViewGroup parent = (ViewGroup) cling.getParent();
4166 parent.post(new Runnable() {
4167 @Override
4168 public void run() {
4169 parent.removeView(cling);
4170 }
4171 });
Michael Jurka22143132012-10-04 17:42:38 +02004172 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004173 }
4174 }
Michael Jurka974c3862012-05-22 22:00:31 -07004175
4176 private boolean skipCustomClingIfNoAccounts() {
4177 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4178 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4179 if (customCling) {
4180 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004181 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004182 Account[] accounts = am.getAccountsByType("com.google");
4183 return accounts.length == 0;
4184 }
4185 return false;
4186 }
4187
Winson Chung82e5c982013-10-09 12:04:50 -07004188 public void updateCustomContentHintVisibility() {
4189 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4190 String ccHintStr = getFirstRunCustomContentHint();
4191
4192 if (mWorkspace.hasCustomContent()) {
4193 // Show the custom content hint if ccHintStr is not empty
4194 if (cling != null) {
4195 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4196 }
4197 } else {
4198 // Hide the custom content hint
4199 if (cling != null) {
4200 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4201 }
4202 }
4203 }
4204
4205 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4206 boolean animate) {
4207 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4208 if (ccHint != null) {
4209 if (visible && !ccHintStr.isEmpty()) {
4210 ccHint.setText(ccHintStr);
4211 ccHint.setVisibility(View.VISIBLE);
4212 if (animate) {
4213 ccHint.setAlpha(0f);
4214 ccHint.animate().alpha(1f)
4215 .setDuration(SHOW_CLING_DURATION)
4216 .start();
4217 } else {
4218 ccHint.setAlpha(1f);
4219 }
4220 } else {
4221 if (animate) {
4222 ccHint.animate().alpha(0f)
4223 .setDuration(SHOW_CLING_DURATION)
4224 .setListener(new AnimatorListenerAdapter() {
4225 @Override
4226 public void onAnimationEnd(Animator animation) {
4227 ccHint.setVisibility(View.GONE);
4228 }
4229 })
4230 .start();
4231 } else {
4232 ccHint.setAlpha(0f);
4233 ccHint.setVisibility(View.GONE);
4234 }
4235 }
4236 }
4237 }
4238
Winson Chungaf40f202013-09-18 18:26:31 -07004239 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004240 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004241 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004242 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004243 // If we're not using the default workspace layout, replace workspace cling
4244 // with a custom workspace cling (usually specified in an overlay)
4245 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004246 if (!DISABLE_CUSTOM_CLINGS) {
4247 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4248 getResources().getBoolean(R.bool.config_useCustomClings)) {
4249 // Use a custom cling
4250 View cling = findViewById(R.id.workspace_cling);
4251 ViewGroup clingParent = (ViewGroup) cling.getParent();
4252 int clingIndex = clingParent.indexOfChild(cling);
4253 clingParent.removeViewAt(clingIndex);
4254 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4255 clingParent.addView(customCling, clingIndex);
4256 customCling.setId(R.id.workspace_cling);
4257 }
4258 }
4259 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4260 if (cling != null) {
4261 String sbHintStr = getFirstRunClingSearchBarHint();
4262 String ccHintStr = getFirstRunCustomContentHint();
4263 if (!sbHintStr.isEmpty()) {
4264 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4265 sbHint.setText(sbHintStr);
4266 sbHint.setVisibility(View.VISIBLE);
4267 }
Winson Chung82e5c982013-10-09 12:04:50 -07004268 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004269 }
Winson Chungfa545132013-09-26 17:45:32 -07004270 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004271 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004272 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004273 }
4274 }
Winson Chungaf40f202013-09-18 18:26:31 -07004275
Winson Chunge6eabff2013-09-24 11:01:23 -07004276 protected String getFirstRunClingSearchBarHint() {
4277 return "";
4278 }
4279 protected String getFirstRunCustomContentHint() {
4280 return "";
4281 }
4282
Winson Chungaf40f202013-09-18 18:26:31 -07004283 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004284 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004285 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004286 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004287 initCling(R.id.workspace_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004288 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004289 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004290 }
4291 }
4292 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004293 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004294 if (isClingsEnabled() &&
4295 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004296 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4297 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004298 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004299 } else {
4300 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004301 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004302 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004303 }
Michael Jurka104c4562013-07-08 18:03:46 -07004304 protected SharedPreferences getSharedPrefs() {
4305 return mSharedPrefs;
4306 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004307 public boolean isFolderClingVisible() {
4308 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004309 if (cling != null) {
4310 return cling.getVisibility() == View.VISIBLE;
4311 }
4312 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004313 }
Winson Chungaf40f202013-09-18 18:26:31 -07004314 public void dismissFirstRunCling(View v) {
4315 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4316 Runnable cb = new Runnable() {
4317 public void run() {
4318 // Show the workspace cling next
4319 showFirstRunWorkspaceCling();
4320 }
4321 };
4322 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4323 DISMISS_CLING_DURATION, false);
4324 }
Winson Chung82f55532011-08-09 14:14:23 -07004325 public void dismissWorkspaceCling(View v) {
4326 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004327 Runnable cb = null;
4328 if (v == null) {
4329 cb = new Runnable() {
4330 public void run() {
4331 mWorkspace.enterOverviewMode();
4332 }
4333 };
4334 }
4335 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4336 DISMISS_CLING_DURATION, true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004337 }
4338 public void dismissFolderCling(View v) {
4339 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004340 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4341 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004342 }
4343
Winson Chung80baf5a2010-08-09 16:03:15 -07004344 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004345 * Prints out out state for debugging.
4346 */
4347 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004348 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004349 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004350 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4351 Log.d(TAG, "mRestoring=" + mRestoring);
4352 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4353 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004354 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004355 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004356
Winson Chung785d2eb2011-04-14 16:08:02 -07004357 if (mAppsCustomizeContent != null) {
4358 mAppsCustomizeContent.dumpState();
4359 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004360 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004361 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004362
4363 @Override
4364 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4365 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004366 synchronized (sDumpLogs) {
4367 writer.println(" ");
4368 writer.println("Debug logs: ");
4369 for (int i = 0; i < sDumpLogs.size(); i++) {
4370 writer.println(" " + sDumpLogs.get(i));
4371 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004372 }
4373 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004374
4375 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004376 if (DEBUG_DUMP_LOG) {
4377 synchronized (sDumpLogs) {
4378 Log.d(TAG, "");
4379 Log.d(TAG, "*********************");
4380 Log.d(TAG, "Launcher debug logs: ");
4381 for (int i = 0; i < sDumpLogs.size(); i++) {
4382 Log.d(TAG, " " + sDumpLogs.get(i));
4383 }
4384 Log.d(TAG, "*********************");
4385 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004386 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004387 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004388 }
4389
4390 public static void addDumpLog(String tag, String log, boolean debugLog) {
4391 if (debugLog) {
4392 Log.d(tag, log);
4393 }
Winson Chungede41292013-09-19 16:27:36 -07004394 if (DEBUG_DUMP_LOG) {
4395 sDateStamp.setTime(System.currentTimeMillis());
4396 synchronized (sDumpLogs) {
4397 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004398 }
Winson Chungede41292013-09-19 16:27:36 -07004399 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004400 }
4401
Winson Chungede41292013-09-19 16:27:36 -07004402 public void dumpLogsToLocalData() {
4403 if (DEBUG_DUMP_LOG) {
4404 new Thread("DumpLogsToLocalData") {
4405 @Override
4406 public void run() {
4407 boolean success = false;
4408 sDateStamp.setTime(sRunStart);
4409 String FILENAME = sDateStamp.getMonth() + "-"
4410 + sDateStamp.getDay() + "_"
4411 + sDateStamp.getHours() + "-"
4412 + sDateStamp.getMinutes() + "_"
4413 + sDateStamp.getSeconds() + ".txt";
4414
4415 FileOutputStream fos = null;
4416 File outFile = null;
4417 try {
4418 outFile = new File(getFilesDir(), FILENAME);
4419 outFile.createNewFile();
4420 fos = new FileOutputStream(outFile);
4421 } catch (Exception e) {
4422 e.printStackTrace();
4423 }
4424 if (fos != null) {
4425 PrintWriter writer = new PrintWriter(fos);
4426
4427 writer.println(" ");
4428 writer.println("Debug logs: ");
4429 synchronized (sDumpLogs) {
4430 for (int i = 0; i < sDumpLogs.size(); i++) {
4431 writer.println(" " + sDumpLogs.get(i));
4432 }
4433 }
4434 writer.close();
4435 }
4436 try {
4437 if (fos != null) {
4438 fos.close();
4439 success = true;
4440 }
4441 } catch (IOException e) {
4442 e.printStackTrace();
4443 }
4444 }
4445 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004446 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004447 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004448}
Michael Jurka2763be32011-02-24 11:19:57 -08004449
Michael Jurkaabded662011-03-04 12:06:57 -08004450interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004451 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004452 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004453 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004454 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004455 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004456}