blob: f35daaade0fbb9387b0d0d05273d4a6ee41322e4 [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;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700200 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
201 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
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();
Winson Chunge5467dc2013-10-14 17:03:04 -0700405 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400406 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700408
Daniel Sandlerff02d492013-08-05 02:12:05 -0400409 mStats = new Stats(this);
410
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700411 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700412
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700413 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
414 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700415
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700416 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
417 // this also ensures that any synchronous binding below doesn't re-trigger another
418 // LauncherModel load.
419 mPaused = false;
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200422 android.os.Debug.startMethodTracing(
423 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100431 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800433 registerContentObservers();
434
Joe Onorato7c312c12009-08-13 21:36:53 -0700435 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mSavedState = savedInstanceState;
438 restoreState(mSavedState);
439
Winson Chunga12a2502010-12-20 14:41:35 -0800440 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700441 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200442 mAppsCustomizeContent.onPackagesUpdated(
443 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700444 }
Winson Chunga12a2502010-12-20 14:41:35 -0800445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
447 android.os.Debug.stopMethodTracing();
448 }
449
450 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700451 if (sPausedFromUserAction) {
452 // If the user leaves launcher, then we should just load items asynchronously when
453 // they return.
454 mModel.startLoader(true, -1);
455 } else {
456 // We only load the page synchronously if the user rotates (or triggers a
457 // configuration change) while launcher is in the foreground
458 mModel.startLoader(true, mWorkspace.getCurrentPage());
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 // For handling default keys
463 mDefaultKeySsb = new SpannableStringBuilder();
464 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800465
466 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
467 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800468
Adam Cohenf9426d52012-06-04 17:26:21 -0700469 updateGlobalIcons();
470
471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700473
474 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700475 }
476
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 protected void onUserLeaveHint() {
478 super.onUserLeaveHint();
479 sPausedFromUserAction = true;
480 }
481
Winson Chung98ca0f72013-07-29 12:58:51 -0700482 /** To be overriden by subclasses to hint to Launcher that we have custom content */
483 protected boolean hasCustomContentToLeft() {
484 return false;
485 }
486
Dave Hawkeya8881582013-09-17 15:55:33 -0600487 /**
488 * To be overridden by subclasses to create the custom content and call
489 * {@link #addToCustomContentPage}. This will only be invoked if
490 * {@link #hasCustomContentToLeft()} is {@code true}.
491 */
492 protected void addCustomContentToLeft() {
493 }
494
495 /**
496 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
497 * ensure the custom content page is added or removed if necessary.
498 */
499 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600500 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600501 // Not bound yet, wait for bindScreens to be called.
502 return;
503 }
504
505 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
506 // Create the custom content page and call the subclass to populate it.
507 mWorkspace.createCustomContentPage();
508 addCustomContentToLeft();
509 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
510 mWorkspace.removeCustomContentPage();
511 }
512 }
513
Adam Cohenf9426d52012-06-04 17:26:21 -0700514 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700515 boolean searchVisible = false;
516 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800517 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700518 int coi = getCurrentOrientationIndexForGlobalIcons();
519 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
520 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700521 if (!DISABLE_MARKET_BUTTON) {
522 updateAppMarketIcon();
523 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700524 searchVisible = updateGlobalSearchIcon();
525 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700526 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700527 if (sGlobalSearchIcon[coi] != null) {
528 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700529 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700531 if (sVoiceSearchIcon[coi] != null) {
532 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700533 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700534 }
Winson Chungd64a6662013-09-30 11:06:59 -0700535 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700536 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800537 }
Winson Chungadf0c182012-08-23 14:59:07 -0700538 if (mSearchDropTargetBar != null) {
539 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
Romain Guycbb89e42009-06-08 15:52:54 -0700542
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700544 if (sLocaleConfiguration == null) {
545 new AsyncTask<Void, Void, LocaleConfiguration>() {
546 @Override
547 protected LocaleConfiguration doInBackground(Void... unused) {
548 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
549 readConfiguration(Launcher.this, localeConfiguration);
550 return localeConfiguration;
551 }
552
553 @Override
554 protected void onPostExecute(LocaleConfiguration result) {
555 sLocaleConfiguration = result;
556 checkForLocaleChange(); // recursive, but now with a locale configuration
557 }
558 }.execute();
559 return;
560 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 final Configuration configuration = getResources().getConfiguration();
563
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700564 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 final String locale = configuration.locale.toString();
566
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700567 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 final int mcc = configuration.mcc;
569
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700570 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 final int mnc = configuration.mnc;
572
Romain Guy84f296c2009-11-04 15:00:44 -0800573 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574
Romain Guy84f296c2009-11-04 15:00:44 -0800575 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700576 sLocaleConfiguration.locale = locale;
577 sLocaleConfiguration.mcc = mcc;
578 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700579
Joe Onorato0589f0f2010-02-08 13:44:00 -0800580 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581
582 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
583 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700584 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 public void run() {
586 writeConfiguration(Launcher.this, localeConfiguration);
587 }
588 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700589 }
590 }
591
592 private static class LocaleConfiguration {
593 public String locale;
594 public int mcc = -1;
595 public int mnc = -1;
596 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700597
Romain Guy98d01652009-06-30 16:21:04 -0700598 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
599 DataInputStream in = null;
600 try {
601 in = new DataInputStream(context.openFileInput(PREFERENCES));
602 configuration.locale = in.readUTF();
603 configuration.mcc = in.readInt();
604 configuration.mnc = in.readInt();
605 } catch (FileNotFoundException e) {
606 // Ignore
607 } catch (IOException e) {
608 // Ignore
609 } finally {
610 if (in != null) {
611 try {
612 in.close();
613 } catch (IOException e) {
614 // Ignore
615 }
616 }
617 }
618 }
619
620 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
621 DataOutputStream out = null;
622 try {
623 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
624 out.writeUTF(configuration.locale);
625 out.writeInt(configuration.mcc);
626 out.writeInt(configuration.mnc);
627 out.flush();
628 } catch (FileNotFoundException e) {
629 // Ignore
630 } catch (IOException e) {
631 //noinspection ResultOfMethodCallIgnored
632 context.getFileStreamPath(PREFERENCES).delete();
633 } finally {
634 if (out != null) {
635 try {
636 out.close();
637 } catch (IOException e) {
638 // Ignore
639 }
640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
642 }
643
Anton Hanssona2f665f2013-09-26 12:18:32 +0100644 public Stats getStats() {
645 return mStats;
646 }
647
Bjorn Bringertc459e522013-06-07 19:36:01 +0100648 public LayoutInflater getInflater() {
649 return mInflater;
650 }
651
Adam Cohen716b51e2011-06-30 12:09:54 -0700652 public DragLayer getDragLayer() {
653 return mDragLayer;
654 }
655
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 boolean isDraggingEnabled() {
657 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
658 // that is subsequently removed from the workspace in startBinding().
659 return !mModel.isLoadingWorkspace();
660 }
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 static int getScreen() {
663 synchronized (sLock) {
664 return sScreen;
665 }
666 }
667
668 static void setScreen(int screen) {
669 synchronized (sLock) {
670 sScreen = screen;
671 }
672 }
673
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 /**
675 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
676 * a configuration step, this allows the proper animations to run after other transitions.
677 */
678 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700679 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 switch (args.requestCode) {
681 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700683 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800684 break;
685 case REQUEST_PICK_SHORTCUT:
686 processShortcut(args.intent);
687 break;
688 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700689 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700691 result = true;
692 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800693 case REQUEST_CREATE_APPWIDGET:
694 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700696 result = true;
697 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800698 }
Michael Jurka27614d22012-04-02 06:40:22 -0700699 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
700 // if you turned the screen off and then back while in All Apps, Launcher would not
701 // return to the workspace. Clearing mAddInfo.container here fixes this issue
702 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700703 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 protected void onActivityResult(
708 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700709 // Reset the startActivity waiting flag
710 mWaitingForResult = false;
711
Adam Cohenad4e15c2013-10-17 16:21:35 -0700712 Runnable exitSpringLoaded = new Runnable() {
713 @Override
714 public void run() {
715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
716 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
717 }
718 };
719
Michael Jurka8b805b12012-04-18 14:23:14 -0700720 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700721 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
723 if (resultCode == RESULT_CANCELED) {
724 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700725 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
726 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700728 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
729 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700730 }
731 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200732 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
733 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
734 mWorkspace.exitOverviewMode(false);
735 }
736 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200738
Adam Cohened66b2b2012-01-23 17:28:51 -0800739 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
740 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700741
Adam Cohened66b2b2012-01-23 17:28:51 -0800742 // We have special handling for widgets
743 if (isWidgetDrop) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 final int appWidgetId = data != null ?
Adam Cohened66b2b2012-01-23 17:28:51 -0800745 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 final int result;
747 final Runnable onComplete;
Winson Chung5aaab772012-04-27 15:24:02 -0700748 if (appWidgetId < 0) {
749 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
750 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 result = RESULT_CANCELED;
752 completeTwoStageWidgetDrop(result, appWidgetId);
753 onComplete = new Runnable() {
754 @Override
755 public void run() {
756 exitSpringLoadedDragModeDelayed(false, 0, null);
757 }
758 };
Winson Chung5aaab772012-04-27 15:24:02 -0700759 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 result = resultCode;
761 onComplete = new Runnable() {
762 @Override
763 public void run() {
764 completeTwoStageWidgetDrop(result, appWidgetId);
765 }
766 };
Winson Chung5aaab772012-04-27 15:24:02 -0700767 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
769 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800770 return;
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 // The pattern used here is that a user PICKs a specific application,
774 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700775
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
777 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700778 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800779 final PendingAddArguments args = new PendingAddArguments();
780 args.requestCode = requestCode;
781 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 args.cellX = mPendingAddInfo.cellX;
785 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800786 if (isWorkspaceLocked()) {
787 sPendingAddList.add(args);
788 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700793 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800797 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 }
799
800 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700801 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700802 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 Runnable onCompleteRunnable = null;
804 int animationType = 0;
805
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700806 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800807 if (resultCode == RESULT_OK) {
808 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
809 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700810 mPendingAddWidgetInfo);
811 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 onCompleteRunnable = new Runnable() {
813 @Override
814 public void run() {
815 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700817 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
818 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 }
820 };
821 } else if (resultCode == RESULT_CANCELED) {
822 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800823 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700824 if (mDragLayer.getAnimatedView() != null) {
825 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
826 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
827 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700829 // The animated view may be null in the case of a rotation during widget configuration
830 onCompleteRunnable.run();
831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
834 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700835 protected void onStop() {
836 super.onStop();
837 FirstFrameAnimatorHelper.setIsVisible(false);
838 }
839
840 @Override
841 protected void onStart() {
842 super.onStart();
843 FirstFrameAnimatorHelper.setIsVisible(true);
844 }
845
846 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200848 long startTime = 0;
849 if (DEBUG_RESUME_TIME) {
850 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400851 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700854
Winson Chung4a2afa32012-07-19 14:53:05 -0700855 // Restore the previous launcher state
856 if (mOnResumeState == State.WORKSPACE) {
857 showWorkspace(false);
858 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung82963d52013-10-09 11:20:57 -0700859 showAllApps(false, AppsCustomizePagedView.ContentType.Applications, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700860 }
861 mOnResumeState = State.NONE;
862
Craig Mautner360310b2012-10-26 15:13:08 -0700863 // Background was set to gradient in onPause(), restore to black if in all apps.
864 setWorkspaceBackground(mState == State.WORKSPACE);
865
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800866 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700867 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700868 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700872 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700874 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200875 // We might have postponed some bind calls until onResume (see waitUntilResume) --
876 // execute them here
877 long startTimeCallbacks = 0;
878 if (DEBUG_RESUME_TIME) {
879 startTimeCallbacks = System.currentTimeMillis();
880 }
881
882 if (mAppsCustomizeContent != null) {
883 mAppsCustomizeContent.setBulkBind(true);
884 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700885 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
886 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200887 }
888 if (mAppsCustomizeContent != null) {
889 mAppsCustomizeContent.setBulkBind(false);
890 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700891 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200892 if (DEBUG_RESUME_TIME) {
893 Log.d(TAG, "Time spent processing callbacks in onResume: " +
894 (System.currentTimeMillis() - startTimeCallbacks));
895 }
Michael Jurka447bf842013-05-15 14:52:15 +0200896 }
Michael Jurka54554252013-08-01 12:52:23 +0200897 if (mOnResumeCallbacks.size() > 0) {
898 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
899 mOnResumeCallbacks.get(i).run();
900 }
901 mOnResumeCallbacks.clear();
902 }
Winson Chunge4e50662012-01-23 14:45:13 -0800903
904 // Reset the pressed state of icons that were locked in the press state while activities
905 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800906 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800907 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800908 mWaitingForResume.setStayPressed(false);
909 }
Winson Chunge4e50662012-01-23 14:45:13 -0800910 if (mAppsCustomizeContent != null) {
911 // Resets the previous all apps icon press state
912 mAppsCustomizeContent.resetDrawableState();
913 }
Winson Chung82963d52013-10-09 11:20:57 -0700914
Adam Cohen06dff352012-06-01 17:17:08 -0700915 // It is possible that widgets can receive updates while launcher is not in the foreground.
916 // Consequently, the widgets will be inflated in the orientation of the foreground activity
917 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
918 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700919 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700920
Winson Chung780fe592013-09-26 14:48:44 -0700921 // Process any items that were added while Launcher was away.
922 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
923
Winson Chung5841efa2013-09-30 18:06:44 -0700924 // Update the voice search button proxy
925 updateVoiceButtonProxyVisible(false);
926
Adam Cohenf9426d52012-06-04 17:26:21 -0700927 // Again, as with the above scenario, it's possible that one or more of the global icons
928 // were updated in the wrong orientation.
929 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200930 if (DEBUG_RESUME_TIME) {
931 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
932 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700933
934 if (mWorkspace.getCustomContentCallbacks() != null) {
935 // If we are resuming and the custom content is the current page, we call onShow().
936 // It is also poassible that onShow will instead be called slightly after first layout
937 // if PagedView#setRestorePage was set to the custom content page in onCreate().
938 if (mWorkspace.isOnOrMovingToCustomContent()) {
939 mWorkspace.getCustomContentCallbacks().onShow();
940 }
941 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700942 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700943 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700944 }
945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700948 // Ensure that items added to Launcher are queued until Launcher returns
949 InstallShortcutReceiver.enableInstallQueue();
950
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700952 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800953 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700954 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -0700955
956 // We call onHide() aggressively. The custom content callbacks should be able to
957 // debounce excess onHide calls.
958 if (mWorkspace.getCustomContentCallbacks() != null) {
959 mWorkspace.getCustomContentCallbacks().onHide();
960 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961 }
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Adam Cohen66a01fd2013-06-11 12:48:00 -0700963 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -0700964 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600965 addCustomContentToLeft();
966 }
Adam Cohen66a01fd2013-06-11 12:48:00 -0700967 }
968
Adam Cohenbffe7452013-07-22 18:21:45 -0700969 QSBScroller mQsbScroller = new QSBScroller() {
970 int scrollY = 0;
971
972 @Override
973 public void setScrollY(int scroll) {
974 scrollY = scroll;
975
976 if (mWorkspace.isOnOrMovingToCustomContent()) {
977 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +0100978 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -0700979 }
980 }
981 };
982
983 public void resetQSBScroll() {
984 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +0100985 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -0700986 }
987
988 public interface CustomContentCallbacks {
989 // Custom content is completely shown
990 public void onShow();
991
992 // Custom content is completely hidden
993 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700994
995 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
996 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700997 }
998
Adam Cohen30bacb22013-08-29 14:25:25 -0700999 protected void startSettings() {
1000 }
1001
Adam Cohenbffe7452013-07-22 18:21:45 -07001002 public interface QSBScroller {
1003 public void setScrollY(int scrollY);
1004 }
1005
Winson Chung98ca0f72013-07-29 12:58:51 -07001006 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001007 CustomContentCallbacks callbacks, String description) {
1008 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001009 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001010 }
1011
Adam Cohenedb40762013-07-18 16:45:45 -07001012 // The custom content needs to offset its content to account for the QSB
1013 public int getTopOffsetForCustomContent() {
1014 return mWorkspace.getPaddingTop();
1015 }
1016
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001017 @Override
1018 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 // Flag the loader to stop early before switching
1020 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001021 if (mAppsCustomizeContent != null) {
1022 mAppsCustomizeContent.surrender();
1023 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001024 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001025 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001027 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001028 @Override
1029 public void onWindowFocusChanged(boolean hasFocus) {
1030 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001031 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001032 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 private boolean acceptFilter() {
1035 final InputMethodManager inputManager = (InputMethodManager)
1036 getSystemService(Context.INPUT_METHOD_SERVICE);
1037 return !inputManager.isFullscreenMode();
1038 }
1039
1040 @Override
1041 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001042 final int uniChar = event.getUnicodeChar();
1043 final boolean handled = super.onKeyDown(keyCode, event);
1044 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1045 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1047 keyCode, event);
1048 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001049 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001050 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001051 // showSearchDialog()
1052 // If there are multiple keystrokes before the search dialog takes focus,
1053 // onSearchRequested() will be called for every keystroke,
1054 // but it is idempotent, so it's fine.
1055 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057 }
1058
Joe Onorato8a9625e2010-01-28 15:55:35 -08001059 // Eat the long press event so the keyboard doesn't come up.
1060 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1061 return true;
1062 }
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 return handled;
1065 }
1066
Karl Rosaen138a0412009-04-23 19:00:21 -07001067 private String getTypedText() {
1068 return mDefaultKeySsb.toString();
1069 }
1070
1071 private void clearTypedText() {
1072 mDefaultKeySsb.clear();
1073 mDefaultKeySsb.clearSpans();
1074 Selection.setSelection(mDefaultKeySsb, 0);
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001078 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1079 * State
1080 */
1081 private static State intToState(int stateOrdinal) {
1082 State state = State.WORKSPACE;
1083 final State[] stateValues = State.values();
1084 for (int i = 0; i < stateValues.length; i++) {
1085 if (stateValues[i].ordinal() == stateOrdinal) {
1086 state = stateValues[i];
1087 break;
1088 }
1089 }
1090 return state;
1091 }
1092
1093 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 * Restores the previous state, if it exists.
1095 *
1096 * @param savedState The previous state.
1097 */
1098 private void restoreState(Bundle savedState) {
1099 if (savedState == null) {
1100 return;
1101 }
1102
Michael Jurka883f55b2010-10-21 15:47:14 -07001103 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001104 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001105 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001106 }
1107
Adam Cohen21cd0022013-10-09 18:57:02 -07001108 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1109 PagedView.INVALID_RESTORE_PAGE);
1110 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001111 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Winson Chung3d503fb2011-07-13 17:25:49 -07001114 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116
Winson Chung3d503fb2011-07-13 17:25:49 -07001117 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1118 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001119 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1121 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001122 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1123 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1124 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001125 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 mRestoring = true;
1127 }
1128
1129 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1130 if (renameFolder) {
1131 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001132 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 mRestoring = true;
1134 }
Winson Chunga12a2502010-12-20 14:41:35 -08001135
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 // Restore the AppsCustomize tab
1137 if (mAppsCustomizeTabHost != null) {
1138 String curTab = savedState.getString("apps_customize_currentTab");
1139 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001140 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001142 mAppsCustomizeContent.loadAssociatedPages(
1143 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 }
1145
Winson Chung5afbf7b2011-07-25 11:53:08 -07001146 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1147 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001148 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 }
1150
1151 /**
1152 * Finds all the views we need and configure them properly.
1153 */
1154 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001155 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001156
Craig Mautner360310b2012-10-26 15:13:08 -07001157 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001158 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1159 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001160
John Spurlock77e1f472013-09-11 10:09:51 -04001161 mLauncherView.setSystemUiVisibility(
1162 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001163 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164
Winson Chung4c98d922011-05-31 16:50:48 -07001165 // Setup the drag layer
1166 mDragLayer.setup(this, dragController);
1167
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 // Setup the hotseat
1169 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1170 if (mHotseat != null) {
1171 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001172 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001173 }
1174
Adam Cohenf358a4b2013-07-23 16:47:31 -07001175 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001176 View widgetButton = findViewById(R.id.widget_button);
1177 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001178 @Override
1179 public void onClick(View arg0) {
Winson Chung82963d52013-10-09 11:20:57 -07001180 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001181 }
1182 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001183 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1184
1185 View wallpaperButton = findViewById(R.id.wallpaper_button);
1186 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001187 @Override
1188 public void onClick(View arg0) {
1189 startWallpaper();
1190 }
1191 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001192 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1193
1194 View settingsButton = findViewById(R.id.settings_button);
1195 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001196 @Override
1197 public void onClick(View arg0) {
1198 startSettings();
1199 }
1200 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001201 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001202 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001203
Winson Chung4c98d922011-05-31 16:50:48 -07001204 // Setup the workspace
1205 mWorkspace.setHapticFeedbackEnabled(false);
1206 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001207 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001208 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001209
Winson Chungf0ea4d32011-06-06 14:27:16 -07001210 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001211 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001212
Winson Chungf0ea4d32011-06-06 14:27:16 -07001213 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001214 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001215 mAppsCustomizeContent = (AppsCustomizePagedView)
1216 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1217 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001218
Winson Chung3d503fb2011-07-13 17:25:49 -07001219 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001220 dragController.setDragScoller(mWorkspace);
1221 dragController.setScrollView(mDragLayer);
1222 dragController.setMoveTarget(mWorkspace);
1223 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001224 if (mSearchDropTargetBar != null) {
1225 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001226 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001227
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001228 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001229 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001230 mWeightWatcher = new WeightWatcher(this);
1231 mWeightWatcher.setAlpha(0.5f);
1232 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001233 new FrameLayout.LayoutParams(
1234 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001235 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001236 Gravity.BOTTOM)
1237 );
Adam Cohen39a06042013-07-19 14:30:12 -07001238
1239 boolean show = shouldShowWeightWatcher();
1240 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
1244 /**
1245 * Creates a view representing a shortcut.
1246 *
1247 * @param info The data structure describing the shortcut.
1248 *
1249 * @return A View inflated from R.layout.application.
1250 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001251 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001253 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255
1256 /**
1257 * Creates a view representing a shortcut inflated from the specified resource.
1258 *
1259 * @param layoutResId The id of the XML layout used to create the shortcut.
1260 * @param parent The group the shortcut belongs to.
1261 * @param info The data structure describing the shortcut.
1262 *
1263 * @return A View inflated from layoutResId.
1264 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001265 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001266 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1267 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 return favorite;
1270 }
1271
1272 /**
1273 * Add an application shortcut to the workspace.
1274 *
1275 * @param data The intent describing the application.
1276 * @param cellInfo The position on screen where to create the shortcut.
1277 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001278 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001280 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001281
Adam Cohenfbba09b2011-07-18 21:43:05 -07001282 // First we check if we already know the exact location where we want to add this item.
1283 if (cellX >= 0 && cellY >= 0) {
1284 cellXY[0] = cellX;
1285 cellXY[1] = cellY;
1286 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001287 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001288 return;
1289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001291 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001292
Romain Guy73b979d2009-06-09 12:57:21 -07001293 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001294 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001296 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001297 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001298 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001299 } else {
1300 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
Romain Guycbb89e42009-06-08 15:52:54 -07001303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 /**
1305 * Add a shortcut to the workspace.
1306 *
1307 * @param data The intent describing the shortcut.
1308 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001310 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 int cellY) {
1312 int[] cellXY = mTmpAddItemCellCoordinates;
1313 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Michael Jurkad3ef3062010-11-23 16:23:58 -08001316 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001317
Adam Cohen558baaf2011-08-15 15:22:57 -07001318 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001319 if (info == null) {
1320 return;
1321 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001322 final View view = createShortcut(info);
1323
Adam Cohenfbba09b2011-07-18 21:43:05 -07001324 // First we check if we already know the exact location where we want to add this item.
1325 if (cellX >= 0 && cellY >= 0) {
1326 cellXY[0] = cellX;
1327 cellXY[1] = cellY;
1328 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001329
1330 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001331 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001332 true, null,null)) {
1333 return;
1334 }
1335 DragObject dragObject = new DragObject();
1336 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001337 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1338 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001339 return;
1340 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001341 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001342 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001344 foundCellSpan = (result != null);
1345 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001346 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001347 }
1348
1349 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001350 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 return;
1352 }
1353
Adam Cohendcd297f2013-06-18 13:13:40 -07001354 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
1356 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360 }
1361
Adam Cohen2f093b62012-04-30 18:59:53 -07001362 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1363 int minHeight) {
1364 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001365 // We want to account for the extra amount of padding that we are adding to the widget
1366 // to ensure that it gets the full amount of space that it has requested
1367 int requiredWidth = minWidth + padding.left + padding.right;
1368 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001369 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001370 }
1371
Adam Cohen2f093b62012-04-30 18:59:53 -07001372 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1373 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001374 }
1375
Adam Cohen2f093b62012-04-30 18:59:53 -07001376 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1377 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001378 }
1379
Adam Cohen2f093b62012-04-30 18:59:53 -07001380 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1381 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001382 }
1383
Adam Cohen2f093b62012-04-30 18:59:53 -07001384 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1385 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001386 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001390 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001392 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001393 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001395 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001396 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1397 if (appWidgetInfo == null) {
1398 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001401 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001403
Adam Cohen2f093b62012-04-30 18:59:53 -07001404 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1405 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001408 // We have saved the position to which the widget was dragged-- this really only matters
1409 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 int[] cellXY = mTmpAddItemCellCoordinates;
1411 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001412 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001413 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1415 cellXY[0] = mPendingAddInfo.cellX;
1416 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001417 spanXY[0] = mPendingAddInfo.spanX;
1418 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001419 foundCellSpan = true;
1420 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001421 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001423 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1424 spanXY[1], cellXY, finalSpan);
1425 spanXY[0] = finalSpan[0];
1426 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001427 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001429 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001430 }
1431
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001433 if (appWidgetId != -1) {
1434 // Deleting an app widget ID is a void call but writes to disk before returning
1435 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001436 new Thread("deleteAppWidgetId") {
1437 public void run() {
1438 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1439 }
1440 }.start();
1441 }
Winson Chung93eef082012-03-23 15:59:27 -07001442 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001443 return;
1444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001446 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001447 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1448 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001449 launcherInfo.spanX = spanXY[0];
1450 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001451 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1452 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001455 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
1457 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001458 if (hostView == null) {
1459 // Perform actual inflation because we're live
1460 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1461 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1462 } else {
1463 // The AppWidgetHostView has already been inflated and instantiated
1464 launcherInfo.hostView = hostView;
1465 }
Romain Guycbb89e42009-06-08 15:52:54 -07001466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001468 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001469 launcherInfo.notifyWidgetSizeChanged(this);
1470
Adam Cohendcd297f2013-06-18 13:13:40 -07001471 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001473
1474 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001476 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Adam Cohended9f8d2010-11-03 13:25:16 -07001479 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1480 @Override
1481 public void onReceive(Context context, Intent intent) {
1482 final String action = intent.getAction();
1483 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1484 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001485 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001486 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001487
Winson Chungc100e8e2011-08-09 16:02:43 -07001488 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001489 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001490 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001491 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001492 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1494 mUserPresent = true;
1495 updateRunning();
1496 }
1497 }
1498 };
1499
1500 @Override
1501 public void onAttachedToWindow() {
1502 super.onAttachedToWindow();
1503
1504 // Listen for broadcasts related to user-presence
1505 final IntentFilter filter = new IntentFilter();
1506 filter.addAction(Intent.ACTION_SCREEN_OFF);
1507 filter.addAction(Intent.ACTION_USER_PRESENT);
1508 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001509 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001510 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001511 mVisible = true;
1512 }
1513
1514 @Override
1515 public void onDetachedFromWindow() {
1516 super.onDetachedFromWindow();
1517 mVisible = false;
1518
Adam Cohend113e0c2010-11-11 10:48:05 -08001519 if (mAttached) {
1520 unregisterReceiver(mReceiver);
1521 mAttached = false;
1522 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001523 updateRunning();
1524 }
1525
1526 public void onWindowVisibilityChanged(int visibility) {
1527 mVisible = visibility == View.VISIBLE;
1528 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001529 // The following code used to be in onResume, but it turns out onResume is called when
1530 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1531 // is a more appropriate event to handle
1532 if (mVisible) {
1533 mAppsCustomizeTabHost.onWindowVisible();
1534 if (!mWorkspaceLoading) {
1535 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001536 // We want to let Launcher draw itself at least once before we force it to build
1537 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001538 // apps is nice and speedy.
1539 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001540 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001541 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001542 if (mStarted) return;
1543 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001544 // We delay the layer building a bit in order to give
1545 // other message processing a time to run. In particular
1546 // this avoids a delay in hiding the IME if it was
1547 // currently shown, because doing that may involve
1548 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001549 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001550 final ViewTreeObserver.OnDrawListener listener = this;
1551 mWorkspace.post(new Runnable() {
1552 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001553 if (mWorkspace != null &&
1554 mWorkspace.getViewTreeObserver() != null) {
1555 mWorkspace.getViewTreeObserver().
1556 removeOnDrawListener(listener);
1557 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001558 }
1559 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001560 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001561 }
1562 });
1563 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001564 // When Launcher comes back to foreground, a different Activity might be responsible for
1565 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001566 if (!DISABLE_MARKET_BUTTON) {
1567 updateAppMarketIcon();
1568 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001569 clearTypedText();
1570 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 }
1572
1573 private void sendAdvanceMessage(long delay) {
1574 mHandler.removeMessages(ADVANCE_MSG);
1575 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1576 mHandler.sendMessageDelayed(msg, delay);
1577 mAutoAdvanceSentTime = System.currentTimeMillis();
1578 }
1579
1580 private void updateRunning() {
1581 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1582 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1583 mAutoAdvanceRunning = autoAdvanceRunning;
1584 if (autoAdvanceRunning) {
1585 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1586 sendAdvanceMessage(delay);
1587 } else {
1588 if (!mWidgetsToAdvance.isEmpty()) {
1589 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1590 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1591 }
1592 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001593 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595 }
1596 }
1597
1598 private final Handler mHandler = new Handler() {
1599 @Override
1600 public void handleMessage(Message msg) {
1601 if (msg.what == ADVANCE_MSG) {
1602 int i = 0;
1603 for (View key: mWidgetsToAdvance.keySet()) {
1604 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1605 final int delay = mAdvanceStagger * i;
1606 if (v instanceof Advanceable) {
1607 postDelayed(new Runnable() {
1608 public void run() {
1609 ((Advanceable) v).advance();
1610 }
1611 }, delay);
1612 }
1613 i++;
1614 }
1615 sendAdvanceMessage(mAdvanceInterval);
1616 }
1617 }
1618 };
1619
1620 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001621 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1623 if (v instanceof Advanceable) {
1624 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001625 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 updateRunning();
1627 }
1628 }
1629
1630 void removeWidgetToAutoAdvance(View hostView) {
1631 if (mWidgetsToAdvance.containsKey(hostView)) {
1632 mWidgetsToAdvance.remove(hostView);
1633 updateRunning();
1634 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001635 }
1636
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001639 launcherInfo.hostView = null;
1640 }
1641
Winson Chung93eef082012-03-23 15:59:27 -07001642 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1643 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1644 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001647 public LauncherAppWidgetHost getAppWidgetHost() {
1648 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Winson Chunga9abd0e2010-10-27 17:18:37 -07001651 public LauncherModel getModel() {
1652 return mModel;
1653 }
1654
Bjorn Bringertc459e522013-06-07 19:36:01 +01001655 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001656 getWindow().closeAllPanels();
1657
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001658 // Whatever we were doing is hereby canceled.
1659 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 @Override
1663 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001664 long startTime = 0;
1665 if (DEBUG_RESUME_TIME) {
1666 startTime = System.currentTimeMillis();
1667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 super.onNewIntent(intent);
1669
1670 // Close the menu
1671 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001672 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001673 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001674
Adam Cohened307df2013-10-02 09:37:31 -07001675 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1676 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1677 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001678
Adam Cohen6fecd412013-10-02 17:41:50 -07001679 if (mWorkspace == null) {
1680 // Can be cases where mWorkspace is null, this prevents a NPE
1681 return;
1682 }
1683 Folder openFolder = mWorkspace.getOpenFolder();
1684 // In all these cases, only animate if we're already on home
1685 mWorkspace.exitWidgetResizeMode();
1686 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1687 openFolder == null) {
1688 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001689 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001690
Adam Cohen6fecd412013-10-02 17:41:50 -07001691 closeFolder();
1692 exitSpringLoadedDragMode();
1693
1694 // If we are already on home, then just animate back to the workspace,
1695 // otherwise, just wait until onResume to set the state back to Workspace
1696 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001697 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001698 } else {
1699 mOnResumeState = State.WORKSPACE;
1700 }
1701
1702 final View v = getWindow().peekDecorView();
1703 if (v != null && v.getWindowToken() != null) {
1704 InputMethodManager imm = (InputMethodManager)getSystemService(
1705 INPUT_METHOD_SERVICE);
1706 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1707 }
1708
1709 // Reset the apps customize page
1710 if (mAppsCustomizeTabHost != null) {
1711 mAppsCustomizeTabHost.reset();
1712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Adam Cohened307df2013-10-02 09:37:31 -07001714
Michael Jurka447bf842013-05-15 14:52:15 +02001715 if (DEBUG_RESUME_TIME) {
1716 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719
1720 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001721 public void onRestoreInstanceState(Bundle state) {
1722 super.onRestoreInstanceState(state);
1723 for (int page: mSynchronouslyBoundPages) {
1724 mWorkspace.restoreInstanceStateForChild(page);
1725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727
1728 @Override
1729 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001730 if (mWorkspace.getChildCount() > 0) {
1731 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getRestorePage());
1732 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001733 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734
Michael Jurka883f55b2010-10-21 15:47:14 -07001735 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001736 // We close any open folder since it will not be re-opened, and we need to make sure
1737 // this state is reflected.
1738 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739
Adam Cohendcd297f2013-06-18 13:13:40 -07001740 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001741 mWaitingForResult) {
1742 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001743 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001744 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1745 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001746 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1747 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1748 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750
1751 if (mFolderInfo != null && mWaitingForResult) {
1752 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1753 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1754 }
Michael Jurka946ad472010-07-09 18:05:18 -07001755
Winson Chung785d2eb2011-04-14 16:08:02 -07001756 // Save the current AppsCustomize tab
1757 if (mAppsCustomizeTabHost != null) {
1758 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1759 if (currentTabTag != null) {
1760 outState.putString("apps_customize_currentTab", currentTabTag);
1761 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001762 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1763 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766
1767 @Override
1768 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001770
Winson Chunge7a03942011-08-05 15:05:12 -07001771 // Remove all pending runnables
1772 mHandler.removeMessages(ADVANCE_MSG);
1773 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001774 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001775
Winson Chungcd2b0142011-06-08 16:02:26 -07001776 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001777 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001778 mModel.stopLoader();
1779 app.setLauncher(null);
1780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001782 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001784 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001786 mAppWidgetHost = null;
1787
1788 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789
1790 TextKeyListener.getInstance().release();
1791
Winson Chung81b52252012-08-27 15:34:29 -07001792 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1793 // to prevent leaking Launcher activities on orientation change.
1794 if (mModel != null) {
1795 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1796 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001797
1798 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001799 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001800
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001801 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001802 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1803 mWorkspace.removeAllViews();
1804 mWorkspace = null;
1805 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001806
Michael Jurka2ecf9952012-06-18 12:52:28 -07001807 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
1809
Adam Cohena9cf38f2011-05-02 15:36:58 -07001810 public DragController getDragController() {
1811 return mDragController;
1812 }
1813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 @Override
1815 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001816 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 super.startActivityForResult(intent, requestCode);
1818 }
1819
Winson Chung70d72102011-08-12 11:24:35 -07001820 /**
1821 * Indicates that we want global search for this activity by setting the globalSearch
1822 * argument for {@link #startSearch} to true.
1823 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001825 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001827
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001828 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001829
Karl Rosaen138a0412009-04-23 19:00:21 -07001830 if (initialQuery == null) {
1831 // Use any text typed in the launcher as the initial query
1832 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 if (appSearchData == null) {
1835 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001836 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Winson Chungadf0c182012-08-23 14:59:07 -07001838 Rect sourceBounds = new Rect();
1839 if (mSearchDropTargetBar != null) {
1840 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1841 }
Romain Guycbb89e42009-06-08 15:52:54 -07001842
Bjorn Bringertc459e522013-06-07 19:36:01 +01001843 startSearch(initialQuery, selectInitialQuery,
1844 appSearchData, sourceBounds);
1845 }
1846
1847 public void startSearch(String initialQuery,
1848 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001849 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001850 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001851 }
1852
1853 /**
1854 * Starts the global search activity. This code is a copied from SearchManager
1855 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001856 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001857 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001858 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001859 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1860 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1861 if (globalSearchActivity == null) {
1862 Log.w(TAG, "No global search activity found.");
1863 return;
1864 }
1865 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1866 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1867 intent.setComponent(globalSearchActivity);
1868 // Make sure that we have a Bundle to put source in
1869 if (appSearchData == null) {
1870 appSearchData = new Bundle();
1871 } else {
1872 appSearchData = new Bundle(appSearchData);
1873 }
1874 // Set source to package name of app that starts global search, if not set already.
1875 if (!appSearchData.containsKey("source")) {
1876 appSearchData.putString("source", getPackageName());
1877 }
1878 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1879 if (!TextUtils.isEmpty(initialQuery)) {
1880 intent.putExtra(SearchManager.QUERY, initialQuery);
1881 }
1882 if (selectInitialQuery) {
1883 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1884 }
1885 intent.setSourceBounds(sourceBounds);
1886 try {
1887 startActivity(intent);
1888 } catch (ActivityNotFoundException ex) {
1889 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892
Winson Chung70d72102011-08-12 11:24:35 -07001893 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001894 public boolean onPrepareOptionsMenu(Menu menu) {
1895 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001896 if (!mWorkspace.isInOverviewMode()) {
1897 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001898 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001899 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001902 @Override
1903 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001904 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001905 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001906 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001907 }
1908
Joe Onorato9c1289c2009-08-17 11:03:03 -04001909 public boolean isWorkspaceLocked() {
1910 return mWorkspaceLoading || mWaitingForResult;
1911 }
1912
Michael Jurka0280c3b2010-09-17 15:00:07 -07001913 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1917 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001918 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001920 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001921
Adam Cohenad4e15c2013-10-17 16:21:35 -07001922 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1923 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
1924 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
1925 }
1926
1927 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
1928 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
1929 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001930 if (appWidgetInfo.configure != null) {
1931 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001932
Bjorn Bringert7984c942009-12-09 15:38:25 +00001933 // Launch over to configure widget, if needed
1934 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001935 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001936 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001937 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001939 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07001940 Runnable onComplete = new Runnable() {
1941 @Override
1942 public void run() {
1943 // Exit spring loaded mode if necessary after adding the widget
1944 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1945 null);
1946 }
1947 };
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001949 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001950 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952 }
Romain Guycbb89e42009-06-08 15:52:54 -07001953
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001954 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07001955 // Close any folders that may be open.
1956 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01001957 mWorkspace.moveToCustomContentScreen(animate);
1958 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959 /**
1960 * Process a shortcut drop.
1961 *
1962 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001964 * @param cell The cell it should be added to, optional
1965 * @param position The location on the screen where it was dropped, optional
1966 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001968 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001969 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001972 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001973
1974 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 mPendingAddInfo.cellX = cell[0];
1976 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001977 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001978
1979 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1980 createShortcutIntent.setComponent(componentName);
1981 processShortcut(createShortcutIntent);
1982 }
1983
Adam Cohenfbba09b2011-07-18 21:43:05 -07001984 /**
1985 * Process a widget drop.
1986 *
1987 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001989 * @param cell The cell it should be added to, optional
1990 * @param position The location on the screen where it was dropped, optional
1991 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001993 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001994 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001995 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001996 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001998 mPendingAddInfo.minSpanX = info.minSpanX;
1999 mPendingAddInfo.minSpanY = info.minSpanY;
2000
Adam Cohenfbba09b2011-07-18 21:43:05 -07002001 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 mPendingAddInfo.cellX = cell[0];
2003 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002004 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002005 if (span != null) {
2006 mPendingAddInfo.spanX = span[0];
2007 mPendingAddInfo.spanY = span[1];
2008 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002009
Adam Cohened66b2b2012-01-23 17:28:51 -08002010 AppWidgetHostView hostView = info.boundWidget;
2011 int appWidgetId;
2012 if (hostView != null) {
2013 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002014 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002015 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002016 // In this case, we either need to start an activity to get permission to bind
2017 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002018 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002019 Bundle options = info.bindOptions;
2020
2021 boolean success = false;
2022 if (options != null) {
2023 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2024 info.componentName, options);
2025 } else {
2026 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2027 info.componentName);
2028 }
2029 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002030 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002031 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002032 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002033 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2034 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2035 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002036 // TODO: we need to make sure that this accounts for the options bundle.
2037 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002038 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2039 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002040 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002041 }
2042
Joe Onoratodeb98af2010-02-19 14:59:39 -08002043 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002044 // Handle case where user selected "Applications"
2045 String applicationName = getResources().getString(R.string.group_applications);
2046 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002047
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002048 if (applicationName != null && applicationName.equals(shortcutName)) {
2049 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2050 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002051
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2053 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002054 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002055 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002056 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002057 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002058 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060
Winson Chung24ab2f12010-09-16 14:10:47 -07002061 void processWallpaper(Intent intent) {
2062 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2063 }
2064
Adam Cohendcd297f2013-06-18 13:13:40 -07002065 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002066 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002067 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 folderInfo.title = getText(R.string.folder_name);
2069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002071 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002072 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002073 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074
2075 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002076 FolderIcon newFolder =
2077 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002078 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002080 // Force measure the new folder icon
2081 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2082 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002083 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
Romain Guycbb89e42009-06-08 15:52:54 -07002085
Joe Onorato9c1289c2009-08-17 11:03:03 -04002086 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002087 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 }
2089
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002090 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002092 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002093 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002096 protected ComponentName getWallpaperPickerComponent() {
2097 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2098 }
2099
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002100 /**
2101 * Registers various content observers. The current implementation registers
2102 * only a favorites observer to keep track of the favorites applications.
2103 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002104 private void registerContentObservers() {
2105 ContentResolver resolver = getContentResolver();
2106 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2107 true, mWidgetObserver);
2108 }
2109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 @Override
2111 public boolean dispatchKeyEvent(KeyEvent event) {
2112 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2113 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002115 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002116 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002117 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002118 dumpState();
2119 return true;
2120 }
2121 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002122 }
2123 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2124 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002125 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 return true;
2127 }
2128 }
2129
2130 return super.dispatchKeyEvent(event);
2131 }
2132
Joe Onorato88ec0992009-11-19 13:16:06 -08002133 @Override
2134 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002135 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002136 if (mAppsCustomizeContent.getContentType() ==
2137 AppsCustomizePagedView.ContentType.Applications) {
2138 showWorkspace(true);
2139 } else {
2140 showOverviewMode(true);
2141 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002142 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002143 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002144 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002145 Folder openFolder = mWorkspace.getOpenFolder();
2146 if (openFolder.isEditingName()) {
2147 openFolder.dismissEditingName();
2148 } else {
2149 closeFolder();
2150 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002151 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002152 mWorkspace.exitWidgetResizeMode();
2153
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002154 // Back button is a no-op here, but give at least some feedback for the button press
2155 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002156 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002157 }
2158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002160 * Re-listen when widgets are reset.
2161 */
2162 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002163 if (mAppWidgetHost != null) {
2164 mAppWidgetHost.startListening();
2165 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002166 }
2167
2168 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 * Launches the intent referred by the clicked shortcut.
2170 *
2171 * @param v The view representing the clicked shortcut.
2172 */
2173 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002174 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2175 // view has detached (it's possible for this to happen if the view is removed mid touch).
2176 if (v.getWindowToken() == null) {
2177 return;
2178 }
2179
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002180 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002181 return;
2182 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002183
Adam Cohen1697b792013-09-17 19:08:21 -07002184 if (v instanceof Workspace) {
2185 if (mWorkspace.isInOverviewMode()) {
2186 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002187 }
2188 return;
2189 }
2190
2191 if (v instanceof CellLayout) {
2192 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002193 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002194 }
2195 }
2196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002198 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002200 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2201 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002202
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002203 // Check for special shortcuts
2204 if (intent.getComponent() != null) {
2205 final String shortcutClass = intent.getComponent().getClassName();
2206
2207 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002208 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002209 return;
2210 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2211 MemoryDumpActivity.startDump(this);
2212 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002213 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2214 toggleShowWeightWatcher();
2215 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002216 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002217 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002218
2219 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002220 int[] pos = new int[2];
2221 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002222 intent.setSourceBounds(new Rect(pos[0], pos[1],
2223 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002224
2225 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002226
Daniel Sandlerff02d492013-08-05 02:12:05 -04002227 mStats.recordLaunch(intent, shortcut);
2228
Michael Jurkaddd62e92011-02-16 17:49:14 -08002229 if (success && v instanceof BubbleTextView) {
2230 mWaitingForResume = (BubbleTextView) v;
2231 mWaitingForResume.setStayPressed(true);
2232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002234 if (v instanceof FolderIcon) {
2235 FolderIcon fi = (FolderIcon) v;
2236 handleFolderClick(fi);
2237 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002238 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002239 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002240 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002241 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002242 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245 }
2246
Michael Jurka0e260592010-06-30 17:07:39 -07002247 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002248 return false;
2249 }
2250
Michael Jurkaaf442092010-06-10 17:01:57 -07002251 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002252 * Event handler for the search button
2253 *
2254 * @param v The view that was clicked.
2255 */
2256 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002257 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2258
Amith Yamasania135ba82011-08-09 17:42:01 -07002259 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002260 }
2261
2262 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002263 * Event handler for the voice button
2264 *
2265 * @param v The view that was clicked.
2266 */
2267 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002268 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002269
Bjorn Bringertc459e522013-06-07 19:36:01 +01002270 startVoice();
2271 }
2272
2273 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002274 try {
2275 final SearchManager searchManager =
2276 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2277 ComponentName activityName = searchManager.getGlobalSearchActivity();
2278 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002279 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002280 if (activityName != null) {
2281 intent.setPackage(activityName.getPackageName());
2282 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002283 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002284 } catch (ActivityNotFoundException e) {
2285 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002286 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002287 startActivitySafely(null, intent, "onClickVoiceButton");
2288 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002289 }
2290
2291 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002292 * Event handler for the "grid" button that appears on the home screen, which
2293 * enters all apps mode.
2294 *
2295 * @param v The view that was clicked.
2296 */
2297 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002298 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002299 }
2300
2301 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002302 // Provide the same haptic feedback that the system offers for virtual keys.
2303 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002304 }
2305
Adam Cohen61f560d2013-09-30 15:58:20 -07002306 public void performHapticFeedbackOnTouchDown(View v) {
2307 // Provide the same haptic feedback that the system offers for virtual keys.
2308 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2309 }
2310
2311 public View.OnTouchListener getHapticFeedbackTouchListener() {
2312 if (mHapticFeedbackTouchListener == null) {
2313 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2314 @Override
2315 public boolean onTouch(View v, MotionEvent event) {
2316 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2317 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2318 }
2319 return false;
2320 }
2321 };
2322 }
2323 return mHapticFeedbackTouchListener;
2324 }
2325
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002326 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002327 if (!DISABLE_MARKET_BUTTON) {
2328 if (mAppMarketIntent != null) {
2329 startActivitySafely(v, mAppMarketIntent, "app market");
2330 } else {
2331 Log.e(TAG, "Invalid app market intent.");
2332 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002333 }
2334 }
2335
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002336 /**
2337 * Called when the user stops interacting with the launcher.
2338 * This implies that the user is now on the homescreen and is not doing housekeeping.
2339 */
2340 protected void onInteractionEnd() {}
2341
2342 /**
2343 * Called when the user starts interacting with the launcher.
2344 * The possible interactions are:
2345 * - open all apps
2346 * - reorder an app shortcut, or a widget
2347 * - open the overview mode.
2348 * This is a good time to stop doing things that only make sense
2349 * when the user is on the homescreen and not doing housekeeping.
2350 */
2351 protected void onInteractionBegin() {}
2352
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002353 void startApplicationDetailsActivity(ComponentName componentName) {
2354 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002355 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2356 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002357 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002358 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002359 }
2360
Michael Jurka1e2f4652013-07-08 18:03:46 -07002361 // returns true if the activity was started
2362 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002363 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002364 // System applications cannot be installed. For now, show a toast explaining that.
2365 // We may give them the option of disabling apps this way.
2366 int messageId = R.string.uninstall_system_app_text;
2367 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002368 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002369 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002370 String packageName = componentName.getPackageName();
2371 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002372 Intent intent = new Intent(
2373 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002374 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2375 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002376 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002377 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002378 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002379 }
2380
Michael Jurka86a720e2012-05-09 11:23:23 -07002381 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002385 // Only launch using the new animation if the shortcut has not opted out (this is a
2386 // private contract between launcher and may be ignored in the future).
2387 boolean useLaunchAnimation = (v != null) &&
2388 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2389 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002390 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2391 v.getMeasuredWidth(), v.getMeasuredHeight());
2392
2393 startActivity(intent, opts.toBundle());
2394 } else {
2395 startActivity(intent);
2396 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002397 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 } catch (SecurityException e) {
2399 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002400 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002402 "or use the exported attribute for this activity. "
2403 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002405 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002407
Michael Jurka86a720e2012-05-09 11:23:23 -07002408 boolean startActivitySafely(View v, Intent intent, Object tag) {
2409 boolean success = false;
2410 try {
2411 success = startActivity(v, intent, tag);
2412 } catch (ActivityNotFoundException e) {
2413 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2414 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2415 }
2416 return success;
2417 }
2418
Adam Cohena9cf38f2011-05-02 15:36:58 -07002419 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002420 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002421 Folder openFolder = mWorkspace.getFolderForTag(info);
2422
2423 // If the folder info reports that the associated folder is open, then verify that
2424 // it is actually opened. There have been a few instances where this gets out of sync.
2425 if (info.opened && openFolder == null) {
2426 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002428 info.opened = false;
2429 }
2430
Adam Cohenfb91f302012-06-11 15:45:18 -07002431 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 // Close any open folder
2433 closeFolder();
2434 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002435 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 } else {
2437 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 int folderScreen;
2439 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002440 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 // .. and close it
2442 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002443 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 // Close any folder open on the current screen
2445 closeFolder();
2446 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002447 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 }
2449 }
2450 }
2451 }
2452
Adam Cohen268c4752012-06-06 17:47:33 -07002453 /**
2454 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2455 * in the DragLayer in the exact absolute location of the original FolderIcon.
2456 */
2457 private void copyFolderIconToImage(FolderIcon fi) {
2458 final int width = fi.getMeasuredWidth();
2459 final int height = fi.getMeasuredHeight();
2460
2461 // Lazy load ImageView, Bitmap and Canvas
2462 if (mFolderIconImageView == null) {
2463 mFolderIconImageView = new ImageView(this);
2464 }
2465 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2466 mFolderIconBitmap.getHeight() != height) {
2467 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2468 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2469 }
2470
2471 DragLayer.LayoutParams lp;
2472 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2473 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2474 } else {
2475 lp = new DragLayer.LayoutParams(width, height);
2476 }
2477
Adam Cohen307fe232012-08-16 17:55:58 -07002478 // The layout from which the folder is being opened may be scaled, adjust the starting
2479 // view size by this scale factor.
2480 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002481 lp.customPosition = true;
2482 lp.x = mRectForFolderAnimation.left;
2483 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002484 lp.width = (int) (scale * width);
2485 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002486
2487 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2488 fi.draw(mFolderIconCanvas);
2489 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002490 if (fi.getFolder() != null) {
2491 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2492 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002493 }
Adam Cohen268c4752012-06-06 17:47:33 -07002494 // Just in case this image view is still in the drag layer from a previous animation,
2495 // we remove it and re-add it.
2496 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2497 mDragLayer.removeView(mFolderIconImageView);
2498 }
2499 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002500 if (fi.getFolder() != null) {
2501 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002502 }
Adam Cohen268c4752012-06-06 17:47:33 -07002503 }
2504
Adam Cohen2801caf2011-05-13 20:57:39 -07002505 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002506 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002507 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2508 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2509 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2510
Adam Cohenc51934b2011-07-26 21:07:43 -07002511 FolderInfo info = (FolderInfo) fi.getTag();
2512 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2513 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002514 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2515 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002516 }
2517
Adam Cohen268c4752012-06-06 17:47:33 -07002518 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2519 copyFolderIconToImage(fi);
2520 fi.setVisibility(View.INVISIBLE);
2521
Michael Jurka2ecf9952012-06-18 12:52:28 -07002522 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002523 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002524 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2525 oa.start();
2526 }
2527
Adam Cohen268c4752012-06-06 17:47:33 -07002528 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002529 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002530 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2531 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2532 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2533
Adam Cohen268c4752012-06-06 17:47:33 -07002534 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002535
Adam Cohen268c4752012-06-06 17:47:33 -07002536 // We remove and re-draw the FolderIcon in-case it has changed
2537 mDragLayer.removeView(mFolderIconImageView);
2538 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002539 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002540 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002541 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002542 oa.addListener(new AnimatorListenerAdapter() {
2543 @Override
2544 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002545 if (cl != null) {
2546 cl.clearFolderLeaveBehind();
2547 // Remove the ImageView copy of the FolderIcon and make the original visible.
2548 mDragLayer.removeView(mFolderIconImageView);
2549 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002550 }
2551 }
2552 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002553 oa.start();
2554 }
2555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002557 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 * is animated relative to the specified View. If the View is null, no animation
2559 * is played.
2560 *
2561 * @param folderInfo The FolderInfo describing the folder to open.
2562 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002563 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002564 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002565 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566
Adam Cohena9cf38f2011-05-02 15:36:58 -07002567 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568
Adam Cohen4554ee12011-08-03 16:13:21 -07002569 // Just verify that the folder hasn't already been added to the DragLayer.
2570 // There was a one-off crash where the folder had a parent already.
2571 if (folder.getParent() == null) {
2572 mDragLayer.addView(folder);
2573 mDragController.addDropTarget((DropTarget) folder);
2574 } else {
2575 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2576 folder.getParent() + ").");
2577 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002578 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002579 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002580
2581 // Notify the accessibility manager that this folder "window" has appeared and occluded
2582 // the workspace items
2583 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2584 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002585 }
2586
2587 public void closeFolder() {
2588 Folder folder = mWorkspace.getOpenFolder();
2589 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002590 if (folder.isEditingName()) {
2591 folder.dismissEditingName();
2592 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002593 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002594
2595 // Dismiss the folder cling
2596 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002597 }
2598 }
2599
2600 void closeFolder(Folder folder) {
2601 folder.getInfo().opened = false;
2602
2603 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2604 if (parent != null) {
2605 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2606 shrinkAndFadeInFolderIcon(fi);
2607 }
2608 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002609
2610 // Notify the accessibility manager that this folder "window" has disappeard and no
2611 // longer occludeds the workspace items
2612 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 }
2614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002616 if (!isDraggingEnabled()) return false;
2617 if (isWorkspaceLocked()) return false;
2618 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619
Adam Cohen1697b792013-09-17 19:08:21 -07002620 if (v instanceof Workspace) {
2621 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002622 if (mWorkspace.enterOverviewMode()) {
2623 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2624 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2625 return true;
2626 } else {
2627 return false;
2628 }
Adam Cohen1697b792013-09-17 19:08:21 -07002629 }
Adam Cohen1697b792013-09-17 19:08:21 -07002630 }
2631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002632 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002633 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 }
2635
Michael Jurka0280c3b2010-09-17 15:00:07 -07002636 resetAddInfo();
2637 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002639 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 return true;
2641 }
2642
Winson Chung3d503fb2011-07-13 17:25:49 -07002643 // The hotseat touch handling does not go through Workspace, and we always allow long press
2644 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002645 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002646 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2647 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002648 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002649 // User long pressed on empty space
2650 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2651 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2652 // Disabling reordering until we sort out some issues.
2653 if (mWorkspace.isInOverviewMode()) {
2654 mWorkspace.startReordering(v);
2655 } else {
2656 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002659 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002661 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 }
2663 }
2664 }
2665 return true;
2666 }
2667
Winson Chung3d503fb2011-07-13 17:25:49 -07002668 boolean isHotseatLayout(View layout) {
2669 return mHotseat != null && layout != null &&
2670 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2671 }
2672 Hotseat getHotseat() {
2673 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002674 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002675 View getOverviewPanel() {
2676 return mOverviewPanel;
2677 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002678 SearchDropTargetBar getSearchBar() {
2679 return mSearchDropTargetBar;
2680 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002681
Winson Chung3d503fb2011-07-13 17:25:49 -07002682 /**
2683 * Returns the CellLayout of the specified container at the specified screen.
2684 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002685 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002686 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2687 if (mHotseat != null) {
2688 return mHotseat.getLayout();
2689 } else {
2690 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002691 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002692 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002693 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002694 }
2695 }
2696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 Workspace getWorkspace() {
2698 return mWorkspace;
2699 }
2700
Daniel Sandler843e8602010-06-07 14:59:01 -04002701 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002702 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002703 }
2704
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 /**
2706 * Helper method for the cameraZoomIn/cameraZoomOut animations
2707 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 * @param scaleFactor The scale factor used for the zoom
2709 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002710 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002711 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002712 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002713 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002714
Craig Mautner360310b2012-10-26 15:13:08 -07002715 private void setWorkspaceBackground(boolean workspace) {
2716 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002717 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002718 }
2719
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002720 void updateWallpaperVisibility(boolean visible) {
2721 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2722 int curflags = getWindow().getAttributes().flags
2723 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2724 if (wpflags != curflags) {
2725 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2726 }
Craig Mautner360310b2012-10-26 15:13:08 -07002727 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002728 }
2729
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002730 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2731 if (v instanceof LauncherTransitionable) {
2732 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2733 }
2734 }
2735
Michael Jurkabed61d22012-02-14 22:51:29 -08002736 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2737 if (v instanceof LauncherTransitionable) {
2738 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2739 }
Winson Chung70442722012-02-10 15:43:22 -08002740
2741 // Update the workspace transition step as well
2742 dispatchOnLauncherTransitionStep(v, 0f);
2743 }
2744
2745 private void dispatchOnLauncherTransitionStep(View v, float t) {
2746 if (v instanceof LauncherTransitionable) {
2747 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2748 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002749 }
2750
2751 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2752 if (v instanceof LauncherTransitionable) {
2753 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2754 }
Winson Chung70442722012-02-10 15:43:22 -08002755
2756 // Update the workspace transition step as well
2757 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 }
2759
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002761 * Things to test when changing the following seven functions.
2762 * - Home from workspace
2763 * - from center screen
2764 * - from other screens
2765 * - Home from all apps
2766 * - from center screen
2767 * - from other screens
2768 * - Back from all apps
2769 * - from center screen
2770 * - from other screens
2771 * - Launch app from workspace and quit
2772 * - with back
2773 * - with home
2774 * - Launch app from all apps and quit
2775 * - with back
2776 * - with home
2777 * - Go to a screen that's not the default, then all
2778 * apps, and launch and app, and go back
2779 * - with back
2780 * -with home
2781 * - On workspace, long press power and go back
2782 * - with back
2783 * - with home
2784 * - On all apps, long press power and go back
2785 * - with back
2786 * - with home
2787 * - On workspace, power off
2788 * - On all apps, power off
2789 * - Launch an app and turn off the screen while in that app
2790 * - Go back with home key
2791 * - Go back with back key TODO: make this not go to workspace
2792 * - From all apps
2793 * - From workspace
2794 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2795 * - From all apps
2796 * - From the center workspace
2797 * - From another workspace
2798 */
2799
2800 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002801 * Zoom the camera out from the workspace to reveal 'toView'.
2802 * Assumes that the view to show is anchored at either the very top or very bottom
2803 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002804 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002805 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002806 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2807 showAppsCustomizeHelper(animated, springLoaded, contentType);
2808 }
2809 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2810 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002811 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002812 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002813 mStateAnimation.cancel();
2814 mStateAnimation = null;
2815 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002816 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002817
Winson Chungf0ea4d32011-06-06 14:27:16 -07002818 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2819 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2820 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002821 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002822 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002823 final int startDelay =
2824 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002825
Michael Jurkab3e22d92011-10-31 15:58:33 -07002826 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827
Michael Jurkad74c9842011-07-10 12:44:21 -07002828 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002829 Animator workspaceAnim =
2830 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002831 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2832 // Set the content type for the all apps space
2833 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2834 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002835
2836 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002837 toView.setScaleX(scale);
2838 toView.setScaleY(scale);
2839 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2840 scaleAnim.
2841 scaleX(1f).scaleY(1f).
2842 setDuration(duration).
2843 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002844
Winson Chungf0ea4d32011-06-06 14:27:16 -07002845 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002846 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002847 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002848 .ofFloat(toView, "alpha", 0f, 1f)
2849 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002850 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002851 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2852 @Override
2853 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002854 if (animation == null) {
2855 throw new RuntimeException("animation is null");
2856 }
Winson Chung70442722012-02-10 15:43:22 -08002857 float t = (Float) animation.getAnimatedValue();
2858 dispatchOnLauncherTransitionStep(fromView, t);
2859 dispatchOnLauncherTransitionStep(toView, t);
2860 }
2861 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002862
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002863 // toView should appear right at the end of the workspace shrink
2864 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002865 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002866 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002867 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002868
2869 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002870 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002871 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872 // Prepare the position
2873 toView.setTranslationX(0.0f);
2874 toView.setTranslationY(0.0f);
2875 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002876 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002877 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002878 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002879 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002880 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2881 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002882
Winson Chungc7d2b602012-05-16 17:02:20 -07002883 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002884 if (mSearchDropTargetBar != null) {
2885 mSearchDropTargetBar.hideSearchBar(false);
2886 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002887 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002888 });
2889
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002890 if (workspaceAnim != null) {
2891 mStateAnimation.play(workspaceAnim);
2892 }
Michael Jurka1899a362011-11-03 13:50:45 -07002893
2894 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002895
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002896 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2897 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002898
2899 // If any of the objects being animated haven't been measured/laid out
2900 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002901 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002902 (mWorkspace.getMeasuredWidth() == 0) ||
2903 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002904 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002905 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002906
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002907 final AnimatorSet stateAnimation = mStateAnimation;
2908 final Runnable startAnimRunnable = new Runnable() {
2909 public void run() {
2910 // Check that mStateAnimation hasn't changed while
2911 // we waited for a layout/draw pass
2912 if (mStateAnimation != stateAnimation)
2913 return;
2914 setPivotsForZoom(toView, scale);
2915 dispatchOnLauncherTransitionStart(fromView, animated, false);
2916 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002917 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002918 }
2919 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002920 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002921 final ViewTreeObserver observer = toView.getViewTreeObserver();
2922 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2923 public void onGlobalLayout() {
2924 startAnimRunnable.run();
2925 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2926 }
2927 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002928 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002929 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002930 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002931 } else {
2932 toView.setTranslationX(0.0f);
2933 toView.setTranslationY(0.0f);
2934 toView.setScaleX(1.0f);
2935 toView.setScaleY(1.0f);
2936 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002937 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002938
Daniel Sandlere4f98912013-06-25 15:13:26 -04002939 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002940 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002941 if (mSearchDropTargetBar != null) {
2942 mSearchDropTargetBar.hideSearchBar(false);
2943 }
Michael Jurkaabded662011-03-04 12:06:57 -08002944 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002945 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002946 dispatchOnLauncherTransitionStart(fromView, animated, false);
2947 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002948 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002949 dispatchOnLauncherTransitionStart(toView, animated, false);
2950 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002951 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002952 }
2953
2954 /**
2955 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002956 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002957 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002958 */
Adam Cohened307df2013-10-02 09:37:31 -07002959 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002961
Michael Jurkab3e22d92011-10-31 15:58:33 -07002962 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002963 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002964 mStateAnimation.cancel();
2965 mStateAnimation = null;
2966 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002967 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002968
Winson Chungf0ea4d32011-06-06 14:27:16 -07002969 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002970 final int fadeOutDuration =
2971 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002972 final float scaleFactor = (float)
2973 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2974 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002975 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002976 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07002977 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002978 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2979 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002980 toState, animated, stagger, -1);
2981 } else if (toState == Workspace.State.SPRING_LOADED ||
2982 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002983 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07002984 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002985 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002986
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07002988 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002989 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002990 final LauncherViewPropertyAnimator scaleAnim =
2991 new LauncherViewPropertyAnimator(fromView);
2992 scaleAnim.
2993 scaleX(scaleFactor).scaleY(scaleFactor).
2994 setDuration(duration).
2995 setInterpolator(new Workspace.ZoomInInterpolator());
2996
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002997 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002998 .ofFloat(fromView, "alpha", 1f, 0f)
2999 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003000 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003001 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3002 @Override
3003 public void onAnimationUpdate(ValueAnimator animation) {
3004 float t = 1f - (Float) animation.getAnimatedValue();
3005 dispatchOnLauncherTransitionStep(fromView, t);
3006 dispatchOnLauncherTransitionStep(toView, t);
3007 }
3008 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003009
Michael Jurka2ecf9952012-06-18 12:52:28 -07003010 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003011
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003012 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3013 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07003014 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003015
3016 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003017 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003018 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003019 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003020 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3021 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003022 if (onCompleteRunnable != null) {
3023 onCompleteRunnable.run();
3024 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003025 mAppsCustomizeContent.updateCurrentPageScroll();
3026 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003027 }
3028 });
3029
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003031 if (workspaceAnim != null) {
3032 mStateAnimation.play(workspaceAnim);
3033 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003034 dispatchOnLauncherTransitionStart(fromView, animated, true);
3035 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003036 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003037 } else {
3038 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003039 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003040 dispatchOnLauncherTransitionStart(fromView, animated, true);
3041 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003042 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003043 dispatchOnLauncherTransitionStart(toView, animated, true);
3044 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003045 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003046 }
3047
Michael Jurkae326f182011-11-21 14:05:46 -08003048 @Override
3049 public void onTrimMemory(int level) {
3050 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003051 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003052 mAppsCustomizeTabHost.onTrimMemory();
3053 }
3054 }
3055
Adam Cohened307df2013-10-02 09:37:31 -07003056 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003057 showWorkspace(animated, null);
3058 }
3059
Adam Cohened307df2013-10-02 09:37:31 -07003060 protected void showWorkspace() {
3061 showWorkspace(true);
3062 }
3063
Adam Cohened66b2b2012-01-23 17:28:51 -08003064 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003065 if (mWorkspace.isInOverviewMode()) {
3066 mWorkspace.exitOverviewMode(animated);
3067 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003068 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003069 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003070 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003071 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072
Winson Chungc7d2b602012-05-16 17:02:20 -07003073 // Show the search bar (only animate if we were showing the drop target bar in spring
3074 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003075 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003076 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003077 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003078
Michael Jurkab3e22d92011-10-31 15:58:33 -07003079 // Set focus to the AppsCustomize button
3080 if (mAllAppsButton != null) {
3081 mAllAppsButton.requestFocus();
3082 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003083 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003084
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003085 // Change the state *after* we've called all the transition code
3086 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003087
Winson Chung5fb63472011-02-02 17:03:37 -08003088 // Resume the auto-advance of widgets
3089 mUserPresent = true;
3090 updateRunning();
3091
alanv1d4fde62012-10-17 13:15:47 -07003092 // Send an accessibility event to announce the context change
3093 getWindow().getDecorView()
3094 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003095
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003096 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003097 }
3098
Adam Cohened307df2013-10-02 09:37:31 -07003099 void showOverviewMode(boolean animated) {
3100 mWorkspace.setVisibility(View.VISIBLE);
3101 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3102 mState = State.WORKSPACE;
3103 onWorkspaceShown(animated);
3104 }
3105
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003106 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003107 }
3108
Winson Chung82963d52013-10-09 11:20:57 -07003109 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3110 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003111 if (mState != State.WORKSPACE) return;
3112
Winson Chung82963d52013-10-09 11:20:57 -07003113 if (resetPageToZero) {
3114 mAppsCustomizeTabHost.reset();
3115 }
Winson Chungc58497e2013-09-03 17:48:37 -07003116 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003117 mAppsCustomizeTabHost.requestFocus();
3118
Michael Jurkab3e22d92011-10-31 15:58:33 -07003119 // Change the state *after* we've called all the transition code
3120 mState = State.APPS_CUSTOMIZE;
3121
3122 // Pause the auto-advance of widgets until we are out of AllApps
3123 mUserPresent = false;
3124 updateRunning();
3125 closeFolder();
3126
3127 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003128 getWindow().getDecorView()
3129 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003130 }
3131
Adam Cohen7777d962011-08-18 18:58:38 -07003132 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003133 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003134 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003135 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003136 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003137 }
Adam Cohen7777d962011-08-18 18:58:38 -07003138
Adam Cohenad4e15c2013-10-17 16:21:35 -07003139 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003140 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003141 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3142
Winson Chunge7a03942011-08-05 15:05:12 -07003143 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003144 @Override
3145 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003146 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003147 // Before we show workspace, hide all apps again because
3148 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3149 // clean up our state transition functions
3150 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003151 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003152 } else {
3153 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003154 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003155 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003156 }, delay);
3157
Winson Chung557d6ed2011-07-08 15:34:52 -07003158 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003159
Michael Jurkad3ef3062010-11-23 16:23:58 -08003160 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003161 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003162 final boolean animated = true;
3163 final boolean springLoaded = true;
3164 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003165 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 }
3167 // Otherwise, we are not in spring loaded mode, so don't do anything.
3168 }
3169
Michael Jurkab3e22d92011-10-31 15:58:33 -07003170 void lockAllApps() {
3171 // TODO
3172 }
3173
3174 void unlockAllApps() {
3175 // TODO
3176 }
3177
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003182 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003183 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003184 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003185 int duration = 0;
3186 if (mSearchDropTargetBar != null) {
3187 duration = mSearchDropTargetBar.getTransitionInDuration();
3188 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003189 mHotseat.animate().alpha(1f).setDuration(duration);
3190 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003191 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003193 }
3194 }
3195 }
3196
3197 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003199 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003201 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003202 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003203 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003204 int duration = 0;
3205 if (mSearchDropTargetBar != null) {
3206 duration = mSearchDropTargetBar.getTransitionOutDuration();
3207 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003208 mHotseat.animate().alpha(0f).setDuration(duration);
3209 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003210 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003212 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003213 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003214 }
3215
Patrick Dubroy5f445422011-02-18 14:35:21 -08003216 /**
3217 * Add an item from all apps or customize onto the given workspace screen.
3218 * If layout is null, add to the current screen.
3219 */
3220 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003221 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003222 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003223 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003224 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003225
Winson Chungdff8ebb2011-09-08 17:25:31 -07003226 /** Maps the current orientation to an index for referencing orientation correct global icons */
3227 private int getCurrentOrientationIndexForGlobalIcons() {
3228 // default - 0, landscape - 1
3229 switch (getResources().getConfiguration().orientation) {
3230 case Configuration.ORIENTATION_LANDSCAPE:
3231 return 1;
3232 default:
3233 return 0;
3234 }
3235 }
3236
Michael Jurkaae65ee32012-04-30 11:45:54 -07003237 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003238 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003239 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003240 // Look for the toolbar icon specified in the activity meta-data
3241 Bundle metaData = packageManager.getActivityInfo(
3242 activityName, PackageManager.GET_META_DATA).metaData;
3243 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003244 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003245 if (iconResId != 0) {
3246 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003247 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003248 }
3249 }
3250 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003251 // This can happen if the activity defines an invalid drawable
3252 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3253 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003254 } catch (Resources.NotFoundException nfe) {
3255 // This can happen if the activity defines an invalid drawable
3256 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3257 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003258 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003259 return null;
3260 }
3261
3262 // if successful in getting icon, return it; otherwise, set button to use default drawable
3263 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003264 int buttonId, ComponentName activityName, int fallbackDrawableId,
3265 String toolbarResourceName) {
3266 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003267 Resources r = getResources();
3268 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3269 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003270
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003271 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003272 // If we were unable to find the icon via the meta-data, use a generic one
3273 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003274 toolbarIcon = r.getDrawable(fallbackDrawableId);
3275 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003276 if (button != null) {
3277 button.setCompoundDrawables(toolbarIcon, null, null, null);
3278 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003279 return null;
3280 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003281 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003282 if (button != null) {
3283 button.setCompoundDrawables(toolbarIcon, null, null, null);
3284 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003285 return toolbarIcon.getConstantState();
3286 }
3287 }
3288
3289 // if successful in getting icon, return it; otherwise, set button to use default drawable
3290 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003291 int buttonId, ComponentName activityName, int fallbackDrawableId,
3292 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003293 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003294 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003295
Michael Jurka19e0fc52011-07-22 18:00:21 -07003296 if (button != null) {
3297 // If we were unable to find the icon via the meta-data, use a
3298 // generic one
3299 if (toolbarIcon == null) {
3300 button.setImageResource(fallbackDrawableId);
3301 } else {
3302 button.setImageDrawable(toolbarIcon);
3303 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003304 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003305
3306 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3307
Michael Jurka0423dcf2010-10-05 14:56:18 -07003308 }
3309
Winson Chung1b884092012-06-08 17:13:02 -07003310 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003311 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003312 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003313 }
3314
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003315 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003316 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003317 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003318 }
3319
Winson Chungbb185bd2011-11-21 12:31:42 -08003320 private void invalidatePressedFocusedStates(View container, View button) {
3321 if (container instanceof HolographicLinearLayout) {
3322 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3323 layout.invalidatePressedFocusedStates();
3324 } else if (button instanceof HolographicImageView) {
3325 HolographicImageView view = (HolographicImageView) button;
3326 view.invalidatePressedFocusedStates();
3327 }
3328 }
3329
Cristina Stancu476493b2013-08-07 17:20:14 +01003330 public View getQsbBar() {
3331 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003332 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3333 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003334 }
3335 return mQsbBar;
3336 }
3337
3338 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003339 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003340 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003341 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003342 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003343
Winson Chung1cad91e2011-05-25 17:41:01 -07003344 final SearchManager searchManager =
3345 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3346 ComponentName activityName = searchManager.getGlobalSearchActivity();
3347 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003348 int coi = getCurrentOrientationIndexForGlobalIcons();
3349 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003350 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3351 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3352 if (sGlobalSearchIcon[coi] == null) {
3353 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3354 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3355 TOOLBAR_ICON_METADATA_NAME);
3356 }
3357
Winson Chungc51db6a2011-10-05 11:44:49 -07003358 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3359 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003360 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003361 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003362 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003363 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3365 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003366 if (searchButton != null) searchButton.setVisibility(View.GONE);
3367 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003368 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003369 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003370 }
3371 }
3372
Cristina Stancu476493b2013-08-07 17:20:14 +01003373 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003374 final View searchButtonContainer = findViewById(R.id.search_button_container);
3375 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003376 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003377 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003378 }
3379
Cristina Stancu476493b2013-08-07 17:20:14 +01003380 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003381 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003382 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003383
Winson Chungc51db6a2011-10-05 11:44:49 -07003384 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003385 final SearchManager searchManager =
3386 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3387 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3388
3389 ComponentName activityName = null;
3390 if (globalSearchActivity != null) {
3391 // Check if the global search activity handles voice search
3392 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3393 intent.setPackage(globalSearchActivity.getPackageName());
3394 activityName = intent.resolveActivity(getPackageManager());
3395 }
3396
3397 if (activityName == null) {
3398 // Fallback: check if an activity other than the global search activity
3399 // resolves this
3400 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3401 activityName = intent.resolveActivity(getPackageManager());
3402 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003403 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003404 int coi = getCurrentOrientationIndexForGlobalIcons();
3405 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003406 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3407 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3408 if (sVoiceSearchIcon[coi] == null) {
3409 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3410 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3411 TOOLBAR_ICON_METADATA_NAME);
3412 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003413 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003414 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003415 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003416 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003417 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003418 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003419 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003420 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003421 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003422 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003423 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003424 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003425
Cristina Stancu476493b2013-08-07 17:20:14 +01003426 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003427 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3428 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003429 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003430 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003431 }
3432
Winson Chung5841efa2013-09-30 18:06:44 -07003433 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003434 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3435 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003436 boolean visible = !forceDisableVoiceButtonProxy &&
3437 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003438 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003439 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003440 }
3441 }
Winson Chung5841efa2013-09-30 18:06:44 -07003442
3443 /**
3444 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3445 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3446 */
3447 public void disableVoiceButtonProxy(boolean disabled) {
3448 updateVoiceButtonProxyVisible(disabled);
3449 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003450 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003451 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003452 */
3453 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003454 if (!DISABLE_MARKET_BUTTON) {
3455 final View marketButton = findViewById(R.id.market_button);
3456 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3457 // Find the app market activity by resolving an intent.
3458 // (If multiple app markets are installed, it will return the ResolverActivity.)
3459 ComponentName activityName = intent.resolveActivity(getPackageManager());
3460 if (activityName != null) {
3461 int coi = getCurrentOrientationIndexForGlobalIcons();
3462 mAppMarketIntent = intent;
3463 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3464 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3465 TOOLBAR_ICON_METADATA_NAME);
3466 marketButton.setVisibility(View.VISIBLE);
3467 } else {
3468 // We should hide and disable the view so that we don't try and restore the visibility
3469 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3470 marketButton.setVisibility(View.GONE);
3471 marketButton.setEnabled(false);
3472 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003473 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003474 }
3475
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003476 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003477 if (!DISABLE_MARKET_BUTTON) {
3478 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3479 Resources r = getResources();
3480 Drawable marketIconDrawable = d.newDrawable(r);
3481 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3482 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3483 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003484
Winson Chungd64a6662013-09-30 11:06:59 -07003485 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3486 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003487 }
3488
Michael Jurkad7c28052012-04-27 15:43:36 -07003489 @Override
3490 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003491 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003492 final List<CharSequence> text = event.getText();
3493 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003494 // Populate event with a fake title based on the current state.
3495 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003496 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003497 } else {
3498 text.add(getString(R.string.all_apps_home_button_label));
3499 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003500 return result;
3501 }
3502
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003503 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003504 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003505 */
3506 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3507 @Override
3508 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003509 closeSystemDialogs();
3510 }
3511 }
3512
3513 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003514 * Receives notifications whenever the appwidgets are reset.
3515 */
3516 private class AppWidgetResetObserver extends ContentObserver {
3517 public AppWidgetResetObserver() {
3518 super(new Handler());
3519 }
3520
3521 @Override
3522 public void onChange(boolean selfChange) {
3523 onAppWidgetReset();
3524 }
3525 }
3526
3527 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003528 * If the activity is currently paused, signal that we need to run the passed Runnable
3529 * in onResume.
3530 *
3531 * This needs to be called from incoming places where resources might have been loaded
3532 * while we are paused. That is becaues the Configuration might be wrong
3533 * when we're not running, and if it comes back to what it was when we
3534 * were paused, we are not restarted.
3535 *
3536 * Implementation of the method from LauncherModel.Callbacks.
3537 *
3538 * @return true if we are currently paused. The caller might be able to
3539 * skip some work in that case since we will come back again.
3540 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003541 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003542 if (mPaused) {
3543 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003544 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003545 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003546 }
3547 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003548 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 return true;
3550 } else {
3551 return false;
3552 }
3553 }
3554
Michael Jurkac402cd92013-05-20 15:49:32 +02003555 private boolean waitUntilResume(Runnable run) {
3556 return waitUntilResume(run, false);
3557 }
3558
Michael Jurka1e2f4652013-07-08 18:03:46 -07003559 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003560 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003561 }
3562
Michael Jurka7607c2f2013-04-03 14:33:19 -07003563 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003564 * If the activity is currently paused, signal that we need to re-run the loader
3565 * in onResume.
3566 *
3567 * This needs to be called from incoming places where resources might have been loaded
3568 * while we are paused. That is becaues the Configuration might be wrong
3569 * when we're not running, and if it comes back to what it was when we
3570 * were paused, we are not restarted.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 *
3574 * @return true if we are currently paused. The caller might be able to
3575 * skip some work in that case since we will come back again.
3576 */
3577 public boolean setLoadOnResume() {
3578 if (mPaused) {
3579 Log.i(TAG, "setLoadOnResume");
3580 mOnResumeNeedsLoad = true;
3581 return true;
3582 } else {
3583 return false;
3584 }
3585 }
3586
3587 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003588 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003589 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003591 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003592 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003593 } else {
3594 return SCREEN_COUNT / 2;
3595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003597
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 /**
3599 * Refreshes the shortcuts shown on the workspace.
3600 *
3601 * Implementation of the method from LauncherModel.Callbacks.
3602 */
3603 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 // If we're starting binding all over again, clear any bind calls we'd postponed in
3605 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3606 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003607 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003608
Winson Chungd64d1762013-08-20 14:37:16 -07003609 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003610 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003611 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003612
Michael Jurka05bf6442011-11-30 20:28:53 -08003613 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003614 if (mHotseat != null) {
3615 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003616 }
3617 }
3618
Adam Cohendcd297f2013-06-18 13:13:40 -07003619 @Override
3620 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003621 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003622
Adam Cohen5084cba2013-09-03 12:01:16 -07003623 // If there are no screens, we need to have an empty screen
3624 if (orderedScreenIds.size() == 0) {
3625 mWorkspace.addExtraEmptyScreen();
3626 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003627
3628 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003629 // setCurrentPage() so ensure that all pages are added before calling this).
3630 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003631 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3632 mWorkspace.createCustomContentPage();
3633 }
Winson Chung64359a52013-07-08 17:17:08 -07003634 }
3635
3636 @Override
3637 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003638 int count = orderedScreenIds.size();
3639 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003640 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 }
3643
Adam Cohen39a06042013-07-19 14:30:12 -07003644 private boolean shouldShowWeightWatcher() {
3645 String spKey = LauncherAppState.getSharedPreferencesKey();
3646 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003647 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003648
3649 return show;
3650 }
3651
3652 private void toggleShowWeightWatcher() {
3653 String spKey = LauncherAppState.getSharedPreferencesKey();
3654 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3655 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3656
3657 show = !show;
3658
3659 SharedPreferences.Editor editor = sp.edit();
3660 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3661 editor.commit();
3662
3663 if (mWeightWatcher != null) {
3664 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3665 }
3666 }
3667
Winson Chungd64d1762013-08-20 14:37:16 -07003668 public void bindAppsAdded(final ArrayList<Long> newScreens,
3669 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003670 final ArrayList<ItemInfo> addAnimated,
3671 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003672 Runnable r = new Runnable() {
3673 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003674 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003675 }
3676 };
3677 if (waitUntilResume(r)) {
3678 return;
3679 }
3680
Winson Chungd64d1762013-08-20 14:37:16 -07003681 // Add the new screens
3682 bindAddScreens(newScreens);
3683
3684 // We add the items without animation on non-visible pages, and with
3685 // animations on the new page (which we will try and snap to).
3686 if (!addNotAnimated.isEmpty()) {
3687 bindItems(addNotAnimated, 0,
3688 addNotAnimated.size(), false);
3689 }
3690 if (!addAnimated.isEmpty()) {
3691 bindItems(addAnimated, 0,
3692 addAnimated.size(), true);
3693 }
Winson Chungc58497e2013-09-03 17:48:37 -07003694
Winson Chung87412982013-10-03 18:34:14 -07003695 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003696 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003697
Winson Chungc58497e2013-09-03 17:48:37 -07003698 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3699 addedApps != null && mAppsCustomizeContent != null) {
3700 mAppsCustomizeContent.addApps(addedApps);
3701 }
Winson Chungd64d1762013-08-20 14:37:16 -07003702 }
3703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003704 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 * Bind the items start-end from the list.
3706 *
3707 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003708 */
Winson Chung64359a52013-07-08 17:17:08 -07003709 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3710 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003711 Runnable r = new Runnable() {
3712 public void run() {
3713 bindItems(shortcuts, start, end, forceAnimateIcons);
3714 }
3715 };
3716 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003717 return;
3718 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003719
Winson Chungf0c6ae02012-03-21 16:10:31 -07003720 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003721 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3722 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003723 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003725 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003726 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003728
3729 // Short circuit if we are loading dock items for a configuration which has no dock
3730 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3731 mHotseat == null) {
3732 continue;
3733 }
3734
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 switch (item.itemType) {
3736 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3737 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003738 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003739 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003740
Winson Chung64359a52013-07-08 17:17:08 -07003741 /*
3742 * TODO: FIX collision case
3743 */
Winson Chungce376632013-07-11 16:12:41 -07003744 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3745 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3746 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3747 throw new RuntimeException("OCCUPIED");
3748 }
Winson Chung64359a52013-07-08 17:17:08 -07003749 }
3750
Adam Cohendcd297f2013-06-18 13:13:40 -07003751 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3752 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003753 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003754 // Animate all the applications up now
3755 shortcut.setAlpha(0f);
3756 shortcut.setScaleX(0f);
3757 shortcut.setScaleY(0f);
3758 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003759 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003762 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003763 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003764 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003765 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003766 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3767 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003769 default:
3770 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003772 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003773
Winson Chung997a9232013-07-24 15:33:46 -07003774 if (animateIcons) {
3775 // Animate to the correct page
3776 if (newShortcutsScreenId > -1) {
3777 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003778 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003779 final Runnable startBounceAnimRunnable = new Runnable() {
3780 public void run() {
3781 anim.playTogether(bounceAnims);
3782 anim.start();
3783 }
3784 };
Winson Chung997a9232013-07-24 15:33:46 -07003785 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003786 // We post the animation slightly delayed to prevent slowdowns
3787 // when we are loading right after we return to launcher.
3788 mWorkspace.postDelayed(new Runnable() {
3789 public void run() {
3790 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003791 mWorkspace.postDelayed(startBounceAnimRunnable,
3792 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003793 }
3794 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003795 } else {
3796 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003797 }
3798 }
Winson Chung64359a52013-07-08 17:17:08 -07003799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003801 }
3802
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 /**
3804 * Implementation of the method from LauncherModel.Callbacks.
3805 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003807 Runnable r = new Runnable() {
3808 public void run() {
3809 bindFolders(folders);
3810 }
3811 };
3812 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003813 return;
3814 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003815 sFolders.clear();
3816 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818
3819 /**
3820 * Add the views for a widget to the workspace.
3821 *
3822 * Implementation of the method from LauncherModel.Callbacks.
3823 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003824 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003825 Runnable r = new Runnable() {
3826 public void run() {
3827 bindAppWidget(item);
3828 }
3829 };
3830 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003831 return;
3832 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003833
Daniel Sandler843e8602010-06-07 14:59:01 -04003834 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3835 if (DEBUG_WIDGETS) {
3836 Log.d(TAG, "bindAppWidget: " + item);
3837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 final Workspace workspace = mWorkspace;
3839
3840 final int appWidgetId = item.appWidgetId;
3841 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003842 if (DEBUG_WIDGETS) {
3843 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3844 }
3845
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3847
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003849 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850
Adam Cohendcd297f2013-06-18 13:13:40 -07003851 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003853 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3854
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855 workspace.requestLayout();
3856
Daniel Sandler843e8602010-06-07 14:59:01 -04003857 if (DEBUG_WIDGETS) {
3858 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3859 + (SystemClock.uptimeMillis()-start) + "ms");
3860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 }
3862
Adam Cohen1462de32012-07-24 22:34:36 -07003863 public void onPageBoundSynchronously(int page) {
3864 mSynchronouslyBoundPages.add(page);
3865 }
3866
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 /**
3868 * Callback saying that there aren't any more items to bind.
3869 *
3870 * Implementation of the method from LauncherModel.Callbacks.
3871 */
Adam Cohene25af792013-06-06 23:08:25 -07003872 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003873 Runnable r = new Runnable() {
3874 public void run() {
3875 finishBindingItems(upgradePath);
3876 }
3877 };
3878 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003879 return;
3880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 if (mSavedState != null) {
3882 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003883 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885 mSavedState = null;
3886 }
3887
Adam Cohen1462de32012-07-24 22:34:36 -07003888 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003890 // If we received the result of any pending adds while the loader was running (e.g. the
3891 // widget configuration forced an orientation change), process them now.
3892 for (int i = 0; i < sPendingAddList.size(); i++) {
3893 completeAdd(sPendingAddList.get(i));
3894 }
3895 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896
Winson Chungc51db6a2011-10-05 11:44:49 -07003897 // Update the market app icon as necessary (the other icons will be managed in response to
3898 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003899 if (!DISABLE_MARKET_BUTTON) {
3900 updateAppMarketIcon();
3901 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003902
Winson Chungf0c6ae02012-03-21 16:10:31 -07003903 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003904 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003905 mWorkspace.getUniqueComponents(true, null);
3906 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003907 }
Adam Cohen99894d92013-06-14 11:22:59 -07003908
Adam Cohen66a01fd2013-06-11 12:48:00 -07003909 mWorkspace.post(new Runnable() {
3910 @Override
3911 public void run() {
3912 onFinishBindingItems();
3913 }
3914 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003915 }
3916
Winson Chunga0b7e862013-09-05 16:03:15 -07003917 public boolean isAllAppsButtonRank(int rank) {
3918 if (mHotseat != null) {
3919 return mHotseat.isAllAppsButtonRank(rank);
3920 }
3921 return false;
3922 }
3923
Winson Chunga2413752012-04-03 14:22:34 -07003924 private boolean canRunNewAppsAnimation() {
3925 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3926 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3927 }
3928
Winson Chungc9168342013-06-26 14:54:55 -07003929 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3930 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3931 PropertyValuesHolder.ofFloat("alpha", 1f),
3932 PropertyValuesHolder.ofFloat("scaleX", 1f),
3933 PropertyValuesHolder.ofFloat("scaleY", 1f));
3934 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3935 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3936 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3937 return bounceAnim;
3938 }
3939
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003940 @Override
3941 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003942 boolean searchVisible = updateGlobalSearchIcon();
3943 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003944 if (mSearchDropTargetBar != null) {
3945 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3946 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003947 }
3948
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003949 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 * Add the icons for all apps.
3951 *
3952 * Implementation of the method from LauncherModel.Callbacks.
3953 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003954 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07003955 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
3956 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3957 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3958 getHotseat().addAllAppsFolder(mIconCache, apps,
3959 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3960 }
3961 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003962 }
Winson Chungc58497e2013-09-03 17:48:37 -07003963 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07003964 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07003965 mAppsCustomizeContent.setApps(apps);
3966 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 }
3969
3970 /**
3971 * A package was updated.
3972 *
3973 * Implementation of the method from LauncherModel.Callbacks.
3974 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003975 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003976 Runnable r = new Runnable() {
3977 public void run() {
3978 bindAppsUpdated(apps);
3979 }
3980 };
3981 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003982 return;
3983 }
3984
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003985 if (mWorkspace != null) {
3986 mWorkspace.updateShortcuts(apps);
3987 }
Winson Chungc58497e2013-09-03 17:48:37 -07003988
3989 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3990 mAppsCustomizeContent != null) {
3991 mAppsCustomizeContent.updateApps(apps);
3992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 }
3994
3995 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003996 * A package was uninstalled. We take both the super set of packageNames
3997 * in addition to specific applications to remove, the reason being that
3998 * this can be called when a package is updated as well. In that scenario,
3999 * we only remove specific components from the workspace, where as
4000 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 *
4002 * Implementation of the method from LauncherModel.Callbacks.
4003 */
Winson Chung83892cc2013-05-01 16:53:33 -07004004 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004005 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004006 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004007 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004008 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004009 }
Winson Chungd64d1762013-08-20 14:37:16 -07004010 };
4011 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004012 return;
4013 }
4014
Winson Chungdf95eb12013-10-16 14:57:07 -07004015 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004016 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004017 }
4018 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004019 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004020 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004021
Winson Chunga1820962011-10-03 16:31:06 -07004022 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004023 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004024
Winson Chungdf95eb12013-10-16 14:57:07 -07004025 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004026 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4027 mAppsCustomizeContent != null) {
4028 mAppsCustomizeContent.removeApps(appInfos);
4029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 }
Joe Onoratobe386092009-11-17 17:32:16 -08004031
4032 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004033 * A number of packages were updated.
4034 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004035 private ArrayList<Object> mWidgetsAndShortcuts;
4036 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004037 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004038 bindPackagesUpdated(mWidgetsAndShortcuts);
4039 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004040 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004041 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004042 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4043 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4044 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004045 return;
4046 }
4047
Winson Chung64359a52013-07-08 17:17:08 -07004048 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004049 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4050 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004051 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004052 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004053 }
4054
Winson Chung400438b2011-01-16 17:53:48 -08004055 private int mapConfigurationOriActivityInfoOri(int configOri) {
4056 final Display d = getWindowManager().getDefaultDisplay();
4057 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4058 switch (d.getRotation()) {
4059 case Surface.ROTATION_0:
4060 case Surface.ROTATION_180:
4061 // We are currently in the same basic orientation as the natural orientation
4062 naturalOri = configOri;
4063 break;
4064 case Surface.ROTATION_90:
4065 case Surface.ROTATION_270:
4066 // We are currently in the other basic orientation to the natural orientation
4067 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4068 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4069 break;
4070 }
4071
4072 int[] oriMap = {
4073 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4074 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4075 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4076 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4077 };
4078 // Since the map starts at portrait, we need to offset if this device's natural orientation
4079 // is landscape.
4080 int indexOffset = 0;
4081 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4082 indexOffset = 1;
4083 }
4084 return oriMap[(d.getRotation() + indexOffset) % 4];
4085 }
Adam Cohen446e9402011-09-15 18:21:21 -07004086
Winson Chung4b919f82012-05-01 10:44:08 -07004087 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004088 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004089 getResources().getBoolean(R.bool.allow_rotation);
4090 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004091 }
Winson Chung4b919f82012-05-01 10:44:08 -07004092 public void lockScreenOrientation() {
4093 if (isRotationEnabled()) {
4094 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4095 .getConfiguration().orientation));
4096 }
4097 }
4098 public void unlockScreenOrientation(boolean immediate) {
4099 if (isRotationEnabled()) {
4100 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004101 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004102 } else {
4103 mHandler.postDelayed(new Runnable() {
4104 public void run() {
4105 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4106 }
4107 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004108 }
Winson Chung4b919f82012-05-01 10:44:08 -07004109 }
Winson Chung400438b2011-01-16 17:53:48 -08004110 }
4111
Winson Chung82f55532011-08-09 14:14:23 -07004112 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004114 if (DISABLE_CLINGS) {
4115 return false;
4116 }
4117
Winson Chung3a6e7f32013-10-09 15:50:52 -07004118 // For now, limit only to phones
4119 LauncherAppState app = LauncherAppState.getInstance();
4120 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4121 if (grid.isTablet()) {
4122 return false;
4123 }
4124
Brett Chabot2a9e2282011-08-23 15:03:13 -07004125 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004126 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004127
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004128 // Disable clings for accessibility when explore by touch is enabled
4129 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4130 ACCESSIBILITY_SERVICE);
4131 if (a11yManager.isTouchExplorationEnabled()) {
4132 return false;
4133 }
4134
Michael Jurkae233a8b2013-03-19 13:49:20 +01004135 // Restricted secondary users (child mode) will potentially have very few apps
4136 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004137// boolean supportsLimitedUsers =
4138// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4139// Account[] accounts = AccountManager.get(this).getAccounts();
4140// if (supportsLimitedUsers && accounts.length == 0) {
4141// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4142// Bundle restrictions = um.getUserRestrictions();
4143// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4144// return false;
4145// }
4146// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004147 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004148 }
Michael Jurka22143132012-10-04 17:42:38 +02004149
Winson Chungfa545132013-09-26 17:45:32 -07004150 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004151 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004152 Cling cling = (Cling) findViewById(clingId);
4153 View scrim = null;
4154 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004155 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004156 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004158 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004159 cling.show(animate, SHOW_CLING_DURATION);
4160
4161 if (dimNavBarVisibilty) {
4162 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4163 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 }
4165 }
4166 return cling;
4167 }
Michael Jurka22143132012-10-04 17:42:38 +02004168
Winson Chungaf40f202013-09-18 18:26:31 -07004169 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4170 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004171 // To catch cases where siblings of top-level views are made invisible, just check whether
4172 // the cling is directly set to GONE before dismissing it.
4173 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004174 final Runnable cleanUpClingCb = new Runnable() {
4175 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004176 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004177 // We should update the shared preferences on a background thread
4178 new Thread("dismissClingThread") {
4179 public void run() {
4180 SharedPreferences.Editor editor = mSharedPrefs.edit();
4181 editor.putBoolean(flag, true);
4182 editor.commit();
4183 }
4184 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004185 if (postAnimationCb != null) {
4186 postAnimationCb.run();
4187 }
4188 }
4189 };
4190 if (duration <= 0) {
4191 cleanUpClingCb.run();
4192 } else {
4193 cling.hide(duration, cleanUpClingCb);
4194 }
Michael Jurka22143132012-10-04 17:42:38 +02004195 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004196
4197 if (restoreNavBarVisibilty) {
4198 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4199 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4200 }
Winson Chung82f55532011-08-09 14:14:23 -07004201 }
4202 }
Michael Jurka22143132012-10-04 17:42:38 +02004203
Winson Chung9d9d74f2011-09-19 11:49:12 -07004204 private void removeCling(int id) {
4205 final View cling = findViewById(id);
4206 if (cling != null) {
4207 final ViewGroup parent = (ViewGroup) cling.getParent();
4208 parent.post(new Runnable() {
4209 @Override
4210 public void run() {
4211 parent.removeView(cling);
4212 }
4213 });
Michael Jurka22143132012-10-04 17:42:38 +02004214 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004215 }
4216 }
Michael Jurka974c3862012-05-22 22:00:31 -07004217
4218 private boolean skipCustomClingIfNoAccounts() {
4219 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4220 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4221 if (customCling) {
4222 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004223 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004224 Account[] accounts = am.getAccountsByType("com.google");
4225 return accounts.length == 0;
4226 }
4227 return false;
4228 }
4229
Winson Chung82e5c982013-10-09 12:04:50 -07004230 public void updateCustomContentHintVisibility() {
4231 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4232 String ccHintStr = getFirstRunCustomContentHint();
4233
4234 if (mWorkspace.hasCustomContent()) {
4235 // Show the custom content hint if ccHintStr is not empty
4236 if (cling != null) {
4237 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4238 }
4239 } else {
4240 // Hide the custom content hint
4241 if (cling != null) {
4242 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4243 }
4244 }
4245 }
4246
4247 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4248 boolean animate) {
4249 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4250 if (ccHint != null) {
4251 if (visible && !ccHintStr.isEmpty()) {
4252 ccHint.setText(ccHintStr);
4253 ccHint.setVisibility(View.VISIBLE);
4254 if (animate) {
4255 ccHint.setAlpha(0f);
4256 ccHint.animate().alpha(1f)
4257 .setDuration(SHOW_CLING_DURATION)
4258 .start();
4259 } else {
4260 ccHint.setAlpha(1f);
4261 }
4262 } else {
4263 if (animate) {
4264 ccHint.animate().alpha(0f)
4265 .setDuration(SHOW_CLING_DURATION)
4266 .setListener(new AnimatorListenerAdapter() {
4267 @Override
4268 public void onAnimationEnd(Animator animation) {
4269 ccHint.setVisibility(View.GONE);
4270 }
4271 })
4272 .start();
4273 } else {
4274 ccHint.setAlpha(0f);
4275 ccHint.setVisibility(View.GONE);
4276 }
4277 }
4278 }
4279 }
4280
Winson Chungaf40f202013-09-18 18:26:31 -07004281 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004282 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004283 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004284 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004285 // If we're not using the default workspace layout, replace workspace cling
4286 // with a custom workspace cling (usually specified in an overlay)
4287 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004288 if (!DISABLE_CUSTOM_CLINGS) {
4289 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4290 getResources().getBoolean(R.bool.config_useCustomClings)) {
4291 // Use a custom cling
4292 View cling = findViewById(R.id.workspace_cling);
4293 ViewGroup clingParent = (ViewGroup) cling.getParent();
4294 int clingIndex = clingParent.indexOfChild(cling);
4295 clingParent.removeViewAt(clingIndex);
4296 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4297 clingParent.addView(customCling, clingIndex);
4298 customCling.setId(R.id.workspace_cling);
4299 }
4300 }
4301 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4302 if (cling != null) {
4303 String sbHintStr = getFirstRunClingSearchBarHint();
4304 String ccHintStr = getFirstRunCustomContentHint();
4305 if (!sbHintStr.isEmpty()) {
4306 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4307 sbHint.setText(sbHintStr);
4308 sbHint.setVisibility(View.VISIBLE);
4309 }
Winson Chung82e5c982013-10-09 12:04:50 -07004310 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004311 }
Winson Chungfa545132013-09-26 17:45:32 -07004312 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004313 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004314 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004315 }
4316 }
Winson Chungaf40f202013-09-18 18:26:31 -07004317
Winson Chunge6eabff2013-09-24 11:01:23 -07004318 protected String getFirstRunClingSearchBarHint() {
4319 return "";
4320 }
4321 protected String getFirstRunCustomContentHint() {
4322 return "";
4323 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004324 protected int getFirstRunFocusedHotseatAppDrawableId() {
4325 return -1;
4326 }
4327 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4328 return null;
4329 }
4330 protected int getFirstRunFocusedHotseatAppRank() {
4331 return -1;
4332 }
4333 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4334 return "";
4335 }
4336 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4337 return "";
4338 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004339
Winson Chungaf40f202013-09-18 18:26:31 -07004340 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004341 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004342 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004343 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004344 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4345
4346 // Set the focused hotseat app if there is one
4347 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4348 getFirstRunFocusedHotseatAppRank(),
4349 getFirstRunFocusedHotseatAppComponentName(),
4350 getFirstRunFocusedHotseatAppBubbleTitle(),
4351 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004352 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004353 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004354 }
4355 }
4356 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004357 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004358 if (isClingsEnabled() &&
4359 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004360 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4361 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004362 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004363 } else {
4364 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004365 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004366 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004367 }
Michael Jurka104c4562013-07-08 18:03:46 -07004368 protected SharedPreferences getSharedPrefs() {
4369 return mSharedPrefs;
4370 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004371 public boolean isFolderClingVisible() {
4372 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004373 if (cling != null) {
4374 return cling.getVisibility() == View.VISIBLE;
4375 }
4376 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004377 }
Winson Chungaf40f202013-09-18 18:26:31 -07004378 public void dismissFirstRunCling(View v) {
4379 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4380 Runnable cb = new Runnable() {
4381 public void run() {
4382 // Show the workspace cling next
4383 showFirstRunWorkspaceCling();
4384 }
4385 };
4386 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4387 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004388
4389 // Fade out the search bar for the workspace cling coming up
4390 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004391 }
Winson Chung82f55532011-08-09 14:14:23 -07004392 public void dismissWorkspaceCling(View v) {
4393 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004394 Runnable cb = null;
4395 if (v == null) {
4396 cb = new Runnable() {
4397 public void run() {
4398 mWorkspace.enterOverviewMode();
4399 }
4400 };
4401 }
4402 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4403 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004404
4405 // Fade in the search bar
4406 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004407 }
4408 public void dismissFolderCling(View v) {
4409 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004410 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4411 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004412 }
4413
Winson Chung80baf5a2010-08-09 16:03:15 -07004414 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004415 * Prints out out state for debugging.
4416 */
4417 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004418 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004419 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004420 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4421 Log.d(TAG, "mRestoring=" + mRestoring);
4422 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4423 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004424 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004425 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004426
Winson Chung785d2eb2011-04-14 16:08:02 -07004427 if (mAppsCustomizeContent != null) {
4428 mAppsCustomizeContent.dumpState();
4429 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004430 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004431 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004432
4433 @Override
4434 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4435 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004436 synchronized (sDumpLogs) {
4437 writer.println(" ");
4438 writer.println("Debug logs: ");
4439 for (int i = 0; i < sDumpLogs.size(); i++) {
4440 writer.println(" " + sDumpLogs.get(i));
4441 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004442 }
4443 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004444
4445 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004446 if (DEBUG_DUMP_LOG) {
4447 synchronized (sDumpLogs) {
4448 Log.d(TAG, "");
4449 Log.d(TAG, "*********************");
4450 Log.d(TAG, "Launcher debug logs: ");
4451 for (int i = 0; i < sDumpLogs.size(); i++) {
4452 Log.d(TAG, " " + sDumpLogs.get(i));
4453 }
4454 Log.d(TAG, "*********************");
4455 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004456 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004457 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004458 }
4459
4460 public static void addDumpLog(String tag, String log, boolean debugLog) {
4461 if (debugLog) {
4462 Log.d(tag, log);
4463 }
Winson Chungede41292013-09-19 16:27:36 -07004464 if (DEBUG_DUMP_LOG) {
4465 sDateStamp.setTime(System.currentTimeMillis());
4466 synchronized (sDumpLogs) {
4467 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004468 }
Winson Chungede41292013-09-19 16:27:36 -07004469 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004470 }
4471
Winson Chungede41292013-09-19 16:27:36 -07004472 public void dumpLogsToLocalData() {
4473 if (DEBUG_DUMP_LOG) {
4474 new Thread("DumpLogsToLocalData") {
4475 @Override
4476 public void run() {
4477 boolean success = false;
4478 sDateStamp.setTime(sRunStart);
4479 String FILENAME = sDateStamp.getMonth() + "-"
4480 + sDateStamp.getDay() + "_"
4481 + sDateStamp.getHours() + "-"
4482 + sDateStamp.getMinutes() + "_"
4483 + sDateStamp.getSeconds() + ".txt";
4484
4485 FileOutputStream fos = null;
4486 File outFile = null;
4487 try {
4488 outFile = new File(getFilesDir(), FILENAME);
4489 outFile.createNewFile();
4490 fos = new FileOutputStream(outFile);
4491 } catch (Exception e) {
4492 e.printStackTrace();
4493 }
4494 if (fos != null) {
4495 PrintWriter writer = new PrintWriter(fos);
4496
4497 writer.println(" ");
4498 writer.println("Debug logs: ");
4499 synchronized (sDumpLogs) {
4500 for (int i = 0; i < sDumpLogs.size(); i++) {
4501 writer.println(" " + sDumpLogs.get(i));
4502 }
4503 }
4504 writer.close();
4505 }
4506 try {
4507 if (fos != null) {
4508 fos.close();
4509 success = true;
4510 }
4511 } catch (IOException e) {
4512 e.printStackTrace();
4513 }
4514 }
4515 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004516 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004518}
Michael Jurka2763be32011-02-24 11:19:57 -08004519
Michael Jurkaabded662011-03-04 12:06:57 -08004520interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004521 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004522 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004523 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004524 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004525 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004526}